Spring当中aop:scoped-proxy 的用法

马克- to-win:马克 java社区:防盗版实名手机尾号: 73203
当把生命周期短的bean(比如下例中的MyBean)注入到生命周期长的bean(比如下例中的SingletonBean)时,我们必须做特殊处理,比如加<aop:scoped-proxy>来修饰短生命周期的bean。为什么?其实也好理解。比如下例中的生命周期长的bean (SingletonBean)的类型是Singleton,还没有用户访问时,在最初的时刻就建立了,而且只建立一次。这时它的一个属性myBean却要急着指向另外一个session类型的bean(com.MyBean),而com.MyBean的生命周期短(只有当有用户访问时,它才被生成)。现在处于初始阶段,还没有用户上网呢,所以com.MyBean的真正对象还没有生成呢。所以<aop:scoped-proxy>的意思就是让myBean这个属性指向com.MyBean的一个代理对象。(该代理对象拥有和com.MyBean完全相同的public接口。调用代理对象方法时,代理对象会从Session范围内获取真正的com.MyBean对象,调用其方法)。下例中如果去除<aop:scoped-proxy /> 会报以下的错误:Error creating bean with name 'myBean': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean,注意在做以下实验时,要导入包cglib-nodep-2.1_3.jar。


例 2.4.1

    <bean id="myBean" class="com.MyBean" scope="session">
         <aop:scoped-proxy />
    </bean>  
  
    <bean id="singletonBean" class="com.SingletonBean">
        <property name="myBean">
            <ref bean="myBean" />
        </property>
    </bean>





package com;
public class MyBean implements IMyBean{
    private int count;
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public void increment()
    {
        count ++;
    }
}



package com;
public interface IMyBean {
    public void increment();
    public int getCount();
}



package com;
public class SingletonBean {
    private MyBean myBean;
    public int getCount() {
        return myBean.getCount();
    }
    public void increment() {
        myBean.increment();
    }
    public void setMyBean(MyBean myBean) {
        this.myBean = myBean;
    }
}





package com;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;
import service.interfac.ILoginService;

@Controller
public class HelloWorldController {
    private ILoginService loginServic;

    private IMyBean myBean;
    @RequestMapping("/helloa")
    public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response,
            HttpSession sesssion) {
        ServletContext sc=RequestContextUtils.getWebApplicationContext(request).getServletContext();
        WebApplicationContext wac=WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        ILoginService loginServic=(ILoginService)wac.getBean("loginService");
        SingletonBean singletonBean=(SingletonBean)wac.getBean("singletonBean");
 //       loginServic.login();
        singletonBean.increment();
        System.out.println("myBean.getCount() "+singletonBean.getCount());
        System.out.println("after loginServic.login()");
        return new ModelAndView("/helloq", "message", "你好");
    }
}


输出结果:(在同一个浏览器中反复执行就是以下结果,换一个浏览器数据重新向上加)

myBean.getCount() 1
after loginServic.login()
myBean.getCount() 2
after loginServic.login()
myBean.getCount() 3
after loginServic.login()