springboot 啟動類中關鍵注解作用解析


 

  1. 一、Springboot:請求入口
  2.  
     
  3.  
    @SpringBootApplication
  4.  
    @EnableAspectJAutoProxy
  5.  
    @EnableScheduling
  6.  
    @EnableTransactionManagement
  7.  
    public class Application {
  8.  
     
  9.  
    public static void main(String[] args) {
  10.  
     
  11.  
    SpringApplication.run(Application.class, args);
  12.  
     
  13.  
    }
  14.  
    }

1、@SpringBootApplication注解

 

  1.  
    @Target(ElementType.TYPE)
  2.  
    @Retention(RetentionPolicy.RUNTIME)
  3.  
    @Documented
  4.  
    @Inherited
  5.  
    @SpringBootConfiguration
  6.  
    @EnableAutoConfiguration
  7.  
    @ComponentScan(excludeFilters = {
  8.  
    @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  9.  
    @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  10.  
    public @interface SpringBootApplication {
  11.  
    ……
  12.  
    }

復合注解:包含@EnableAutoConfiguration@ComponentScan@SpringBootConfiguration

@EnableAutoConfiguration:也是復合注解、借助@Import將所有符合自動配置條件的bean定義加載到Spring ioc 中。幫助springboot應用將所有符合條件的@configuration配置都加載到當前spring ioc

@Import注解的使用。這個注解幫助我們將多個配置文件(可能是按功能分,或是按業務分)導入到單個主配置中,以避免將所有配置寫在一個配置中

b@ComponentScan:主要作用掃描當前包及其子包下被@Component,@Controller@Service@Repository注解標記的類並納入到spring容器中進行管理。是以前的<context:component-scan>(以前使用在xml中使用的標簽,用來掃描包配置的平行支持)。可通過@ComponentScan 的basepackage等屬性來指定掃描范圍。(@SpringBootApplication(scanBasePackages = "com.ucredit"如果不指定默認spring框架實現,從聲明@ComponentScan所在的類的package進行掃描。所以springboot的啟動類最好放在root package下。

c@SpringBootConfiguration:繼承自@Configuration,二者功能也一致,標注當前類是配置類,並會將當前類內聲明的一個或多個以@Bean注解標記的方法的實例納入到spring容器中,並且實例名就是方法名。@SpringBootConfiguration注解類相當於spring配置beanxml文件。

d@Configuration

Spring是給予IOC的,在4.0之前的版本,通常都是程序依賴上下文xml文件來管理bean,盡管有了掃描配置后相對簡單,然而java配置的方式不同於xml,通過注解能夠更簡單。下面我們通過這兩種方式比較下。

  • xml中bean的定義

 

  1.  
    <beans>
  2.  
    <bean id="course" class="demo.Course">
  3.  
    <property name="module" ref="module"/>
  4.  
    </bean>
  5.  
    <bean id="module" class="demo.Module">
  6.  
    <property name="assignment" ref="assignment"/>
  7.  
    </bean>
  8.  
    <bean id="assignment" class="demo.Assignment" />
  9.  
    </beans>
  • 注解配置類

 

 

  1.  
    @Configuration
  2.  
    public class AppContext {
  3.  
    @Bean
  4.  
    public Course course() {
  5.  
    Course course = new Course();
  6.  
    course.setModule( module());
  7.  
    return course;
  8.  
    }
  9.  
    @Bean
  10.  
    public Module module() {
  11.  
    Module module = new Module();
  12.  
    module.setAssignment(assignment());
  13.  
    return module;
  14.  
    }
  15.  
    @Bean
  16.  
    public Assignment assignment() {
  17.  
    return new Assignment();
  18.  
    }
  19.  
    }

@Configuration,該注解配置在類上,告知Spring這個類是一個擁有bean定義和依賴項的配置類。@Bean注釋用於定義Bean,該注解位於實例化bean並設置依賴項的方法上。方法名默認通beanId活默認名稱相同,該方法返回類型是Spring注冊的bean。總體來說就是告訴Spring容器加載這個配置,相對於xml,這個注解就是將*.xml配置進web.xml

e、@Import

 

  1.  
    @Target(ElementType.TYPE)
  2.  
    @Retention(RetentionPolicy.RUNTIME)
  3.  
    @Documented
  4.  
    public @interface Import {
  5.  
     
  6.  
    /**
  7.  
    * {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
  8.  
    * or regular component classes to import.
  9.  
    */
  10.  
    Class<?>[] value();
  11.  
     
  12.  
    }
  • @Import 與xml配置方式下的 作用一樣。支持導入的類型有: 
    一個或多個擁有 @Configuration 注解的配置類
  • ImportSelector 接口的實現類
  • ImportBeanDefinitionRegistrar 的實現類

 1)、如果Import注解中Class為ImportSelector子類,通過invokeAwareMethods(selector)設置aware值,如果類型為  DeferredImportSelector則添加到deferredImportSelectors集合中,待前面的parser.parse(configCandidates)
 方法中processDeferredImportSelectors()處理;如果不是,則執行selectImports方法,將獲取到的結果遞歸調用  processImports,解析selectImports得到的結果

 2)、如果Import注解中Class為ImportBeanDefinitionRegistrar子類,則添加到importBeanDefinitionRegistrars中,注 意該部分的數據在執行完parser.parse(configCandidates)后調用this.reader.loadBeanDefinitions(configClasses)解  析,否則執行配置信息的解析操作。

 

  1.  
    public interface Car {
  2.  
     
  3.  
    public void print();
  4.  
    }
  5.  
    @Component
  6.  
    public class Toyota implements Car {
  7.  
     
  8.  
    @Override
  9.  
    public void print() {
  10.  
    // TODO Auto-generated method stub
  11.  
    System.out.println( "I am Toyota");
  12.  
    }
  13.  
     
  14.  
    }
  15.  
     
  16.  
    @Component
  17.  
    public class Volkswagen implements Car {
  18.  
     
  19.  
    @Override
  20.  
    public void print() {
  21.  
    // TODO Auto-generated method stub
  22.  
    System.out.println( "I am Volkswagen");
  23.  
    }
  24.  
     
  25.  
    }
  26.  
     
  27.  
    @Configuration
  28.  
    public class JavaConfigA {
  29.  
     
  30.  
    @Bean(name="volkswagen")
  31.  
    public Car getVolkswagen(){
  32.  
    return new Volkswagen();
  33.  
    }
  34.  
    }
  35.  
     
  36.  
    @Configuration
  37.  
    public class JavaConfigB {
  38.  
    @Bean(name="toyota")
  39.  
    public Car getToyota(){
  40.  
    return new Toyota();
  41.  
    }
  42.  
    }
  43.  
     
  44.  
    @Configuration
  45.  
    @Import({JavaConfigA.class,JavaConfigB.class})
  46.  
    public class ParentConfig {
  47.  
    //Any other bean definitions
  48.  
    }
  49.  
    public class ContextLoader {
  50.  
     
  51.  
    public static void main (String args[]){
  52.  
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ParentConfig.class);
  53.  
    Car car = (Toyota)context.getBean("toyota");
  54.  
    car.print();
  55.  
    car = (Volkswagen)context.getBean("volkswagen");
  56.  
    car.print();
  57.  
    context.close();
  58.  
    }
  59.  
    }
  60.  
     
  61.  
     



 

2、@EnableAspectJAutoProxy 注解

   表示開啟AOP代理自動配置,如果配@EnableAspectJAutoProxy表示使用cglib進行代理對象的生成;

3、@EnableScheduling

@EnableScheduling是通過@ImportSpring調度框架相關的bean定義都加載到IoC容器。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM