spring学习之IOC容器2

spring学习之IOC容器2

bean的作用域

当我们定义一个bean时,你得为创建的很多bean实例提供一种方式。这个方式的定义很重要,它意味着你能为很多实例归并到一种方式里。
可以控制各个依赖和要被插入到从一特定bean定义创建的对象的配置值。
以下作用域支持开箱即用。您还可以创建一个自定义的范围。
singleton代表这在spring-ioc容器中只存在一个实例化的对象对应着bean。

prototype代表有多个不同的实例对象。

request代表每个http请求的生命周期都会新创建一个对象实例,只在基于spring-web的上下文才有效。ApplicationContext。

session代表每个http session的生命周期中都会新创建一个对象,只有存在spring-web的上下文中才有效,ApplicationContext。

application代表每个http整个应用期间都会只创建一个对象,只有存在spring-web的上下文中才有效,ApplicationContext。

web-socket代表每个web-socket生命周期期间都会只创建一个对象,只有存在spring-web的上下文中才有效,ApplicationContext。

singleton

只有一个实例被管理。
singleton

<bean id="accountService" class="com.foo.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>

prototype-scope

每次请求都会生成一个实例。
prototype

<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>

Request, session, application, and WebSocket scopes

开始的web配置

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

另外,如果有与您的监听器设置问题,可以考虑使用Spring的RequestContextFilter两个。该过滤器映射取决于周围的Web应用程序的配置,所以你必须改变它为宜。

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

Request scope
xml配置如下

<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>

注解方式

@RequestScope
@Component
public class LoginAction {
    // ...
}

在下面的例子中的配置只有加了一行,而是要理解“为什么”以及“如何做”的背后是非常重要的。

<?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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- an HTTP Session-scoped bean exposed as a proxy -->
    <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
        <!-- instructs the container to proxy 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>
</beans>

为什么在注入bean scope在request,session,自定义的scope时,在配置中需要加入aop:scoped-proxy?
默认情况下,当生成一个代理的bean时,自动的标记aop:scoped-proxy,一个cglib的代理被创建。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值