SpringBoot关于Bean的自动创建--Contdition
Contdition
只有在pom文件中导入了相关的坐标,该bean才会纳入到Spring的IOC容器中,示例:
只有在pom文件中导入了redis坐标,才能拿到redisTemplate
org.springframework.boot
spring-boot-starter-data-redis
获取redis
@SpringBootApplication
public class HelloWorld {
public static void main(String[] args) {
//Springboot的主启动类的run方法会返回一个对象
ConfigurableApplicationContext run = SpringApplication.run(HelloWorld.class, args);
//使用该对象的getBean属性获取对应的bean
Object redisTemplate = run.getBean("redisTemplate");
System.out.print(redisTemplate);
}
}
@Conditional 注解的应用场景