配置bean之scope

本文详细探讨了Spring IoC容器中bean的scope属性,包括singleton、prototype、request、session等不同作用域的使用场景和区别,帮助读者更好地理解和配置bean的生命周期。
摘要由CSDN通过智能技术生成
siye@r480:~/svlution/workspace/springcore4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springioc
│   │               └── domain
│   │                   └── User.java
│   └── resources
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springioc
    │               └── domain
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

15 directories, 4 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springioc.domain;

public class User {

}
<?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.xsd">

	<!--
		bean的作用域
		spring容器的上下文中,对bean的创建,默认采用的是单例模式的.

		常见的作用域
		singleton(单例的)
		org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_SINGLETON
		prototype(多例的)
		org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE

		使用方式
		a,在bean标签中设置scope属性值;
		b,或者是直接使用注解 @Scope;
		在类级别上,一般常常和 @Component 组合使用;在方法级别上,一般和 @Bean 组合使用;

		xmlConfig的方式
		<bean id="" class="" scope=""/>

		自定义bean的作用域,可参考接口,
		org.springframework.beans.factory.config.Scope
		其中有几个已经内置的实现类,其作用域是:
		request,session,globalSession,application,websocket

		一般常常使用的是单例和多例的作用域
		其他的作用域类型,待定.

	-->
	<bean class="ocn.site.springioc.domain.User" scope="prototype"></bean>

</beans>
package ocn.site.springioc.domain;

import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {

	private final Logger logger = Logger.getLogger(this.getClass());
	private @Autowired ApplicationContext context;

	@Test
	public void run() throws Exception {
		User auser = context.getBean(User.class);
		logger.info(auser);
		User buser = context.getBean(User.class);
		logger.info(buser);
		Assert.assertNotEquals(auser, buser);
	}

}
19-09-08 15:38:03 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-08 15:38:04 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@13fee20c, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4e04a765, org.springframework.test.context.support.DirtiesContextTestExecutionListener@783e6358]
19-09-08 15:38:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-08 15:38:04 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@643b1d11: startup date [Sun Sep 08 15:38:04 CST 2019]; root of context hierarchy
19-09-08 15:38:04 ocn.site.springioc.domain.Runtest  =====>>> ocn.site.springioc.domain.User@45f45fa1
19-09-08 15:38:04 ocn.site.springioc.domain.Runtest  =====>>> ocn.site.springioc.domain.User@4c6e276e
19-09-08 15:38:04 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@643b1d11: startup date [Sun Sep 08 15:38:04 CST 2019]; root of context hierarchy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值