条件注入
在我们需要切换工作环境的时候,就会用到Profile:
简单的介绍一下代码:
1、XML配置:
<beans profile="dev">
<bean class="com.pojo.User" id="user">
<property name="name" value="zsl" />
</bean>
</beans>
<beans profile="pro">
<bean class="com.pojo.User" id="user">
<property name="name" value="zsl" />
</bean>
</beans>
测试类:
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
applicationContext.getEnvironment().setActiveProfiles("dev");
applicationContext.refresh();
User bean = (User) applicationContext.getBean("user");
System.out.println(bean);
2、Java配置类:
@Configuration
@ComponentScan(basePackages="com.controller,com.service,com.dao,com.pojo")
public class App {
@Bean("user")
@Profile("pro")
public User proUser() {
// TODO Auto-generated method stub
User user = new User();
return user;
}
@Bean("user")
@Profile("dev")
public User devUser() {
// TODO Auto-generated method stub
User user = new User();
return user;
}
}
测试:
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.getEnvironment().setActiveProfiles("dev");
applicationContext.register(App.class);
applicationContext.refresh();
User bean = (User) applicationContext.getBean("user");
bean.setName("卢本伟牛逼");
System.out.println(bean);
二、条件注解Condition
Profile是条件注解的一种特殊形式,条件注解更加灵活,用户可以根据各种不同的条件使用不同的Bean。
SpringBoot中提供了许多自动化的配置,例如数据库配置,SpringBoot使用条件注解提前配置好许多常用的类,使用条件注解,在某一个条件满足时,这些配置就会生效。
Java配置的方式:
Java配置类:
@Configuration
public class App {
@Bean("show")
@Conditional(ConditionA.class)//
public ShowA proUser() {
// TODO Auto-generated method stub
ShowA showA = new ShowA();
return showA;
}
@Bean("show")
@Conditional(ConditionB.class)//
public ShowB devUser() {
// TODO Auto-generated method stub
ShowB showB = new ShowB();
return showB;
}
}
定义接口:Ishow
public interface IShow {
public String show();
}
ShowA和ShowB
public class ShowA implements IShow {
@Override
public String show() {
// TODO Auto-generated method stub
return "ShowA";
}
}
public class ShowB implements IShow{
@Override
public String show() {
// TODO Auto-generated method stub
return "ShowB";
}
}
条件类需要实现接口Condition:
/**
*返回true则选择该条件
*
**/
public class ConditionA implements Condition{
@Override
public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
// TODO Auto-generated method stub
String[] profiles = arg0.getEnvironment().getActiveProfiles();
for (String string : profiles) {
if (string.contains("A")) {
return true;
}
}
return false;
}
}
/**
*返回true则选择该条件
*
**/
public class ConditionB implements Condition{
@Override
public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
// TODO Auto-generated method stub
String[] profiles = arg0.getEnvironment().getActiveProfiles();
for (String string : profiles) {
if (string.contains("B")) {
return true;
}
}
return false;
}
}
测试类:
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext();
applicationContext.getEnvironment().setActiveProfiles("A");
applicationContext.register(App.class);
applicationContext.refresh();
IShow bean = (IShow) applicationContext.getBean("show");
System.out.println(bean.show());
原文:https://www.cnblogs.com/zhangsonglin/p/10920127.html