Hi,
After my last post concerning the customization of the Spring Context ‘XmlWebApplicationContext’, I will present you a simple solution to access to Spring context in all places of an application.
For example, in a Spring MVC application with ORM layer, commonly, we have the following stack:
SpringWebMVCCtrler
==calls==> ServiceDelegate
==calls==> ServiceImpl
==calls==> HibernateDaoImpl
..so, it is necessary to have access to the context in all places or layers of an application.
First, we will create a classic servlet context listener SpringContextLoader which implements ServletContextListener. During the initialization context, this listener will get a parameter ‘CONFIG_SPRING_FILE’ configured in web.xml, and it will call a singleton named ApplicationContextContainer use this parameter.
01 | public class SpringContextLoader implements ServletContextListener { |
11 | * Method contextDestroyed |
13 | * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) |
15 | public void contextDestroyed(ServletContextEvent arg0) { |
19 | * Method contextInitialized |
21 | * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) |
23 | public void contextInitialized(ServletContextEvent ctx) { |
24 | String springFileName = ctx.getServletContext().getInitParameter( "CONFIG_SPRING_FILE" ); |
25 | ApplicationContextContainer.getInstance(springFileName, ctx.getServletContext()); |
Then, we will create the singleton ApplicationContextContainer which gives an access to a container containing the Spring context with the XmlWebApplicationContext type.
Once, the context loaded, the next calls to the ‘getInstance()’ or ‘getInstance(String springContextFile, ServletContext servletContext)’ will return the same instance of context.
01 | public class ApplicationContextContainer { |
04 | private static Log log = LogFactory.getLog(ApplicationContextContainer. class ); |
08 | private final static String SPRING_BUSINESS_CONFIG_XML = "/WEB-INF/spring-root-applicationContext-cfg.xml" ; |
15 | private static ApplicationContextContainer instance = null ; |
18 | * Contains the spring configuration. |
20 | private XmlWebApplicationContext ctx = null ; |
25 | private ApplicationContextContainer() {} |
31 | public static synchronized ApplicationContextContainer getInstance() { |
32 | return getInstance(SPRING_BUSINESS_CONFIG_XML, null ); |
34 | public static synchronized ApplicationContextContainer getInstance(String springContextFile, ServletContext servletContext) { |
35 | if ( null == instance) { |
36 | instance = new ApplicationContextContainer(); |
37 | instance.ctx = new XmlWebApplicationContext(); |
38 | instance.ctx.setConfigLocation(springContextFile); |
39 | instance.ctx.setServletContext(servletContext); |
40 | instance.ctx.refresh(); |
46 | * Retrieve the spring bean corresponding to the given key. |
50 | public static Object getBean(String key) { |
51 | return getInstance().ctx.getBean(key); |
54 | public XmlWebApplicationContext getContext() { |
More, we need a enumeration or a class containing the names of beans configured in the Spring Context:
1 | public class DataConstHelper { |
2 | public static final String USER_DAO_HIBERNATE_IMPL = "UserDaoHibernateImpl" ; |
3 | public static final String BOOK_DAO_HIBERNATE_IMPL = "BookDaoHibernateImpl" ; |
4 | public static final String USER_SERVICE_IMPL = "UserServiceImpl" ; |
5 | public static final String BOOK_SERVICE_IMPL = "BookServiceImpl" ; |
6 | public static final String USER_VALIDATOR = "userValidator" ; |
A more important thing, is the configuration of these beans in the web.xml deployment descriptor (DD) file:
05 | < listener-class >com.ho.persistence.hibernate.spring.ex1.utils.SpringContextLoader</ listener-class > |
08 | < param-name >CONFIG_SPRING_FILE</ param-name > |
09 | < param-value >/WEB-INF/spring-root-applicationContext-cfg.xml</ param-value > |
A example of Spring context loading at startup ís the following configuration in DD file which we don’t use in our example:
Related
Hi,
It took two days to find a decent working example like this.Thank you
Thanks and Regards
Kavya M