Spring核心之2 SpringBean的作用域

2.Bean的作用域

当通过Spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域。

默认作用域为singleton单例模式

Spring支持如下5种作用域

2.1.singleton

singleton:单例模式,在整个Spring IoC容器中,使用singleton定义的Bean将只有一个实例

默认作用域为singleton单例模式

对于singleton作用域的Bean,每次请求该Bean都将获得相同的实例。容器负责跟踪Bean实例的状态,负责维护Bean实例的生命周期行为;

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

2.1.1.意图

保证一个类仅有一个实例,并提供一个访问它的全局访问点。

2.1.2.主要解决

一个全局使用的类频繁地创建与销毁。

2.1.3.何时使用

当您想控制实例数目,节省系统资源的时候。

2.1.4.如何解决

判断系统是否已经有这个单例,如果有则返回,如果没有则创建。

2.1.5.关键代码

构造函数是私有的。

2.1.6.应用实例

1、一个党只能有一个主席。

2、Windows 是多进程多线程的,在操作一个文件的时候,就不可避免地出现多个进程或线程同时操作一个文件的现象,所以所有文件的处理必须通过唯一的实例来进行。

3、一些设备管理器常常设计为单例模式,比如一个电脑有两台打印机,在输出的时候就要处理不能两台打印机打印同一个文件。

2.1.7.优点

1、在内存里只有一个实例,减少了内存的开销,尤其是频繁的创建和销毁实例(比如管理学院首页页面缓存)。

2、避免对资源的多重占用(比如写文件操作)。

2.1.8.缺点

没有接口,不能继承,与单一职责原则冲突,一个类应该只关心内部逻辑,而不关心外面怎么样来实例化。

2.1.9.使用场景

1、要求生产唯一序列号。

2、WEB 中的计数器,不用每次刷新都在数据库里加一次,用单例先缓存起来。

3、创建的一个对象需要消耗的资源过多,比如 I/O 与数据库的连接等。

2.1.10.注意事项

1、单例类只能有一个实例。

2、单例类必须自己创建自己的唯一实例。

3、单例类必须给所有其他对象提供这一实例。

4、getInstance() 方法中需要使用同步锁 synchronized (Singleton.class) 防止多线程同时进入造成 instance 被多次实例化。

2.1.11 代码样例

0.日志输出log4j.properties配置
log4j.rootLogger=DEBUG, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n 
1.Beans001.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- 用构造器去实例化bean -->
        <bean
        id="singletonExampleBean"
        class="com.gaoxinfu.demo.spring.example.bean.ExampleBean"/>
</beans>
2.ExampleBean
package com.gaoxinfu.demo.spring.example.bean;

import com.gaoxinfu.demo.base.Base;

public class ExampleBean extends Base{

	public ExampleBean() {
		logger.info("ExampleBean构造方法ExampleBean()实例化ExampleBean");
	}

	public void execute() {
		logger.info("ExampleBean方法execute()执行ExampleBean处理");
	}
}
3.ExampleBeanTest
@Test
public void singleton() {
	String beanConfig="Beans001.xml";
	ApplicationContext context =new ClassPathXmlApplicationContext(beanConfig);
	ExampleBean exampleBean01= context.getBean("singletonExampleBean",ExampleBean.class);
	ExampleBean exampleBean02= context.getBean("singletonExampleBean",ExampleBean.class);
	logger.info("exampleBean01==exampleBean02 结果为="+(exampleBean01==exampleBean02));
	
}

4.日志输出
StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
 StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
 StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
 ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@303bc257: startup date [Sun Feb 11 14:26:33 CST 2018]; root of context hierarchy
 StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
 StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
 StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
 XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [Beans001.xml]
 DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
 PluggableSchemaResolver - Loading schema mappings from [META-INF/spring.schemas]
 PluggableSchemaResolver - Loaded schema mappings: {http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd, http://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd}
 PluggableSchemaResolver - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd
 DefaultBeanDefinitionDocumentReader - Loading bean definitions
 XmlBeanDefinitionReader - Loaded 5 bean definitions from location pattern [Beans001.xml]
 ClassPathXmlApplicationContext - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@303bc257: org.springframework.beans.factory.support.DefaultListableBeanFactory@1018f51f: defining beans [calendarobj1,calendarobj2,calendarobj3,calendarobj4,exampleBean]; root of factory hierarchy
 ClassPathXmlApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@2a8ddc4c]
 ClassPathXmlApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@2f60877b]
 DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1018f51f: defining beans [calendarobj1,calendarobj2,calendarobj3,calendarobj4,exampleBean]; root of factory hierarchy
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj1'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj1'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj1' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj1'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj2'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj2'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj2' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj2'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj3'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj3'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj3' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj3'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj4'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj4'
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'calendarobj3'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj4' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj4'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'exampleBean'
 DefaultListableBeanFactory - Creating instance of bean 'exampleBean'
 BaseDemo - ExampleBean构造方法ExampleBean()实例化ExampleBean
 DefaultListableBeanFactory - Eagerly caching bean 'exampleBean' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'exampleBean'
 ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@6cf1f051]
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
 PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
 PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
 PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'exampleBean'
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'exampleBean'
 BaseDemo - exampleBean01==exampleBean02 结果为=true
 

2.2.prototype

1.prototype:原型模式,每次通过容器的getBean方法获取prototype定义的Bean时,都将产生一个新的Bean实例

2.如果一个Bean被设置成prototype作用域,程序每次请求该id的Bean,Spring都会新建一个Bean实例,然后返回给程序。在这种情况下,Spring容器仅仅使用new 关键字创建Bean实例,一旦创建成功,容器不在跟踪实例,也不会维护Bean实例的状态

3.如果不指定Bean的作用域,Spring默认使用singleton作用域。

4.Java在创建Java实例时,需要进行内存申请;销毁实例时,需要完成垃圾回收,这些工作都会导致系统开销的增加。因此,prototype作用域Bean的创建、销毁代价比较大。而singleton作用域的Bean实例一旦创建成功,可以重复使用。因此,除非必要,否则尽量避免将Bean被设置成prototype作用域。

5.非单例的,原型的Bean指的就是每次请求Bean实例的时候,返回的都是新实例的Bean对象。也就是说,每次注入到另外的Bean或者通过调用getBean()来获得的Bean都将是全新的实例。

6.这是基于线程安全性的考虑,如果使用有状态的Bean对象用原型作用域,而无状态的Bean对象用单例作用域。

下面的例子说明了Spring的原型作用域。DAO通常不会配置为原型对象,因为典型的DAO是不会有任何的状态的。

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

与其他的作用域相比,Spring是不会完全管理原型Bean的生命周期的:Spring容器只会初始化,配置以及装载这些Bean,传递给Client。但是之后就不会再去管原型Bean之后的动作了。

也就是说,初始化生命周期回调方法在所有作用域的Bean是都会调用的,但是销毁生命周期回调方法在原型Bean是不会调用的。所以,客户端代码必须注意清理原型Bean以及释放原型Bean所持有的一些资源。

可以通过使用自定义的bean post-processor来让Spring释放掉原型Bean所持有的资源。

在某些方面来说,Spring容器的角色就是取代了Java的new操作符,所有的生命周期的控制需要由客户端来处理。

1.example样例

0.日志输出log4j.properties配置
log4j.rootLogger=DEBUG, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n 
1.Beans001.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- 用构造器去实例化bean -->
        <bean
        id="prototypeExampleBean"
        class="com.gaoxinfu.demo.spring.example.bean.ExampleBean" scope="prototype" />
</beans>
2.ExampleBean
package com.gaoxinfu.demo.spring.example.bean;

import com.gaoxinfu.demo.base.Base;

public class ExampleBean extends Base{

	public ExampleBean() {
		logger.info("ExampleBean构造方法ExampleBean()实例化ExampleBean");
	}

	public void execute() {
		logger.info("ExampleBean方法execute()执行ExampleBean处理");
	}
}
3.ExampleBeanTest
@Test
public void prototype() {
	String beanConfig="Beans001.xml";
	ApplicationContext context =new ClassPathXmlApplicationContext(beanConfig);
	ExampleBean exampleBean01= context.getBean("prototypeExampleBean",ExampleBean.class);
	ExampleBean exampleBean02= context.getBean("prototypeExampleBean",ExampleBean.class);
	logger.info("exampleBean01==exampleBean02 结果为="+(exampleBean01==exampleBean02));
	
}

4.日志输出
StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
 StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
 StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
 ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@303bc257: startup date [Sun Feb 11 14:37:08 CST 2018]; root of context hierarchy
 StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
 StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
 StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
 XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [Beans001.xml]
 DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
 PluggableSchemaResolver - Loading schema mappings from [META-INF/spring.schemas]
 PluggableSchemaResolver - Loaded schema mappings: {http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd, http://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd}
 PluggableSchemaResolver - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd
 DefaultBeanDefinitionDocumentReader - Loading bean definitions
 XmlBeanDefinitionReader - Loaded 5 bean definitions from location pattern [Beans001.xml]
 ClassPathXmlApplicationContext - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@303bc257: org.springframework.beans.factory.support.DefaultListableBeanFactory@6ab30913: defining beans [calendarobj1,calendarobj2,calendarobj3,calendarobj4,exampleBean]; root of factory hierarchy
 ClassPathXmlApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@2c78bc3b]
 ClassPathXmlApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@f8622f3]
 DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6ab30913: defining beans [calendarobj1,calendarobj2,calendarobj3,calendarobj4,exampleBean]; root of factory hierarchy
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj1'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj1'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj1' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj1'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj2'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj2'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj2' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj2'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj3'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj3'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj3' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj3'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj4'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj4'
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'calendarobj3'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj4' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj4'
 ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@51c888d9]
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
 PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
 PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
 PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
 DefaultListableBeanFactory - Creating instance of bean 'exampleBean'
 BaseDemo - ExampleBean构造方法ExampleBean()实例化ExampleBean
 DefaultListableBeanFactory - Finished creating instance of bean 'exampleBean'
 DefaultListableBeanFactory - Creating instance of bean 'exampleBean'
 BaseDemo - ExampleBean构造方法ExampleBean()实例化ExampleBean
 DefaultListableBeanFactory - Finished creating instance of bean 'exampleBean'
 BaseDemo - exampleBean01==exampleBean02 结果为=false
 

2.example的对象的初始化和销毁

1.beans001配置
 <bean id="prototypeExampleBean" class="com.gaoxinfu.demo.spring.example.bean.ExampleBean" scope="prototype" init-method="init" destroy-method="destroy"/>
2.ExampleBean
public class ExampleBean extends Base{

	public ExampleBean() {
		logger.info("ExampleBean构造方法ExampleBean()实例化ExampleBean");
	}

	public void execute() {
		logger.info("ExampleBean方法execute()执行ExampleBean处理");
	}
	
	public void init() {
		logger.info("ExampleBean方法init()初始化ExampleBean对象");
	}
	
	public void destroy() {
		logger.info("ExampleBean方法destroy()销毁ExampleBean对象");
	}
}
3.ExampleBeanTest
@Test
public void prototype_init_destroy() {
	String beanConfig="Beans001.xml";
	ApplicationContext context =new ClassPathXmlApplicationContext(beanConfig);
	ExampleBean exampleBean01= context.getBean("prototypeExampleBean",ExampleBean.class);
	ExampleBean exampleBean02= context.getBean("prototypeExampleBean",ExampleBean.class);
	logger.info("exampleBean01==exampleBean02 结果为="+(exampleBean01==exampleBean02));
	AbstractApplicationContext abstcontext=(AbstractApplicationContext) context;
	abstcontext.close();
}
4.日志输出
StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
 StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
 StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
 ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@303bc257: startup date [Sun Feb 11 16:15:16 CST 2018]; root of context hierarchy
 StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
 StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
 StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
 XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [Beans001.xml]
 DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
 PluggableSchemaResolver - Loading schema mappings from [META-INF/spring.schemas]
 PluggableSchemaResolver - Loaded schema mappings: {http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd, http://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd}
 PluggableSchemaResolver - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd
 DefaultBeanDefinitionDocumentReader - Loading bean definitions
 XmlBeanDefinitionReader - Loaded 6 bean definitions from location pattern [Beans001.xml]
 ClassPathXmlApplicationContext - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@303bc257: org.springframework.beans.factory.support.DefaultListableBeanFactory@1018f51f: defining beans [calendarobj1,calendarobj2,calendarobj3,calendarobj4,singletonExampleBean,prototypeExampleBean]; root of factory hierarchy
 ClassPathXmlApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@2a8ddc4c]
 ClassPathXmlApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@2f60877b]
 DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1018f51f: defining beans [calendarobj1,calendarobj2,calendarobj3,calendarobj4,singletonExampleBean,prototypeExampleBean]; root of factory hierarchy
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj1'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj1'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj1' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj1'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj2'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj2'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj2' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj2'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj3'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj3'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj3' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj3'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'calendarobj4'
 DefaultListableBeanFactory - Creating instance of bean 'calendarobj4'
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'calendarobj3'
 DefaultListableBeanFactory - Eagerly caching bean 'calendarobj4' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'calendarobj4'
 DefaultListableBeanFactory - Creating shared instance of singleton bean 'singletonExampleBean'
 DefaultListableBeanFactory - Creating instance of bean 'singletonExampleBean'
 BaseDemo - ExampleBean构造方法ExampleBean()实例化ExampleBean
 DefaultListableBeanFactory - Eagerly caching bean 'singletonExampleBean' to allow for resolving potential circular references
 DefaultListableBeanFactory - Finished creating instance of bean 'singletonExampleBean'
 ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@6cf1f051]
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
 PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
 PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
 PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
 DefaultListableBeanFactory - Creating instance of bean 'prototypeExampleBean'
 BaseDemo - ExampleBean构造方法ExampleBean()实例化ExampleBean
 DefaultListableBeanFactory - Invoking init method  'init' on bean with name 'prototypeExampleBean'
 BaseDemo - ExampleBean方法init()初始化ExampleBean对象
 DefaultListableBeanFactory - Finished creating instance of bean 'prototypeExampleBean'
 DefaultListableBeanFactory - Creating instance of bean 'prototypeExampleBean'
 BaseDemo - ExampleBean构造方法ExampleBean()实例化ExampleBean
 DefaultListableBeanFactory - Invoking init method  'init' on bean with name 'prototypeExampleBean'
 BaseDemo - ExampleBean方法init()初始化ExampleBean对象
 DefaultListableBeanFactory - Finished creating instance of bean 'prototypeExampleBean'
 BaseDemo - exampleBean01==exampleBean02 结果为=false
 ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@303bc257: startup date [Sun Feb 11 16:15:16 CST 2018]; root of context hierarchy
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
 DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1018f51f: defining beans [calendarobj1,calendarobj2,calendarobj3,calendarobj4,singletonExampleBean,prototypeExampleBean]; root of factory hierarchy
5.问题

控制台没有输出预期的"ExampleBean方法destroy()销毁ExampleBean对象",原因在于beans001文件中设置的destroy-method属性仅仅对单例模式有用,在prototype模式下没有意义;

修改beans001.xml,使用singleton模式创建对象,代码如下:

<bean
        id="prototypeExampleBean"
        class="com.gaoxinfu.demo.spring.example.bean.ExampleBean"
         scope="singleton" init-method="init" destroy-method="destroy"/>

2.3.request

request:对于每次HTTP请求,使用request定义的Bean都将产生一个新实例,即每次HTTP请求将会产生不同的Bean实例。只有在Web应用中使用Spring时,该作用域才有效

2.4.session

session:对于每次HTTP Session,使用session定义的Bean豆浆产生一个新实例。同样只有在Web应用中使用Spring时,该作用域才有效

2.5.globalsession

globalsession:每个全局的HTTP Session,使用session定义的Bean都将产生一个新实例。典型情况下,仅在使用portlet context的时候有效。同样只有在Web应用中使用Spring时,该作用域才有效

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东山富哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值