一、

Spring的bean默认是单例的

But sometimes you may find yourself working with a mutable class that does main-
tain some state and therefore isn’t safe for reuse. In that case, declaring the class as a
singleton bean probably isn’t a good idea because that object can be tainted and cre-
ate unexpected problems when reused later.
Spring defines several scopes under which a bean can be created, including the
following:
 Singleton—One instance of the bean is created for the entire application.
 Prototype—One instance of the bean is created every time the bean is injected
into or retrieved from the Spring application context.
 Session—In a web application, one instance of the bean is created for each session.
 Request—In a web application, one instance of the bean is created for each request.

二、@Scope的三种用法

1.在自动扫描中

 @Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Notepad { ... }

2.在java配置文件中

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Notepad notepad() {
return new Notepad();
}
ww

3.在xml配置文件中

<bean id="notepad" class="com.myapp.Notepad" scope="prototype" />

三、

若不指明proxyMode,当把一个session或request的bean注入到sigleton的bean时,会出现问题。如把购物车bean注入到service bean

 @Component
@Scope(
value = WebApplicationContext.SCOPE_SESSION,
proxyMode = ScopedProxyMode.INTERFACES)
public ShoppingCart cart() {... }
@Component
public class StoreService {
@Autowired
public void setShoppingCart(ShoppingCart shoppingCart) {
this.shoppingCart = shoppingCart;
}
...
}

因为StoreService是signleton,是在容器启动就会创建,而shoppingcart是session,只有用户访问时才会创建,所以当StoreService企图要注入shoppingcart时,很有可能shoppingcart还没创建。spring用代理解决这个问题,当ShoppingCart是接口时,指定 ScopedProxyMode.INTERFACES。当ShoppingCart是一个类时,则指定ScopedProxy- Mode.TARGET_CLASS,srping会通过CGLib来创建基于类的代理对象。当request注入到signleton bean时,也是一样。

在xml中声明proxy策略

 <?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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cart" class="com.myapp.ShoppingCart" scope="session">
<aop:scoped-proxy />
</bean>
</beans>

<aop:scoped-proxy> is the Spring XML configuration’s counterpart to the @Scope
annotation’s proxyMode attribute. It tells Spring to create a scoped proxy for the bean.
By default, it uses CGL ib to create a target class proxy. But you can ask it to generate an
interface-based proxy by setting the proxy-target-class attribute to false :

<bean id="cart" class="com.myapp.ShoppingCart" scope="session">
<aop:scoped-proxy proxy-target-class="false" />
</bean>

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary

    一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...

  2. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值

    1.When injecting properties and constructor arguments on beans that are created via component-scanni ...

  3. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解

    一. The @Qualifier annotation is the main way to work with qualifiers. It can beapplied alongside @Au ...

  4. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

    一. 1.SpEL expressions are framed with  #{ ... } 2.SpEl的作用 Sp EL has a lot of tricks up its sleeves, ...

  5. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value

    一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...

  6. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

    一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...

  7. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles

    一. Spring honors two separate properties when determining which profiles are active:spring.profiles. ...

  8. SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile

    一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...

  9. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile

    一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...

随机推荐

  1. Oracle常用查询命令

    Oracle查询完整格式如下: Select  *  from XX where XX group by YY having XX order by YY Select count(*), XX fr ...

  2. Ubuntu 14.04 忘记用户密码(备忘)

    参考文章地址:www.linuxidc.com/Linux/2013-11/92236.htm 重启电脑 开机就会进入一个Grub引导页面,选择 "Ubuntu 高级选项"之后,按 ...

  3. Discuz!nt整合心得

    最近研究了下Discuz!nt的整合,因为是网上找的实例,有个地方的写错了,导致纠结了一整天,这里分享出来. Discuz!nt提供了整合工具DiscuzToolkit,用于调用Discuz!nt A ...

  4. Google Developers中国网站发布!(转)

    Google Developers 中国网站是特别为中国开发者而建立的,它汇集了 Google 为全球开发者所提供的开发技术资源,包括 API 文档.开发案例.技术培训的视频.并涵盖了以下关键开发技术 ...

  5. MVC Filter自定义异常(拦截)

    // ----------------------------------------------------------------------- // <copyright file=&qu ...

  6. mysql笔记之集群

    1.主从配置 #主从都要加入以下配置如下 [mysqld] log-bin=mysql-bin #主从要不一样 server-id=222 #在主上建立一个用户给从的用 GRANT REPLICATI ...

  7. MinGW-notepad++开发c/c++程序

    下载MinGW 点击下载 安装好后运行 最后点击左上角的 Installation,开始安装 1.编译: g++ -o a.exe a.cpp gcc -o hello.exe hello.c 2.运 ...

  8. 关于Spring AOP和IOC的一些总结

    Spring官方网站:https://spring.io/ 最早对象的创建是有new关键字,但是如果产生的类比较繁多或者复杂,就用工厂代替new关键字,但是工厂的控制能力有限,譬如对产生对象的生命周期 ...

  9. 切割TOMCAT日志

    tomcat的catalina.out日志如果不做操作的话,日志就会日积月累的不断增加.我刚入职的时候发现某台服务器的硬盘报警,排查之后我慌了,一个tomcat的日志居然有100G,这怎么可以,在网上 ...

  10. github 查看单个文件的历史记录命令

    gitk 安装: apt-get install gitk 点击打开链接http://stackoverflow.com/questions/278192/view-the-change-histor ...