【SSM_02】Spring注解

2. 注意
在 spring 容器中有一些内置的名称,例如:vlaue="${username}" 获取的不是配置文件中的值而是主机的用户名。二、spring 注解配置
1. 前置条件
<!-- 开启组件扫描 --><context:component-scan base-package="com.softwareMan"></context:component-scan
2. 原始注解
① 创建对象        作用在【类】上相当于 <bean id="userDao" class="com.softwareMan.dao.impl.UserDaoImpl"></bean>    - @Component     //用于普通类
    - @Service       //用于service层
    - @Repository    //用于dao层
    - @Controller    //用于controller层
    - 注意
        * 以上四个注解作用相同,只是用来区分是那一层
        * 若注解未赋值则默认使用类名首字母小写作为id
② 注入数据        作用在【成员变量】上相当于<property name="driverClass" value="${driverClassName}" />
    - @Autowired      //先按照字节码文件来查找,其次按照变量名来匹配
    - @Qualifier()    //按照 id 来查找
    - @Resource       //未配置参数相当于 @Autowired 配置了参数相当于 @Autowired + @Qualifier()
    - @Value()        //注入基本数据类型 + string
    - 注意
        * @Autowired 、@Qualifier() 在 spring 包中
        * @Resource 在 jdk javax包中,【jdk 1.8可用】
③ 改变作用范围        作用在【类】上
    - @Scope     //相当的与 bean 标签中的 scope 属性
④ 声明周期        作用在【方法】上相当于 bean 标签中 init-method 、 destory-method 属性
    - @PosrContruct        //创建的方法
    - @PreDestory          //销毁的方法
3. 新注解
① @Configuration        //指定一个类为配置类② @ComponentScan        //指定要扫描的包
③ @Bean                 //将方法的返回值存入 spring 容器中
④ @PropertySource       //加载配置文件
⑤ @Import               //导入其他配置类
    
# 示例
   //标志该类是Spring的核心配置类
   @Configuration
   //<context:component-scan base-package="com.software"/>
   @ComponentScan("com.software")
   //<import resource=""/>
   @Import({DataSourceConfiguration.class})
   public class SpringCofiguration {
   }
    /*
        DataSourceConfiguration 类
    */
    //<context:property-placeholder location="classpath:jdbc.properties"/>
    @PropertySource("classpath:jdbc.properties")
    public class DataSourceConfiguration {
        @Value("${jdbc.driver}")
        private String driver;
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.username}")
        private String username;
        @Value("${jdbc.password}")
        private String password;
        @Bean("dataSource")  //Spring会将当前方法的返回值以指定名称存储到Spring容器中
        public DataSource getDataSource() throws PropertyVetoException {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass(driver);
            dataSource.setJdbcUrl(url);
            dataSource.setUser(username);
            dataSource.setPassword(password);
            return dataSource;
        }
    }
三、spring 整合 junit
1. 使用步骤
① 导包② 使用 @Runwith 替换原来的运行期
③ 使用 @ConfigConfiguration 指定配置文件或配置类
④ 使用 @Autowired 注入测试对象
⑤ 开始测试
2. 示例
@RunWith(SpringJUnit4ClassRunner.class)//@ContextConfiguration("classpath:applicationContext.xml")
@ContextConfiguration(classes = {SpringCofiguration.class})
public class SpringJunitTest {
    @Autowired
    private UserService userService;
    @Autowired
    private DataSource dataSource;
    @Test
    public void test1() throws SQLException {
        userService.save();
        System.out.println(dataSource.getConnection());
    }
}
以上是 【SSM_02】Spring注解 的全部内容, 来源链接: utcz.com/z/513474.html








