这些是springboot特有的,常见的条件依赖注解有:
@ConditionalOnBean,仅在当前上下文中存在某个bean时,才会实例化这个Bean。 @ConditionalOnClass,某个class位于类路径上,才会实例化这个Bean。 @ConditionalOnExpression,当表达式为true的时候,才会实例化这个Bean。 @ConditionalOnMissingBean,仅在当前上下文中不存在某个bean时,才会实例化这个Bean。 @ConditionalOnMissingClass,某个class在类路径上不存在的时候,才会实例化这个Bean。 @ConditionalOnNotWebApplication,不是web应用时才会实例化这个Bean。 @AutoConfigureAfter,在某个bean完成自动配置后实例化这个bean。 @AutoConfigureBefore,在某个bean完成自动配置前实例化这个bean。 @ConfigurationProperties(prefix = "spring.datasource") 注入yml/properties配置文件属性 @ConditionalOnProperty(prefix="spring.datasource", name="enabled", havingValue="true") 控制配置开关的 @SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。 @Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。 这个注解类标识这个类可以使用Spring IoC容器作为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为 在Spring应用程序上下文中的bean。 @EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。 @ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller。