条件注入 在我们需要切换工作环境的时候,就会用到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