企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 1. @EnableConfigurationProperties、ConfigurationProperties > 1. @ConfigurationProperties注解主要用来把properties配置文件转化为bean,并交由Spring IOC容器管理 > 2. @EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。 > 3. 如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的 > ### 1.1 @ConfigurationProperties定义配置转换bean 1. ApplicationContextAware:获取Spring资源 ~~~ /** * Created by dailin on 2018/5/2. * 继承ApplicationContextAware,重写setApplicationContext方法, * Spring自动给这个对象注入ApplicationContext,这样就可以操控Spring资源了 */ @Component public class ContextUtil implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public ApplicationContext getApplicationContext() { return applicationContext; } } ~~~ @ConfigurationProperties(prefix = "spring.rocketmq") prefix = "spring.rocketmq" :配置的前缀 ~~~ import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @Data @ConfigurationProperties(prefix = "spring.rocketmq") public class MQProperties { /** * config name server address */ private String nameServerAddress; /** * config producer group , default to DPG+RANDOM UUID like DPG-fads-3143-123d-1111 */ private String producerGroup; /** * config send message timeout */ private Integer sendMsgTimeout = 3000; /** * switch of trace message consumer: send message consumer info to topic: rmq_sys_TRACE_DATA */ private Boolean traceEnabled = Boolean.TRUE; /** * switch of send message with vip channel */ private Boolean vipChannelEnabled = Boolean.TRUE; } ~~~ ### 1.2 @EnableConfigurationProperties(MQProperties.class) > 1. @EnableConfigurationProperties注解是用来开启对@ConfigurationProperties注解配置Bean的支持。也就是@EnableConfigurationProperties注解告诉Spring Boot 使能支持@ConfigurationProperties > 2. 使得被@ConfigurationProperties标注的MQProperties.class类生成对象,也可以不指定MQProperties.class ~~~ @Configuration @ConditionalOnBean(annotation = EnableMQConfiguration.class) @AutoConfigureAfter({AbstractMQProducer.class, AbstractMQPushConsumer.class}) @EnableConfigurationProperties(MQProperties.class) // 使配置注解生效,以便下边可以注入到这个类对象中 public class MQBaseAutoConfiguration implements ApplicationContextAware { protected MQProperties mqProperties; @Autowired public void setMqProperties(MQProperties mqProperties) { this.mqProperties = mqProperties; } protected ConfigurableApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = (ConfigurableApplicationContext) applicationContext; } } ~~~ ## 2. @ComponentScan @ComponentScan(basePackages={"net.aexit"}) pringBoot在写启动类的时候如果不使用@ComponentScan指明对象扫描范围,默认指扫描当前启动类所在的包里的对象,如果当前启动类没有包,则在启动时会报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package错误。 因为启动类不能直接放在main/java文件夹下,必须要建一个包把它放进去或者使用@ComponentScan指明要扫描的 例如,如果你有个类用@Controller注解标识了,那么,如果不加上@ComponentScan,自动扫描该controller,那么该Controller就不会被spring扫描到,更不会装入spring容器中,因此你配置的这个Controller也没有意义。 ## 3. @Configuration @Configuration相当于以前的beans标签,@Bean相当于xml的bean标签 > 1. 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件。 > 2. 被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。 > 3. 被@Configuration标注的类会被实例成对象 1. MyConfig.class ~~~ @Configuration public class MyConfig { public MyConfig(){ System.out.println("初始化MyConfig..."); } } ~~~ 2. CommonFig.class ~~~ @Configuration public class CommonFig { public CommonFig(){ System.out.println("CommonFig 初始化..."); } } ~~~ 两个配置被实例化了 ![](images/screenshot_1525255735892.png ## 4.根据不同的条件创建bean ### 4.1 @ConditionalOnBean 1. @ConditionalOnBean起到创建bean的过滤功能,不能创建bean 2. 当容器里有指定Bean的条件下,再依靠其他注解来创建bean ~~~ @ConditionalOnBean(MyConfig.class) public class CommonFig { public CommonFig(){ System.out.println("CommonFig 初始化..."); } } ~~~ ~~~ @RequestMapping(value = "/get", method = RequestMethod.GET) @ResponseBody public CommonFig get(Model model) { CommonFig bean = contextUtil.getApplicationContext().getBean(CommonFig.class); System.out.println(bean); return bean; } ~~~ 此时没有MyConfig对象,所以不会创建CommonFig对象,所以调用CommonFig对象的方法报错 ~~~ org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.aixin.lovetocar.tuna.springboot.config.CommonFig' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1092) ~~~ 此时创建对象 ~~~ @Configuration public class MyConfig { private String configName = "tuna"; public MyConfig(){ System.out.println("初始化MyConfig..."); } ~~~ 还是报错(还是没有生成CommonFig对象) ~~~ org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.aixin.lovetocar.tuna.springboot.config.CommonFig' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) ~~~ 修改CommonFig,增加@Configuration注解 ~~~ @Configuration @ConditionalOnBean(MyConfig.class) public class CommonFig { public CommonFig(){ System.out.println("CommonFig 初始化..."); } } ~~~ 成功! ![](https://box.kancloud.cn/2d8792e8bb679260f3e91f723bc20fc7_1091x48.png) **说明@ConditionalOnBean注解起到过滤的作用,不会生成bean;而@Configuration可以生成bean** #### annotation //当有这个注解所标识的bean生成时,才创建bean ~~~ @Configuration @ConditionalOnBean(annotation = TestAnnotation.class) public class CommonFig2 { public CommonFig2(){ System.out.println("CommonFig2 初始化..."); } } ~~~ 报错,没有这个bean ~~~ org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.aixin.lovetocar.tuna.springboot.config.CommonFig2' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) ~~~ **添加这个依赖的注解** ~~~ @TestAnnotation public class AnnotationTest { } ~~~ 还是报错么有创建对象 修改AnnotationTest,让他成为bean ~~~ @TestAnnotation @Component public class AnnotationTest { } ~~~ 成功了 ~~~ com.aixin.lovetocar.tuna.springboot.config.CommonFig2$$EnhancerBySpringCGLIB$$a5e55e1e@4c4643b4 ~~~ ![](https://box.kancloud.cn/d287e120ce0256ceebc338c4fa7ac175_1180x168.png) 除了自己自定义Condition之外,Spring还提供了很多Condition给我们用 @ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean) @ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean) @ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean) @ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean) @ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean) @ConditionalOnNotWebApplication(不是web应用) ## 5. @SpringBootApplication 包扫描规则 扫描 与@SpringBootApplication所在包同级及其子集下的所有包。