SpringBoot 常用注解

@SpringBootApplication 

这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。

@EnableAutoConfiguration

Spring Boot自动配置(auto-configuration),可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。

如果发现应用了你不想要的特定自动配置类,可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。

@Configuration

相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类,可以使用@ImportResource注解加载xml配置文件。

@ComponentScan

@ComponentScan 注解对应XML配置形式中的 <context:component-scan> 元素,表示启用组件扫描,Spring 会自动扫描所有通过注解配置的 bean,然后将其注册到 IOC 容器中。

可以通过 basePackages 等属性来指定 @ComponentScan 自动扫描的范围,如果不指定,默认从声明 @ComponentScan 所在类的 package 进行扫描。

正因为如此,SpringBoot 的启动类都默认在 src/main/java 下。

@ImportResource

用来加载xml配置文件。

@Import 

第一类:直接导入配置类

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({SchedulingConfiguration.class})
@Documented
public @interface EnableScheduling {
}

直接导入配置类SchedulingConfiguration,这个类注解了@Configuration,且注册了一个scheduledAnnotationProcessor的Bean

第二类:依据条件选择配置类

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
    Class<? extends Annotation> annotation() default Annotation.class;
    boolean proxyTargetClass() default false;
    AdviceMode mode() default AdviceMode.PROXY;
    int order() default Ordered.LOWEST_PRECEDENCE;

}

AsyncConfigurationSelector通过条件来选择需要导入的配置类,

AsyncConfigurationSelector的根接口为ImportSelector,这个接口需要重写selectImports方法,在此方法内进行事先条件判断。

若adviceMode为PORXY,则返回ProxyAsyncConfiguration这个配置类。

若activeMode为ASPECTJ,则返回AspectJAsyncConfiguration配置类。

第三类:动态注册Bean

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
    boolean proxyTargetClass() default false;
}

AspectJAutoProxyRegistrar 实现了ImportBeanDefinitionRegistrar接口,ImportBeanDefinitionRegistrar的作用是在运行时自动添加Bean到已有的配置类,通过重写方法:

@Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry)

其中,AnnotationMetadata参数用来获得当前配置类上的注解;

BeanDefinittionRegistry参数用来注册Bean。

@EnableAspectJAutoProxy

@EnableAspectJAutoProxy注解 激活Aspect自动代理

<aop:aspectj-autoproxy/>

开启对AspectJ自动代理的支持。

@EnableAsync

@EnableAsync注解开启异步方法的支持。

@EnableScheduling

@EnableScheduling注解开启计划任务的支持。

@EnableWebMVC

@EnableWebMVC注解用来开启Web MVC的配置支持。

@EnableConfigurationProperties

@EnableConfigurationProperties 注解表示对 @ConfigurationProperties 的内嵌支持。

默认会将对应 Properties Class 作为 bean 注入的 IOC 容器中,即在相应的 Properties 类上不用加 @Component 注解。

@EnableJpaRepositories

@EnableJpaRepositories注解开启对Spring Data JPA Repostory的支持。

Spring Data JPA 框架,主要针对的就是 Spring 唯一没有简化到的业务逻辑代码,至此,开发者连仅剩的实现持久层业务逻辑的工作都省了,唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成。

@EnableTransactionManagement

@EnableTransactionManagement注解开启注解式事务的支持。

注解@EnableTransactionManagement通知Spring,@Transactional注解的类被事务的切面包围。这样@Transactional就可以使用了。

@EnableCaching

@EnableCaching注解开启注解式的缓存支持

@Conditional

@Conditional 注解表示在满足某种条件后才初始化一个 bean 或者启用某些配置。

它一般用在由 @Component、@Service、@Configuration 等注解标识的类上面,或者由 @Bean 标记的方法上。

如果一个 @Configuration 类标记了 @Conditional,则该类中所有标识了 @Bean 的方法和 @Import 注解导入的相关类将遵从这些条件。

在 Spring 里可以很方便的编写你自己的条件类,所要做的就是实现 Condition 接口,并覆盖它的 matches()方法。

 

posted @ 2018-10-16 11:31  wade&luffy  阅读(721)  评论(0编辑  收藏  举报