user2824073
user2824073

Reputation: 2485

Where to store ConfigurableApplicationContext in a Java EE application?

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

Answers (2)

Francesco Marchioni
Francesco Marchioni

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

Magic Wand
Magic Wand

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

Related Questions