package com.lz.springboot.study.spring;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConditionalOnBeanT {
class A {
}
class B {
private String from;
private String to;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
}
class C {
}
class D {
}
@Bean
public A a() {
return new A();
}
// @Bean
public C c() {
return new C();
}
@Bean
@ConditionalOnBean(value = A.class)
// A必须存在一个bean
@ConditionalOnClass(value = C.class)
// 当类路径下有指定类的条件下
@ConditionalOnExpression("${enabled:true}")
// 当表达式为true的时候,才会实例化一个Bean
@ConditionalOnMissingBean(value = C.class)
@ConditionalOnMissingClass
@ConditionalOnProperty(prefix = "spring.boot.admin.notify.mail", name = "from", havingValue = "xx")
// 配置文件中spring.boot.admin.notify.mail.from必须存在
@ConditionalOnNotWebApplication
// (不是web应用)
@ConfigurationProperties("spring.boot.admin.notify.mail")
public B b() {
return new B();
}
}
对ConditionalOn* 的理解
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Spring Boot Rock'n'Roll - 一个架构士的思考与沉淀https://afoo.me/post...