当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须要对象时,仅仅须要简单地调用静态方法,而不用关心创建对象地细节. 要声明通过静 ...
随机推荐
- Java web项目中java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
原来是tomcat找不到MYSQL JAR包的问题.后来又把mysql-connector-java-5.1.7-bin.jar导入到tomcat的lib目录下面就ok了,嘿…… 在java项目中,只 ...
- python 列表的内容赋值
l1 = '20180201 b4b8e187-d59d-33fb-addc-ef189aca3712 com.ss.android.article.news' l2 = re.split('[ ]+ ...
- c++之——派生类的同名成员和函数调用方式及构造析构顺序
#include<iostream> using namespace std; class Object { public: Object(), b(), c() { cout <& ...
- dp之分组背包hdu1712
题意:有n门课程,和m天时间,完成a[i][j]得到的价值为第i行j列的数字,求最大价值...... 思路:分组背包,就是第n门课程,可以做一天,可以做两天,但它们相斥,你做了一天,就不能再做一天.. ...
- enter快捷键盘
protected override bool ProcessDialogKey(Keys keyData) { #region PageDown if (keyData == Keys.Enter) ...
- Winform 加密连接字符串“未能提供RsaProtectedConfigurationProvider加密,对象已存在”的解决方案
当一台机器已安装软件,并有新用户需要使用此软件时提示“未能提供RsaProtectedConfigurationProvider加密,对象已存在”. 这是因为加密模式是用户模式,需要运行以下脚本添加新 ...
- Mac升级yosemite后无法登陆问题
Mac升级yosemite后无法登陆问题 今天心血来潮准备玩玩最新的苹果系统10.10,代号是yosemite.去官网申请了beta版的測试资格,然后在app store下载了一晚上得 ...
- 二、Redis命令行和配置文件redis.windows.conf
一.Redis发送命令的两种方式 redis-cli -h localhost -p 6379redis-cli ping 返回pong 证明正常 二.命令返回值 1.状态回复,如ping命令 2.错 ...
- mongodb查询之从多种分类中获取各分类最新一条记录
mongodb查询之从多种分类中获取各分类最新一条记录 2017年04月06日 13:02:47 monkey_four 阅读数:6707更多 个人分类: MongoDBJavaScript 文章 ...
- BuddyPress创建组、查看成员信息等找不到页面
BuddyPress创建组.查看成员信息等找不到页面 http://aoxuangame.com/wordpress/groups/create/ http://aoxuangame.com/word ...