2016-10-24 12 views
2

セッションリスナークラスで@ServiceアノテーションでアノテーションされたサービスクラスをAutowireする必要があります。セッションを破棄するメソッドでDB操作を行う必要があります。 web.xmlにリスナーを追加したので、私はサービスクラスをautowireできません。これはもはや春に管理されていません。私はアプリケーションコンテキストからサーブレットコンテキストを介してBeanを取得するようないくつかのオプション(回避策)を試しましたが、その方法でBeanを取得していません。続きセッションリスナーでBeanをオートワイヤリングする

は私のクラスである: - たMyService:

@Service 
@Transactional 
public class FxTransactionService{ 
//some autowirings 
public void performDBoperation(Long id) 
{ 
//business logic 
} 
} 

セッションリスナー:

 public class SessionHandler implements HttpSessionListener { 
      private final Logger logger = LoggerFactory.getLogger(this.getClass()); 

      @Autowired 
      private MyService myService; 

      @Override 
      public void sessionCreated(HttpSessionEvent arg0) { 
       System.out.println("Session created"); 
       ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(arg0.getSession() 
        .getServletContext()); 
      System.out.println(Arrays.toString(context.getBeanDefinitionNames())); 
//This gives me empty list 
      } 

      @Override 
      public void sessionDestroyed(HttpSessionEvent arg0) { 
    Long id = (Long) arg0.getSession().getAttribute("Id"); 
       myService.performDBoperation(id); 

      } 

     } 

のweb.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" > 

<web-app> 

    <display-name>Archetype Created Web Application</display-name> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>com.abc.controller.SessionHandler</listener-class> 
    </listener> 

    <listener> 
     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>log4jConfigLocation</param-name> 
     <param-value>/WEB-INF/classes/log4j.properties</param-value> 
    </context-param> 

    <filter> 
     <filter-name>preAuthHeaderAdditionFilter</filter-name> 
     <filter-class>com.abc.filter.PreAuthHeaderAdditionFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>preAuthHeaderAdditionFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <!-- <filter> <filter-name>openEntityManagerInViewFilter</filter-name> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> 
     </filter> <filter-mapping> <filter-name>openEntityManagerInViewFilter</filter-name> 
     <url-pattern>/*</url-pattern> </filter-mapping> --> 


</web-app> 

答えて

2

まず春のリスナーのContextLoaderListenerをインストールします。 独自のリスナーでは、WebApplicationContextUtilsを使用してコンテキストにアクセスできます。 autowiringではありませんが、必要なBean /サービスを自分で取得する必要があります。

+0

私は問題に言及したように試みましたが、それは動作しませんでした。 – Saumyaraj

関連する問題