Spring--Bean scope
singleton, prototype,request, session, global session
bean.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-2.5.xsd"> <bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
</bean> <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype">
<property name="userDAO" ref="u" />
</bean>
</beans>
UserServiceTest.java:
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; //Dependency Injection
//Inverse of Control
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
UserService service2 = (UserService)ctx.getBean("userService");
System.out.println(service == service2); } }
结果:false
xml改成singleton结果就是true
Spring--Bean scope的更多相关文章
- (转)Spring Bean Scope 有状态的Bean 无状态的Bean
有状态会话bean :每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”:一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束.即每个用户最初都会得到一 ...
- Spring Bean Scope 有状态的Bean 无状态的Bean
http://blog.csdn.net/anyoneking/article/details/5182164 在Spring的Bean配置中,存在这样两种情况: <bean id=" ...
- Spring bean - scope详解
Scope是定义Spring如何创建bean的实例的. 在创建bean的时候可以带上scope属性,scope有下面几种类型. Singleton 这也是Spring默认的scope,表示Spring ...
- Spring Bean Scope (作用域)
singleton: 单例模式,针对每个spring容器,只有一个该类的实例被管理,每次调用此实例都是同一个对象被返回,所以适用于无状态bean.默认情况下,singleton作为spring容器中b ...
- [Spring] Bean Scope Singleton cs Prototype
We can define a class to be Singleton or Prototype. If the class was defined as Prototype, then ever ...
- spring作用域(Spring Bean Scopes Example)
http://docs.spring.io/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes In Spring, bean sc ...
- Spring标签之Bean @Scope
@Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...
- Spring课程 Spring入门篇 4-7 Spring bean装配之基于java的容器注解说明--@Scope 控制bean的单例和多例
1 解析 1.1 bean的单例和多例的应用场景 1.2 单例多例的验证方式 1.3 @Scope注解单例多例应用 2 代码演练 2.1 @Scope代码应用 1 解析 1.1 bean的单例和多例的 ...
- spring 支持集中 bean scope?
Spring bean 支持 5 种 scope: Singleton - 每个 Spring IoC 容器仅有一个单实例. Prototype - 每次请求都会产生一个新的实例. Request - ...
- 终于搞懂Spring中Scope为Request和Session的Bean了
之前只是很模糊的知道其意思,在request scope中,每个request创建一个新的bean,在session scope中,同一session中的bean都是一样的 但是不知道怎么用代码去验证 ...
随机推荐
- 【转】curl 查看一个web站点的响应时间(rt)
原文: http://blog.csdn.net/caoshuming_500/article/details/14044697 1. curl 查看web站点rt curl -o /dev/null ...
- TexturePacker license Key免费获取方式
TexturePacker是一款功能非常强大的图片制作工具.是一款付费软件,但是TexturePacker的作者Andreas Löw先生也给出获得免费key 的方法...大家可以到这个网站去申请 h ...
- iOS 枚举写法
1.第一种 typedef enum { kPRStateNormal = 0, kPRStatePulling = 1, kPRStateLoading = 2, kPRStateHitTheEnd ...
- Flask architecture
论文The Flask Security Architecture: System Support for Diverse Security Policies 介绍了Flask architectur ...
- Android之SurfaceView学习(一)转转
Android之SurfaceView学习(一) 首先我们先来看下官方API对SurfaceView的介绍 SurfaceView的API介绍 Provides a dedicated drawing ...
- Android自定义XML属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-style ...
- ssh能够连接而sftp不能连接的解决方法
ssh能够连接而sftp不能连接的解决方法 昨天开始用FileZilla一直不能登录远程的服务器,ssh的登录就OK,因为是服务器,也不敢乱动.查了好多资料终于解决了. 首先,查看一下系统的安全日 ...
- linux文件分割(将大的日志文件分割成小的)【转载】
linux文件分割(将大的日志文件分割成小的)linux下文件分割可以通过split命令来实现,可以指定按行数分割和安大小分割两种模式.Linux下文件合并可以通过cat命令来实现,非常简单. 在Li ...
- 将json转化为model
/// <summary> /// 获取Json的Model /// </summary> /// <typeparam name="T">&l ...
- 利用transform制作幻灯片
html代码 <html><head></head><body><div class='hpic'> <ul style=" ...