Please don't do it like this... If you want your application to stop at a certain time this is the way to go!!! Each time you create a new application context all the xml is loaded, parsed again etc. You really don't want to do that, also WHY do you need it?
You are in a web application let spring do it for you, use the ContextLoaderListener to load those xml files. If you really need a bean like this register is in your ApplicationContext and make it ApplicationContextAware or BeanFactoryAware!
Code:
public class SpringLocateBean implements BeanFactoryAware {
private static BeanFactory factory;
public void setBeanFactory(BeanFactory beanFactory) {
this.factory=beanFactory;
}
public static Object getBean(String nameBean) {
return factory.getBean(nameBean);
}
}
one of your xml files.
Code:
<bean class="SpringLocateBean" />
However you should really strive to not using such a class, it beats the point of doing DI and using spring to manage/wire everything for you.
you should have something like this in your web.xml
Code:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/*applicationContextBean.xml
classpath:/*applicationContextHibernate.xml
classpath:/*MiAplicacion-servlet.xml
classpath:/*applicationContextMail.xml"
</param-value>
</context-param>
<!-- ContextLoaderListener must be the first one so that the ApplicationContext is available -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>elquetengoaquicolgao.listener.SessionListener</listener-class>
</listener>
Your session listener should access the previously loaded ApplicationContext (as I already mentioned) with the WebApplicationContextUtils.
Code:
HttpSession session = //retrieve the session from event
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
ServicioMail mailer = (ServicioMail ) ctx.getBean("mail");