对ConditionalOn* 的理解

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* 的理解)