1.@Configuration注解
作用:标记此类为一个配置类,类似于bean.xml配置文件
@Bean注解:给容器添加组件,一方法名作为组件id,返回类型为组件类型,返回值为组件在容器中的实例
@Configuration
public class TestConfig {
@Bean
public TestReactor MyHandler(){
return new TestReactor();
}
}
①.配置了里面使用@bean标注在方法上给容器注册组件,默认也是单实例;
②.配置类本身也是组件;
③.注解上proxyBeanMethods属性默认为true,代理bean的方法。SpringBoot总会检查这个组件是否在容器中有,保持单实例;
外部无论对配置类中这个方法调用多少次获取的都是之前注册容器中的单实例对象;
这个属性用来接解决组件依赖问题
当这个依赖不用的时候,可以设为false,可以提高springboot的启动速度。
2.@Import
里面是一个Class数组
作用:给容器中自动创建出这多个类型的组件
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
Class<?>[] value();
}
默认组件的名字为全类名
3.@Condition
作用:条件装配,满足Condition条件的才会当做组件注入
主启动类run有个containsBean方法,可以查看容器中是否包含这个组件
4.@ImportResource
作用,导入beans.xml文件,使用xml方法导入组件
5.@ConfigurationProperties
作用:将properties的配置绑定到javaBean里面
属性:prefix 配置前缀
只有在容器中,再会有sSpringboot提供的功能
感觉可以作为@Value的升级版,直接配置指定的前缀来替代@Vlue
6.@EnableConfigurationProperties
作用:1.开始属性配置功能 2.把组件注入到容器中
等效于@component+@ConfigurationProperties
7.自动配置原理
@SpringBootApplication等同于
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {