SpringBoot 經常使用註解

@SpringBootApplication 

這個配置等同於:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三個配置。java

@EnableAutoConfiguration

Spring Boot自動配置(auto-configuration),能夠將@EnableAutoConfiguration或者@SpringBootApplication註解添加到一個@Configuration類上來選擇自動配置。緩存

若是發現應用了你不想要的特定自動配置類,可使用@EnableAutoConfiguration註解的排除屬性來禁用它們。app

@Configuration

至關於傳統的xml配置文件,若是有些第三方庫須要用到xml文件,建議仍然經過@Configuration類做爲項目的配置主類,可使用@ImportResource註解加載xml配置文件。框架

@ComponentScan

@ComponentScan 註解對應XML配置形式中的 <context:component-scan> 元素,表示啓用組件掃描,Spring 會自動掃描全部經過註解配置的 bean,而後將其註冊到 IOC 容器中。異步

能夠經過 basePackages 等屬性來指定 @ComponentScan 自動掃描的範圍,若是不指定,默認從聲明 @ComponentScan 所在類的 package 進行掃描。ide

正由於如此,SpringBoot 的啓動類都默認在 src/main/java 下。post

@ImportResource

用來加載xml配置文件。spa

@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經過條件來選擇須要導入的配置類,code

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()方法。

相關文章
相關標籤/搜索