天天看点

Spring 核心注解

@Configuration 配置类注解

声明一个类是配置类,等同于配置文件中的​

​applicationcontext.xml​

​​。

加载Spring配置的两种方式:

  1. ClassPathXmlApplicationContext XML方式
ApplicationContext app = new ClassPathXmlApplicationContext("applicationcontext.xml");      
  1. AnnoatationConfigApplicationContext 注解方式
ApplicationContext app = new AnnotationConfigApplicationContext(MainConfig.class);      

@ComponentScan 包扫描注解

@ComponentScan(value="com.enjoy.cap2")表示扫描此目录下的包。      

参数说明:

  • value:指定要扫描的包
  • excludeFilters = Filter[]:指定扫描的时候按照什么规则排除那些组件
  • includeFilters = Filter[]:指定扫描的时候只需要包含哪些组件
  • useDefaultFilters = false:默认是true,扫描所有组件,要改成false,includeFilters 和excludeFilters才会生效

扫描规则如下:

  • FilterType.ANNOTATION:按照注解
  • FilterType.ASSIGNABLE_TYPE:按照给定的类型;比如按BookService类型
  • FilterType.ASPECTJ:使用ASPECTJ表达式
  • FilterType.REGEX:使用正则指定
  • FilterType.CUSTOM:使用自定义规则,自已写类,实现TypeFilter接口

示例:

/**
 * 包扫描测试类
 *
 * @author yuhao.wang3
 * @since 2019/8/24 10:39
 */
@Configuration
@ComponentScan(value = "com.xiaolyuh.component.scan", includeFilters = {
        // 只扫描Controller注解的 Bean
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
        // 只扫描特定的类
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {TestService.class}),
        // 使用特定的过滤规则
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {CustomTypeFilter.class})
}, useDefaultFilters = false)
public class ComponentScanConfig {

}      
@Test
public void contextTest() {
    String[] names = context.getBeanDefinitionNames();
    Arrays.stream(names).forEach(System.out::println);
}      

输出结果:

componentScanConfig
testController
customTypeFilter
customTypeFilterService
testService      
TestDao就没有被加载,完整代码请到仓库拉取

@Bean 声明一个Bean,并放到Spring容器

将一个普通类声明成一个Spring里面的Bean。等同于XML中的 ​

​<bean id="xxx" class="xxx"/>​

​​。​​Spring bean 的初始化和销毁的三种方式和执行顺序​​ 可参考这里。

@Scope 描述的是Spring容器如何创建Bean的实例的

  • singleton:一个Spring容器中只有一个Bean的实例,以为Spring的默认配置,全容器共享一个实例,在容器初始化的时候,完成Bean的初始化。
  • prototype:每次调用新建一个Bean的实例。在容器初始化的时候也会存一个没有初始化的Bean标示到容器。
  • request:Web项目中,给每一个HTTP request新建一个Bean的实例。
  • session:Web项目中,给每一个HTTP session新建一个Bean的实例。

示例:

@Configuration
public class ScopeConfig {

    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Bean("scopeTestBean")
    public ScopeTestBean scopeTestBean() {
        return new ScopeTestBean();
    }
}      

@Lazy

在 Spring 单例模式下,默认在容器初始化的时候创建Bean,如果加了@Lazy注解,那么容器启动的时候不会去实例化对象,而是在第一次使用Bean的时候才回去创建对象。

示例:

@Configuration
public class LazyConfig {

    @Lazy
    @Bean
    public LazyTestBean lazyTestBean() {
        return new LazyTestBean();
    }
}      

Spring 声明Bean的注解

  • @Component: 组件,没有明确的角色。
  • @Service : 在业务逻辑层(Service层)使用。
  • @Repository:  再数据访问层(Dao层)使用。
  • @Controller: 再展现层(MVC->Spring MVC)使用。

Spring 注入Bean的注解:

  • @Autowired:Spring提供的注解。
  • @inject:JSR-330提供的注解。
  • @Resource:JSP-250提供的注解。

​​声明Spring Bean和注入Bean的几种常用注解和区别​​可参考这里。

@Conditional 条件注入

当引入@Conditional时, 容器可以选择性的注册bean。具体可以参考:​​Spring 条件注解(@Conditional)​​。

@Import注册bean

​@Import​

​​注解一次可以注入多个Bean,默认使用全类名作为Bean id;而​

​@Bean​

​​一次只能注入一个Bean,默认使用方法名作为Bean id。

详情可参考:​​​声明Spring Bean和注入Bean的几种常用注解和区别​​

源码

​​https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases​​

layering-cache