一、配置类注解二、启动类详解
一、配置类注解@Configuration vs @Component
@Configuration
//配置类 那么以下bean都会被代理 Configuration 对于已经创建的对象就不会再被创建了
//如果不想被代理 那么@Component
这个MainApplication启动类放到根包目录下,这样才能扫描到组件
@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {@org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.context.TypeExcludeFilter.class}), @org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter.class})})
@EnableAutoConfiguration
在原生的Spring frameWork中,组件装配有三个阶段
@import注解:
- Spring 2.5+ @ComponentSpring 3.0+ 使用@Configuration+@BeanSpring 3.1+ @EnableXXX +@import
对于Bean比较少的情况 前两种就行了;
对于一个框架,有大量bean需要注入
新建注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@import({Apple.class, Banana.class})
public @interface EnableFruit {
}
然后在启动类添加相应注解,即可完成注入。
@ComponentScan



