Spring框架学习(6):bean的作用域
来源:互联网 发布:淘宝卖家怎么投诉盗图 编辑:程序博客网 时间:2024/06/05 15:15
本篇文章讲bean的作用域
在默认情况下,IOC容器创建的对象是单例的,即多次使用getBean方法时会返回同一个bean对象,即bean对象只会被创建一次。
用一个简单的例子可以证明这一点。
在Address类的构造函数中打印一句话:
package beans;public class Address {private String city;private String street;public Address() {super();System.out.println("address construct..");}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getStreet() {return street;}public void setStreet(String street) {this.street = street;}@Overridepublic String toString() {return "Address [city=" + city + ", street=" + street + "]";}}在spring bean配置文件中缺省的使作用域为单例模式:
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="address" class="beans.Address"><property name="city" value="GuangZhou"></property><property name="street" value="Shangxiajiu"></property></bean><bean id="person" class="beans.Person" depends-on="address"><property name="name" value="Tom"></property></bean></beans>使用main函数测试一下:
package beans;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");Address address = ctx.getBean(Address.class);address = ctx.getBean(Address.class);}}可以在控制台上看到:
address construct..
现在我们使scope=“prototype”看看会输出什么?
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="address" class="beans.Address" scope="prototype"><property name="city" value="GuangZhou"></property><property name="street" value="Shangxiajiu"></property></bean><bean id="person" class="beans.Person" depends-on="address"><property name="name" value="Tom"></property></bean></beans>运行main函数会发现输出了3行address construct..,这是因为我们在main函数中使用了两次的getBean方法,然后person bean中依赖了address,因此address被实例化了3次。
总结一下就是,singleton是默认值,在容器初始化时就创建bean的实例,在整个容器的生命周期中只创建单个bean,而prototype在每次请求时都创建一个新的bean实例
0 0
- Spring框架学习(6):bean的作用域
- Spring框架中Bean的作用域
- (五)Spring框架——Bean的作用域
- Spring学习(4)--Spring中Bean的作用域
- Spring框架Bean作用域
- Spring 学习笔记 Bean的作用域
- 学习笔记:Spring Bean的作用域
- Spring学习笔记(二)Bean的作用域
- Spring学习一(入门和bean的作用域)
- Spring 4.0 学习日记(4)--- bean的作用域
- Spring学习(五)-Bean的作用域
- Spring学习笔记(5.Spring Bean装配之Bean的配置项及作用域)
- Spring学习,bean作用域
- spring学习笔记(6)——bean的作用域
- Spring bean的作用域(转)
- Spring(六):Bean 的作用域
- Spring bean的作用域(scope)
- spring 6 bean配置--bean的作用域
- machine learning-model总结
- Direct3D学习笔记(一)——精灵(Bomo_catcher游戏)
- UGUI的按钮监听事件
- 异步委托
- java中死锁,并发,web缓存的相关问题
- Spring框架学习(6):bean的作用域
- app专项的网络测试和Fiddler弱网模拟
- 【LeetCode】01 Matrix 解题报告
- 快速排序
- TCP/IP 数据流套接字传输注意事项
- MATLAB 学习(六)M文件和面向对象编程
- QT开发用ffmpeg将图片制作成视频
- 关于MySql 持续积累,进行记录
- js面向对象编程:if中可以使用那些作为判断条件呢?