我有以下类来实例化正确的经理,所以我可以使用一个用于我的暂存,发布和一个用于测试:
public class DAOFactory {
private static final Logger LOG = LoggerFactory.getLogger(DAOFactory.class);
@Autowired
private static ApplicationContext applicationContext;
private final static String dataSourceName = "remoteStagingDataSource";
// TODO : Change variable dataSourceName to the correct DataSource if you deploy on staging/release web server
// -- remoteStagingDataSource -- for deploying on local tomcat server
// -- stagingDataSource -- for deploying on staging server + uncomment the correct bean in src/main/resources/applicationContext.xml
// -- releaseDataSource -- for deploying on release server + uncomment the correct bean in src/main/resources/applicationContext.xml
public static DAOManager createMySQLDAOManager() {
LOG.info("Setting up datasource to " + dataSourceName);
DataSource dataSource = setupDataSource(dataSourceName);
return new DAOManager(dataSource);
}
public static DAOManager createHSQLDBDAOManager() {
LOG.info("Setting up datasource to in-memory HSQLDB");
DataSource dataSource = setupDataSource("hsqldbDataSource");
return new DAOManager(dataSource);
}
private static DataSource setupDataSource(final String dataSourceName) {
applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
return (DataSource)applicationContext.getBean(dataSourceName);
}
}
现在我想将applicationContext
分开用于测试目的(DBUnit
测试我的DAO
所以两个xml文件:
知道我怎么能做到这一点吗?
答案 0 :(得分:1)
Spring提供Profiles,它允许在运行时有条件地选择bean实例,具体取决于应用程序所在的环境。
简而言之,您使用beans
标记将环境相关的bean包装起来,将profile
定义为属性:
<beans profile="DEV">
<bean id="someBean" class="x.y.ClassA"/>
<beans>
<beans profile="QA">
<bean id="someBean" class="x.y.ClassB"/>
<beans>
然后设置系统属性以确定要运行的环境:
-Dspring.profiles.active="QA"
答案 1 :(得分:0)
好的我觉得我明白了:
@Autowired
private static GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
private static final String DATASOURCE = "dataSource";
private static final String ACTIVE_PROFILE = "remoteStaging"; // "staging" -- "release"
// TODO : Change variable activeProfile to the correct active profile if you deploy on staging/release web server
// -- remoteStaging -- for deploying on local tomcat server
// -- staging -- for deploying on staging server
// -- release -- for deploying on release server
public static DAOManager createMySQLDAOManager() {
LOG.info("Setting up datasource to " + ACTIVE_PROFILE);
ConfigurableEnvironment env = (ConfigurableEnvironment)applicationContext.getEnvironment();
env.setActiveProfiles(ACTIVE_PROFILE);
DataSource dataSource = setupDataSource(DATASOURCE);
return new DAOManager(dataSource);
}
public static DAOManager createHSQLDBDAOManager() {
LOG.info("Setting up datasource to in-memory HSQLDB");
ConfigurableEnvironment env = (ConfigurableEnvironment)applicationContext.getEnvironment();
env.setActiveProfiles("test");
DataSource dataSource = setupDataSource("hsqldbDataSource");
return new DAOManager(dataSource);
}
private static DataSource setupDataSource(final String dataSourceName) {
applicationContext.load("classpath:/applicationContext.xml");
applicationContext.refresh();
return applicationContext.getBean(dataSourceName, DataSource.class);
}
现在,如果我在我的发布网络服务器上部署,我该如何指定设置正确的活动配置文件?只是使用maven? 德尔>
mvn Dvop.env=release tomcat:redeploy -Dspring.profiles.active="release"
编辑:傻我,我总是需要指定dataSourceName。有没有办法让应用程序现在应该连接到哪个数据源,而不是手动手动更改?