Pages

Showing posts with label Different. Show all posts
Showing posts with label Different. Show all posts

Thursday, January 9, 2020

Autowire same bean using different Scopes in Class

Definitions in Application Context
      <bean id="RequestHit" class="com.springimplant.mvc.HitCounter" scope="request">  
           <aop:scoped-proxy/>  
      </bean>  
      <bean id="applicationHit" class="com.springimplant.mvc.HitCounter" scope="application">  
           <aop:scoped-proxy/>  
      </bean>  
      <bean id="sessionHit" class="com.springimplant.mvc.HitCounter" scope="session">  
           <aop:scoped-proxy/>  
      </bean>  
      <mvc:interceptors>  
           <mvc:interceptor>  
                <mvc:mapping path="/project/**"/>  
                <bean class="com.springimplant.mvc.interceptors.GlobalInterceptor"></bean>  
           </mvc:interceptor>  
      </mvc:interceptors>  

Defination in Class
 package com.springimplant.mvc.interceptors;  
 import java.util.Date;  
 import javax.annotation.Resource;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;  
 import com.springimplant.mvc.HitCounter;  
 public class GlobalInterceptor extends HandlerInterceptorAdapter {  
      @Resource   
      private HitCounter RequestHit;  
      @Resource  
      private HitCounter applicationHit;  
      @Resource  
      private HitCounter sessionHit;  
      @Override  
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  
                throws Exception {  
           RequestHit.setHits(RequestHit.getHits()+1);  
           System.out.println("Request Hits "+RequestHit.getHits());  
           applicationHit.setHits(applicationHit.getHits()+1);  
           System.out.println("Application Hits "+applicationHit.getHits());  
           sessionHit.setHits(sessionHit.getHits()+1);  
           System.out.println("Session Hits "+sessionHit.getHits());  
           request.setAttribute("currentDate",new Date());  
           return super.preHandle(request, response, handler);  
      }  
 }