posts - 4,  comments - 2,  trackbacks - 0
初始化web配置
如果你使用的是Servlet2.4 及以上的web容器,那么仅需要在web.xml 中增加ContextListener即可

<web-app>
...
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>


如果你使用的是早期版本web容器 Servlet 2.4 以前,那么需要使用一个javax.servlet.Filter 的实现

<web-app>
..
<filter>
    <filter-name>requestContextFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>requestContextFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>

----------------------------------------------------------------------
RequestContextListener and RequestContextFilter 两个类做的都是同样的工作;将HTTP request 对象绑定到为该请求提供服务的Thread。这使具有request or session 作用域的bean能够在后面的调用链中被访问到。


作用域bean 与依赖
如果打算将一个Http request 范围的bean 注入到别一个bean 中,那么需要注入一个AOP代理来替代被注入的作用域bean,也就是说需要注入一个代理对象。
注意
<aop:scoped-proxy/>不能和作用域为singleton 或 prototype 的bean一起使用。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- a HTTP Session-scoped bean exposed as a proxy -->
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
<!-- this next element effects the proxying of the surrounding bean -->
<aop:scoped-proxy/>
</bean>
<!-- a singleton-scoped bean injected with a proxy to the above bean -->
<bean id="userService" class="com.foo.SimpleUserService">
<!-- a reference to the proxied 'userPreferences' bean -->
<property name="userPreferences" ref="userPreferences"/>
</bean>




初始化回调

可以在Bean 定义中指定一个普通的初始化方法,即在XML配置文件中通过指定init-method 属性来完成。

<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init" />

public class ExampleBean {
    public void init(){
        //do some initialization work
    }
}


析构回调
<bean id="exampleInitBean" class="examples.ExampleBean" destory-method="cleanup"/>

public class ExampleBean {
    public void cleanup(){
        //do some destruction work..
    }
}


posted on 2006-12-26 13:35 ziwolf 阅读(359) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: