当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决
在Spring里面,当一个singleton bean依赖一个prototype bean,因为singleton bean是单例的,因此prototype bean在singleton bean里面也会变成单例.
这个怎么解决呢???可以使用Spring提供的lookup-method来注入。
举例说明:先列出相关类:代码中的说明足够说明问题。user类:prototype bean
package com.cn.pojo;
import java.io.Serializable;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int userId;
private String userName;
private String userPwd;
private String userInfo;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserInfo() {
return userInfo;
}
public void setUserInfo(String userInfo) {
this.userInfo = userInfo;
}
}
LookupMethod类:singleton bean
package com.cn.springTest; import com.cn.pojo.User; /**
* LookupMethod为singleton,user为propotype
* 当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决
* @author Administrator
*
*/
public class LookupMethod {
private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} }
核心配置文件:
<!-- user userbean -->
<bean id ="user" class="com.cn.pojo.User" scope="prototype">
<property name="userId" value="1" />
<property name="userName" value="userName" />
<property name="userPwd" value="userPwd" />
<property name="userInfo" value="userInfo" />
</bean> <!-- lookupMethod LookupMethod为单例,user为原型的获取实例-->
<bean id ="lookupMethod" class="com.cn.springTest.LookupMethod">
<!-- <property name="user" ref="user"/> --> <!-- 这种写法会将user当着单例来获取-->
<lookup-method name="getUser" bean="user"/><!--lookup-method方式会将user当着原型来获取-->
</bean>
测试类:SpringLookupMethod
import com.cn.util.SpringContextUtil;
public class SpringLookupMethod {
public static void main(String[] args) {
ApplicationContext actx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
actx.getBean("springContextUtil");
LookupMethod lookupMethod = (LookupMethod) SpringContextUtil.getBean("lookupMethod");
System.out.println(lookupMethod);
LookupMethod lookupMethod1 = (LookupMethod) SpringContextUtil.getBean("lookupMethod");
System.out.println(lookupMethod1);
System.out.println(lookupMethod.getUser());
System.out.println(lookupMethod1.getUser());
System.out.println(lookupMethod.getUser());
System.out.println(lookupMethod1.getUser());
}
}
注:在测试的时候除了spring相关包,发现缺少cglib-jar包。
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>
当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决的更多相关文章
- 4.spriing:Bean的生命周期/工厂方法配置Bean/FactoryBean
1.Bean的生命周期 scope:singleton/prototype 1)spring容器管理singleton作用的生命周期,spring能够精确知道Bean合适创建,何时初始化完成,以及何时 ...
- Spring中<bean>标签之使用p标签配置bean的属性
在spring的bean配置文件中我们常可以见到下面的例子: <bean id="user" class="com.sys.User" p:name-re ...
- Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等
实验1:配置通过静态工厂方法创建的bean [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...
- Spring_配置Bean & 属性配置细节
1.Spring容器 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.Sp ...
- Spring--通过注解来配置bean【转】
Spring通过注解配置bean 基于注解配置bean 基于注解来配置bean的属性在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpat ...
- Spring--通过注解来配置bean
Spring通过注解配置bean 基于注解配置bean 基于注解来配置bean的属性 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpa ...
- 4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean
在 Spring 容器内拼凑 bean 叫做装配.装配 bean 的时候,你是在告诉容器,需要哪些 bean ,以及容器如何使用依赖注入将它们配合在一起. 理论上,bean 装配的信息可以从任何资源获 ...
- Spring 通过工厂配置Bean
1.通过静态工厂方法配置Bean 要声明通过静态方法创建的 Bean, 需要在 Bean 的 class 属性里指定拥有该工厂的方法的类, 同时在 factory-method 属性里指定工厂方法的名 ...
- spring 通过工厂方法配置Bean
概要: 通过调用静态工厂方法创建Bean 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中.当client须要对象时,仅仅须要简单地调用静态方法,而不用关心创建对象地细节. 要声明通过静 ...
随机推荐
- 【转】Hive SQL的编译过程
Hive是基于Hadoop的一个数据仓库系统,在各大公司都有广泛的应用.美团数据仓库也是基于Hive搭建,每天执行近万次的Hive ETL计算流程,负责每天数百GB的数据存储和分析.Hive的稳定性和 ...
- 【转】Java对日期Date类进行加减运算,年份加减,月份加减
import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date; public class Date ...
- 由sqlite在手机上的存储位置,引发的onCreate在哪里执行的小结
我们都知道,android为了操作数据库,一般是继承SQLiteOpenHelper类,并实现他的三个函数. 如下所示: package jz.his.db; import android.conte ...
- dp之区间:Light oj 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多 ...
- [转]好用工具:Oracle SQL Developer
我們一直以來就很少使用 Oracle 資料庫,一年下來也頂多 1 ~ 2 個案子採用 Oracle 的資料庫,所以一直都對 Oracle 資料庫的操作不太熟悉,尤其是用 Oracle 內建的那些超難用 ...
- 百度BAE 平台PHP对Mongodb的连接
<?php /*请替换为你自己的数据库名(可从管理中心查看到)*/ $dbname = 'XgmsRXDEYIDGmQFCjaZl'; /*从环境变量里取host,port,user,pwd*/ ...
- java中常用的类,包,接口
类 StringIntegerLong File DateThread(java.lang.ThreadThread类的定义:public class Thread extends Object im ...
- Linux 目录容量查询和文件打包,清空
查看使用情况 [root@instance-0yj8cprg ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/vda1 20G 4. ...
- SpringMVC之学习(0)
Spring MVC 是一个模型 - 视图 - 控制器(MVC)的Web框架建立在中央前端控制器servlet(DispatcherServlet),它负责发送每个请求到合适的处理程序,使用视图来最终 ...
- spring4.1.6配置quartz2.2.1(maven) <转>
Spring3.0不支持Quartz2.0,因为org.quartz.CronTrigger在2.0从class变成了一个interface造成IncompatibleClassChangeError ...