t0tec
t0tec

Reputation: 340

Spring separate applicationContext's to purpose

I have the following class to instantiate the right manager so I can use one for my staging, release and one for testing:

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);
}
}

Now I want to separate the applicationContext for testing purposes (DBUnit testing my DAO's)

So two xml files:

Any idea how I can accomplish this?

Upvotes: 1

Views: 182

Answers (2)

t0tec
t0tec

Reputation: 340

Ok I think I got it:

@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);
}

Now if I deploy on my release webserver how can I specify to set the right active profile? Just use maven?

mvn Dvop.env=release tomcat:redeploy -Dspring.profiles.active="release"

EDIT: silly me, I always need to specify the dataSourceName. Is there a way to let the application now to which datasource I should connect than rather always change this manually by hand?

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

Spring provides Profiles which allows bean instances to be conditionally chosen at runtime depending on the environment the application resides.

In a nutshell, you wrap environment dependent beans with a beans tag that defines the profile as an attribute:

<beans profile="DEV">
    <bean id="someBean" class="x.y.ClassA"/>
<beans>
<beans profile="QA">
    <bean id="someBean" class="x.y.ClassB"/>
<beans>

Then set a system property to determine which environment to run:

-Dspring.profiles.active="QA"

Upvotes: 1

Related Questions