spring profile机制测试

新建spring普通maven项目:
------------------------
方式一使用ClassPathXmlApplicationContext
打开idea-->new-->project-->选择maven-->选择maven模板quickstart-->添加spring依赖spring-core、spring-context、spring-beans、spring-test及junit-->创建bean-->创建spring xml配置文件,配置profile-->在main方法创建ClassPathXmlApplicationContext并传入参数为配置文件地址
------------------------
方式二使用AnnotationConfigApplicationContext
打开idea-->new-->project-->选择maven-->选择maven模板quickstart-->添加spring依赖spring-core、spring-context、spring-beans、spring-test及junit-->创建bean-->创建java配置类,放在最外层包,方法上添加Profile注解-->在main方法创建AnnotationConfigApplicationContext并传入最外层包
------------------------
设置系统环境变量:
spring.profiles.default dev
------------------------
运行test包里的方法

------------------------

代码示例:https://github.com/ChangMike/spring-profile-test

原文地址:https://www.cnblogs.com/Mike_Chang/p/12723772.html