提交 f7a1c9f8 编写于 作者: 武汉红喜's avatar 武汉红喜

EnvironmentProcessor

上级 cf87d033
......@@ -2,9 +2,6 @@ package org.hongxi.whatsmars.boot.sample.bean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
/**
* Created by shenhongxi on 2020/6/8.
......@@ -15,26 +12,4 @@ public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Order(1)
@ConditionalOnMissingBean
@Bean
public DemoBean demoBean() {
return new DemoBean();
}
@Bean
public DemoBean demoBean2() {
return new DemoBean();
}
@Bean
public OrderedDemoBean orderedDemoBean() {
return new OrderedDemoBean();
}
@Bean
public DemoBeanPostProcessor demoBeanPostProcessor() {
return new DemoBeanPostProcessor();
}
}
package org.hongxi.whatsmars.boot.sample.bean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
/**
* Created by shenhongxi on 2020/6/22.
*/
@Configuration
public class ApplicationConfiguration {
@Order(1)
@ConditionalOnMissingBean
@Bean
public DemoBean demoBean() {
return new DemoBean();
}
@Bean
public DemoBean demoBean2() {
return new DemoBean();
}
@Bean
public OrderedDemoBean orderedDemoBean() {
return new OrderedDemoBean();
}
@Bean
public DemoBeanPostProcessor demoBeanPostProcessor() {
return new DemoBeanPostProcessor();
}
@Bean
@Conditional(OrCondition.class)
public ConditionalBean conditionalBean() {
return new ConditionalBean();
}
}
package org.hongxi.whatsmars.boot.sample.bean;
/**
* Created by shenhongxi on 2020/6/22.
*/
public class ConditionalBean {
public ConditionalBean() {
System.out.println("new ConditionalBean");
}
}
package org.hongxi.whatsmars.boot.sample.bean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
public class EnvironmentProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
System.out.println("postProcessEnvironment");
}
}
\ No newline at end of file
package org.hongxi.whatsmars.boot.sample.bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* Created by shenhongxi on 2020/6/22.
*/
public class OrCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
return environment.containsProperty("a") || environment.containsProperty("b");
}
}
org.springframework.boot.env.EnvironmentPostProcessor=\
org.hongxi.whatsmars.boot.sample.bean.EnvironmentProcessor
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册