您的位置:首页 > 编程语言 > Java开发

spring 官方文档,学习笔记

2015-11-02 10:24 477 查看
scope 相关:

DispatcherServlet
RequestContextListener
,
and 
RequestContextFilter
 all
do exactly the same thing, namely bind the HTTP request object to the 
Thread
 that
is servicing that request. This makes beans that are request- and session-scoped available further down the call chain.

scope 的五种状态中的,singleton 和 prototype 不用做特殊配置,至于 request,session, global session 则需要相关配置,如果使用spring web mvc 框架自动配置,如果使用其他的 比如 struts 则需要在web.xml 里 配置 request  监听器,

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

对于老的web 服务器 如果不起作用,则配置 过滤器

<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>

DispatcherServlet
RequestContextListener
,
and 
RequestContextFilter
 all
do exactly the same thing, namely bind the HTTP request object to the 
Thread
 that
is servicing that request. This makes beans that are request- and session-scoped available further down the call chain.

单例类引用原型类问题:

由于单例只创建一次,则如果其中有对原型类 的依赖,原型也只会创建一次,如果想该单例的原型属性每次都新创建,则需要方法注入。

单例类注入request , session ,global session scope 的类时,需要加代理 :

<bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
 <aop:scoped-proxy/>
</bean>
<bean id="userManager" class="com.foo.UserManager">
<property name="userPreferences" ref="userPreferences"/>
</bean>


xml 配置文件使用property文件的例子:




<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>

<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>


property 文件格式:




jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: