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