Spring 核心注解&原理

1. stereotype 注解(spring context)

1.1 @Component

加在类名上 表示这个类会被解析成spring的bean
当不给这个类指定bean的名字时,默认采用类名首字母变小写的方式 也可以手动指定@Component("bean的名字")

1.2 @Repository

注解上加了 @Component,我们使用时等于在使用 Component。只是用于区分代码层次。

1.3 @Controller

注解上加了 @Component,我们使用时等于在使用 Component。只是用于区分代码层次。

1.4 @Service

注解上加了 @Component,我们使用时等于在使用 Component。只是用于区分代码层次。

2.context 注解(spring context)

2.1 @ComponentScan

通过扫描的方式,寻找spring bean。扫描过程中,寻找 @Component 和 @Configuration 两种注解修饰过的类

2.1 @Configuration

注解上加了 @Component,我们使用时等于在使用 Component。只是用于区分代码层次。

2.3 @Bean

与 Configuration 配合使用,用于使用Java代码的方式实例化spring bean。非常灵活

2.4 @Primary

当同一个接口存在多个实现类时,使用Primary在其中一个实现类上,声明它是默认的第一优先使用的bean

2.5 @Import

2.6 @ImportResource

将spring xml配置文件引入到spring中。

@ImportResource({"classpath:/user.xml"})
@Configuration
@Slf4j
public class XmlConfig {

    @Resource(name = "myUser")
    private User user;

    @PostConstruct
    public void init() {
        log.info("myUser={}", user);
    }

}

2.7 @Lazy

用于定义延迟加载的bean

2.8 @PropertySource

引入配置文件到spring

@PropertySource(value = {
        "classpath:/my.properties",
        "classpath:/my.yml",
}
        , name = "ss"
        , encoding = "utf-8"
        , factory = PropertySourceFactory.class// 用来读取yml配置
        , ignoreResourceNotFound = true)

2.9 @Scope

定义bean的作用域,ConfigurableBeanFactory 中定义了枚举

2.10 @Conditional

作用于实例化bean时,比如 Component、Import、Bean等。作为一个条件,去判断是否符合实例化对象的条件。 在spring boot中,丰富的这块功能,扩展了很多功能,比如:

  • ConditionalOnBean : bean存在
  • ConditionalOnMissingBean: bean不存在
  • ConditionalOnClass: class存在
  • ConditionalOnMissingClass : class不存在
  • ConditionalOnProperty: 配置是否存在,并对值进行正则匹配

2.11 @Profile

一种特殊的Conditional,用于在不同的环境实例化不同的对象

@Bean
@Profile({"dev", "test"})// 设置 dev test 环境开启,保证我们的效率
public String performanceInterceptor() {
    log.info("Profile_String_demo");
    return null;
}

2.12 @DependsOn

与Component配合使用,声明在实例化对象之前,必须先将其依赖的对象先实例化好,才能开始实例化本对象

3.scheduling 注解(spring context)

3.1 @EnableScheduling & @Scheduled

用于开启spring的定时任务,并使用Scheduled定义定时任务的执行规则

3.3 @EnableAsync & @Async

开启异步执行刚刚,使用 @Async 进行异步处理。 默认使用 SimpleAsyncTaskExecutor 并不是线程池,每次都创建新的线程,导致线程不复用,服务器性能降低,所有一般使用的时候,会指定一个线程池

4. format 注解(spring context)

4.1 @NumberFormat

数字格式化

4.2 @DateTimeFormat

时间格式化

5.cache 注解(spring context)

5.1 @Cacheable

5.2 @CacheConfig

5.3 @CacheEvict

5.4 @CachePut

5.5 @Caching

5.6 @EnableCaching

6.spring beans 注解

依赖注入相关的注解

6.1 @Autowired

作用: 属性自动状态自动装配

  1. 装配顺序:类型装配 => 当有接口存在多个实现类时,按照名称装配(名称一般去字段名) => 找不到就报错
  2. 可选依赖:如果你的依赖是可选的,可以使用required=false设置
  3. 作用于:属性、set方法上、构造函数、@Bean方法的参数上面

6.2 @Value

Autowired 和 Value 处理流程是一样的。

6.3 @Qualifier

@Qualifier("要被注入的bean的名字") 要结合 @Autowired。

6.4 @Resource

JDK提供的注解,属于Java依赖注入规范(JSR-250)的一部分。目前只能使用name注入,无法根据类型注入

Resource属性注入流程:

  1. 当启动spring容器的时候,spring容器加载了配置文件
  2. 在spring配置文件中,只要遇到bean的配置,就会为该bean创建对象
  3. 在纳入spring容器的范围内查找所有的bean,看哪些bean的属性或者方法上加有@Resource
  4. 找到@Resource注解以后,判断该注解name的属性是否为""(name没有写) 如果没有写name属性,则会让属性的名称的值和spring中ID的值做匹配,如果匹配成功则赋值 如果匹配不成功,则会按照类型进行匹配,如果匹配不成功,则报错 如果有name属性,则会按照name属性的值和spring的bean中ID进行匹配,匹配成功,则赋值,不成功则报错

AOP注解

mvc 注解


文章信息

张雪随笔
master
Homepage
Issues
Download

This Software is under the terms of The Unlicense.