当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须要对象时,仅仅须要简单地调用静态方法,而不用关心创建对象地细节. 要声明通过静 ...
随机推荐
- WebForm中使用MVC
http://www.cnblogs.com/encoding/articles/3556046.html ********************************************** ...
- ypbind启动失败
[root@bs035 test]# cat /etc/yp.conf # /etc/yp.conf - ypbind configuration file# Valid entries are## ...
- mysql sleep进程 过多
如果你没有修改过MySQL的配置,缺省情况下,wait_timeout的初始值是28800. wait_timeout过大有弊端,其体现就是MySQL里大量的SLEEP进程无法及时释放,拖累系统性能, ...
- activemq5.11整合spring4.2.3
前言 这篇博客记录 activemq5.11整合spring4.2.3的过程,免得以后忘记了 1.工程结构 2.pom.xml <project xmlns="http://maven ...
- Apache HttpComponents 文件上传例子
/* * ==================================================================== * * Licensed to the Apache ...
- java——常用类的总结
package test; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; public cla ...
- socket编程基础-字节序/IP/PORT转换/域名
socket编程基础 网络IP操作函数 字符串的IP和32位的IP转换 #include <sys/socket.h> #inlcude <netinet/in.h> #inc ...
- MySql: ”Commands out of sync“Error (Connect/C++)
使用 Connector/C++ 查询 Mysql , 连续调用存储过程时 会出现如下: Commands out of sync; you can't run this command now,st ...
- javascript获取URL参数和参数值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 用javascript将数据导入Excel
网上收集的代码 <input type="button" name="out_excel" onclick="AutomateExcel();& ...