spring 官方文档,学习笔记

来源:互联网 发布:淘宝旺旺在哪里下载 编辑:程序博客网 时间:2024/04/29 03:44

scope 相关:

DispatcherServletRequestContextListener, 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>
DispatcherServletRequestContextListener, 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.jdbcDriverjdbc.url=jdbc:hsqldb:hsql://production:9002jdbc.username=sajdbc.password=root




0 0
原创粉丝点击