当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须要对象时,仅仅须要简单地调用静态方法,而不用关心创建对象地细节. 要声明通过静 ...
随机推荐
- git 分支管理 推送本地分支到远程分支等
1.创建本地分支 local_branch git branch local_branch 2.创建本地分支local_branch 并切换到local_branch分支 git checkout - ...
- Maven项目下HttpServletRequest 或 HttpServletResponse需引用的依赖包
转载: http://xyly624.blog.51cto.com/842520/865630/ Maven项目下HttpServletRequest 或 HttpServletResponse需引用 ...
- Python3 Scrapy 安装方法
Python3 Scrapy 安装方法 (一脸辛酸泪) 写在前面 最近在学习爬虫,在熟悉了Python语言和BeautifulSoup4后打算下个爬虫框架试试. 没想到啊,这坑太深了... 看了看相关 ...
- Windows C++ 非递归式(stack)深度优先遍历目录
#include <Windows.h> #include <cstdio> #include <cstring> #include <string> ...
- Linux GPIO控制方法
Linux GPIO控制方法 kernel version 4.4.12 在文件系统层: 1. 进入 /sys/class/gpio/ 目录 2. 假设你想控制的GPIO0_29,步骤如下: 1. e ...
- [应用]Linux下" >/dev/null 2>&1 "
转自:http://blog.csdn.net/sunrier/article/details/7695839 这条命令的意思就是在后台执行这个程序,并将错误输出2重定向到标准输出1,然后将标准输出1 ...
- 设计模式-观察者模式(下)<转>
观察者模式在Java中有两种实现方式,上文是一种方式,这种方式是自己写代码实现. 另一种方式是使用Java内置的观察者模式来实现. 相关的接口和类如下: java.util.Observable ...
- Select显示多级分类列表
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 当数据库的字段为date类型时候
当数据库的字段为date类型时候: //--------------------------------------------------数据库设计------------------------- ...
- openfire 详细介绍一
XMPP ExtensibleMessaging and Presence Protocol,简单的来讲,它就是一个发送接收处理消息的协议,但是这个协议发送的消息,既不是二进制的东东也不是字符串,而是 ...