Spring基于注解的IoC配置

版权声明: https://blog.csdn.net/qq_23329167/article/details/82791926

基于注解的IoC配置,与在xml中配置目的是一样的,都是降低代码之间的耦合度的,只是配置的形式不一样。

使用注解的步骤:
    1、添加context的名称控件和约束
    2、开启注解扫描:由spring扫描指定的包及其子包下的所有类,如果类上使用了@Component注解,就将该类装配到容器中
    3、在类上使用@Component注解

使用注解之前一定要先开启注解扫描

<!-- 配置注解扫描的包:声明到指定的包下去进行扫描,如果发现类上有对应的注解,将其装配到容器中 -->
<context:component-scan base-package="com.demon"/>

一、使用注解装配bean

1.1 @Component

作用:在类上面使用该注解,把资源让spring来管理。相当于在xml中配置了一个bean

属性:value:指定bean的id。如果不指定value属性,默认bean的id是当前类的类名,首字母小写。

/**
 * @Component注解:相当于配置了<bean>标签
 * value = "util":相当于配置了bean标签的id属性,单独配置value时,可以省略value属性名称。
 */
@Component(value="util")
public class Util {
    
}

1.2 @Controller

一般用于表现层的注解。用法与@Component相同

1.3 @Service

一般用于业务层的注解。用法与@Component相同

1.4 @Repository

一般用于持久层的注解。 用法与@Component相同

二、用于注入数据的

2.1 @Autowired

作用:自动按类型注入(将该类的子类注入进来),当使用注解注入属性时,set方法可以省略。它只能注入其他bean类型。当有多个类型匹配时,默认使用属性名称作为bean的id,去容器中查找该id的bean并注入。

2.2 @Qualifier

作用: 在自动按照类型注入的基础之上,再按照Bean的id注入。它在给字段注入时不能独立使用,必须和@Autowire一起使用;但是给方法参数注入时,可以独立使用。

属性: value:指定bean的id。

2.3 @Resource

作用: 直接按照Bean的id注入。它也只能注入其他bean类型。 

属性: name:指定bean的id。

2.4 @Value

作用:注入基本数据类型和String类型数据的 

属性:value:用于指定值

三、作用范围的注解

3.1 @Scope

作用: 指定bean的作用范围。 

属性: value:指定范围的值。 取值:singleton/prototype request session globalsession

四、生命周期相关的

4.1 @PostConstruct

作用:指定某个方法为初始化方法

4.2 @PreDestory

作用:指定某个方法为销毁方法

五、配置类相关的

5.1 @Configuration

作用: 用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解。获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class)。 

/**
 * spring的配置类,相当于applicationContext.xml文件
 * @author Demon
 * @date 2018/9/20 18:06
 */
@Configuration
public class SpringConfiguration {

}

5.2 @ComponentScan

作用: 用于指定spring在初始化容器时要扫描的包。作用和在spring的xml配置文件中的: 

<context:component-scan base-package="com.demon"/>

属性:Value(单独使用可省略):用于指定要扫描的包。和标签中的basePackages属性作用一样。 

/**
 * spring的配置类,相当于bean.xml文件
 * @author Demon
 * @date 2018/9/20 18:06
 */
@Configuration
@ComponentScan({"com.demon"})
public class SpringConfiguration {

}

5.3 @PropertySource

作用: 用于加载.properties文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到properties配置文件中,就可以使用此注解指定properties配置文件的位置。 

属性: value[]:用于指定properties文件位置。如果是在类路径下,需要写上classpath:

@PropertySource(value = {"classpath:jdbc.properties"})
public class JdbcConfig {
    @Value(value = "${jdbc.driverClass}")
    private String driverClassName;
    @Value(value = "${jdbc.url}")
    private String url;
    @Value(value = "${jdbc.username}")
    private String username;
    @Value(value = "${jdbc.password}")
    private String password;

    @Bean(name = {"jdbcTemplate"})
    public JdbcTemplate createJdbcTemplate(@Qualifier(value = "dataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = {"dataSource"})
    public DataSource createDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

5.4 @Import

作用: 用于导入其他配置类,在引入其他配置类时,其他类上可以不用再写@Configuration注解。当然,写上也没问题。 

属性: value[]:用于指定其他配置类的字节码。 

@Configuration
@ComponentScan(value = {"com.demon"})
@Import(value = JdbcConfig.class)
public class SpringConfiguration {

}

5.5 @Bean

作用: 该注解只能写在方法上,将方法的返回值作为一个bean,并且放入spring容器。 

属性: name:给当前@Bean注解方法创建的对象指定一个名称(即bean的id)。

@Bean(name = {"jdbcTemplate"})
public JdbcTemplate createJdbcTemplate(@Qualifier(value = "dataSource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
}

@Bean(name = {"dataSource"})
public DataSource createDataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
}

5.6 通过注解获取容器

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);

六、spring的test模块整合junit

使用spring的test模块的时候需要在pom.xml文件中引入

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.0.6.RELEASE</version>
</dependency>

6.1 @RunWith

作用:替换掉junit的运行器,换成一个可以初始化spring容器的运行器。

属性:value:单独配置时,value属性名称可以省略,配置SpringJUnit4ClassRunner.class来代替原来junit的运行器。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class AccountControllerTest {
    @Autowired
    private AccountController accountController;
}

6.2 @ContextConfiguration

作用:加载配置类或者xml配置文件

属性:

        value[]:用来指定xml配置文件的路径

        class[]: 用来指定配置类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class AccountControllerTest {
    @Autowired
    private AccountController accountController;
}

猜你喜欢

转载自blog.csdn.net/qq_23329167/article/details/82791926