SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode
一、
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的更多相关文章
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary
一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值
1.When injecting properties and constructor arguments on beans that are created via component-scanni ...
- 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 ...
- 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, ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value
一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)
一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles
一. Spring honors two separate properties when determining which profiles are active:spring.profiles. ...
- SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile
一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile
一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...
随机推荐
- T-SQL基础 (存储过程,触发器|| 笔记0808)
一:存储过程 1.使用EXEC 调用存储过程 2.系统存储过程是以SP_开头,SP_ProcedureName.:例子:EXEC sp_columns TableName 查看列信息 扩展存储过程 ...
- NUnit使用详解(一)
转载:http://hi.baidu.com/grayworm/item/38e246165aa7b9433a176e90 NUnit是一个单元测试框架,专门针对于.NET来写的,它是是xUnit的一 ...
- 从URI中获取实际的文件path
如题,经常用在onActivityResult方法中解析图片等各种地址,因为Android 4.4之后google更改了对应的方法. /** * Get a file path from a Uri. ...
- Java——类比较器
1.Product类 public class Product { private int pid; private String name; private double price; public ...
- Oracle 递归函数与等级
--基数数据1 SELECT ID, mt.materialtypename, mt.parenttypeid FROM material_type mt; 使用递归还是与LEVEL 1 SELECT ...
- MVC 读取图片
/// <summary> /// 获取上传的图片 /// </summary> public FileResult GetImages(string PhotoPath) ...
- oracle数据库TNS
TNS是Oracle Net的一部分,专门用来管理和配置Oracle数据库和客户端连接的一个工具,在大多数情况下客户端和数据库要通讯,必须配置TNS,下面看一如何配置它吧: TNS简要介绍与应用 :O ...
- 02_Jquery_03_类选择器
[简述] 类选择器就是通过类名(css类名)来查询元素! $(".myClass")就可以把所有包含了class="myClass"的元素查询出来 [index ...
- MFC类的结构
1. CObject类,MFC库中绝大部分类的基类,封装了MFC中的最基本机制. 运行时类信息机制/动态创建机制/序列化机制等... 2. CCmdtarget - 消息映射机制最基类 3. CWin ...
- jquery-ui-datepicker定制化,汉化,因手机布局美观化源码修改
感谢浏览,欢迎交流=.= 公司微信网页需要使用日历控件,想到jquery-mobile,但是css影响页面布局,放弃后使用jquery-ui-datepicker. 话不多说,进入正题: 1.jque ...