Results 1 to 5 of 5

Thread: How to inject a bean property on a session listener

  1. #1
    Join Date
    Jun 2009
    Posts
    3

    Default How to inject a bean property on a session listener

    First, excuse me for my poor english. I know i speak the worst english ever.

    This my problem:

    I need to send an email with session atributes when the session expires.

    I've written a listener which implements HttpSessionAttributeListener. It works perfectly and i can get the attributes in session that i need. The listener is configured into the web.xml like this:

    Code:
           <listener>
                 <listener-class>
                        elquetengoaquicolgao.listener.SessionListener
                 </listener-class>
           </listener>


    ¿ Well, what is the problem? I need to use a bean referenced in the application context xml. This is the bean:

    Code:
          <bean id="mail"
                class="es.indra.vf.frontalAdslTOL.mail.impl.ServicioMailImpl">
                <property name="mailSender" ref="mailSender" />
                <property name="velocityEngine" ref="velocityEngine" />
          </bean>

    The listener fail trying to send emails because it starts without being invoke for any instance.


    I have tried this:
    Code:
          <bean id="SessionAttributeListener"
                class="elquetengoaquicolgao.listener.SessionAttributeListener">
                <property name="mail" ref="mail" />
          </bean>
    but there isn't any method that can invoke the listener with the bean.

    Anyone could help me?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You don't. Spring can and will only inject into beans it knows about. You will have to retrieve it from the context yourself. Make sure your listener is instantiated after the ContextLoaderListener then use the WebApplicationContextUtils to get access to the loaded ApplicationContext and retrieve the bean you need (your mail bean).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2009
    Posts
    3

    Default

    Thanks Mart, you put me in the correct path to solve it, but...

    I've created a class in order to instanciate the bean. My class is like this:



    Code:
    public class SpringLocateBean {
        private static final String[] LOCATIONS = {"applicationContextBean.xml",
            "applicationContextHibernate.xml",
            "MiAplicacion-servlet.xml",
            "applicationContextMail.xml"};
    
        public static Object getBean(String nameBean) {
    
            Object objBean = null;
            try {
                 ApplicationContext appCon =    new ClassPathXmlApplicationContext(LOCATIONS);
                objBean = appCon.getBean("mail");
            } catch (Exception e) {
                //Exception
            }
            return objBean;
        }
    
    }

    I'm afraid I can't load the XML correctly. I get this exception: "No bean named 'mail' is defined". I think i'm not getting the App Context correctly. How can i get it correctly?


    I also tried writing the classpath before the xml like this, but Application Context get the same value.

    Code:
    LOCATIONS = {"classpath:/*applicationContextBean.xml",
            "classpath:/*applicationContextHibernate.xml",
            "classpath:/*MiAplicacion-servlet.xml",
            "classpath:/*applicationContextMail.xml"};

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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");
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jun 2009
    Posts
    3

    Default

    My problem was i didn't know how to get the Servlet Context. Now it seems to be very easy.

    Getting the Servlet Context from the event works perfectly!!!

    heel hartelijk bedankt!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •