Reputation: 2485
I'm planning to port a Spring application to a Java EE environment. Just I'd like to use JSF as presentation layer (instead of Spring MVC). My question is: what is a safe place in a Java EE application where I can store the ConfigurableApplicationContext, so that I don't need to repeat this multiple times:
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Would it be safe storing it in the Application's context of a Web application or maybe in a SingletonEJB ? Thanks
Upvotes: 0
Views: 961
Reputation: 4328
If you are using CDI, then you could create a simple producer for creating one application instance of the ConfigurableApplicationContext. See the following example:
@ApplicationScoped
public class SpringProducer {
@Produces
public ConfigurableApplicationContext create() {
return new ClassPathXmlApplicationContext("applicationContext.xml");
}
public void close(@Disposes final ConfigurableApplicationContext ctx) {
ctx.close();
}
}
For a full example see this Spring tutorial on WildFly
Upvotes: 1
Reputation: 1760
Both options are OK: for application context you need to create a ServletContextListener and in contextInitialized() method create Spring application context. For Singleton EJB, create initialization method and add @PostConstruct method.
Upvotes: 1