当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须要对象时,仅仅须要简单地调用静态方法,而不用关心创建对象地细节. 要声明通过静 ...
随机推荐
- maven install 跳过test方法
方式1:用命令带上参数 mvn install -Dmaven.test.skip=true 方式2:在pom.xml里面配置 <build> <defaultGoal>com ...
- FileZilla server windows 2003系统下适用的版本
最新版的FileZilla server 在windows 2003系统下已经不能用了 http://files.cnblogs.com/files/airoot/FileZilla_Server-0 ...
- 多行文字在一个div中上下左右居中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- RTX——第7章 任务管理
以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 单任务系统学习多任务系统之前,我们先来回顾下单任务系统的编程框架,即裸机时的编程框架. 裸机编程主要是采 ...
- mysql主从复制的介绍
引用:https://my.oschina.net/u/255939/blog/505598 MySQL复制就是一台MySQL服务器(slave)从另一台MySQL服务器(master)进行日志的复制 ...
- docker探索-Win10 Docker 安装使用(一)
本文转自:http://blog.csdn.net/shi1451042748/article/details/52996046 1.前言 Docker最近推出了可以运行在Win10稳定版本,让我们赶 ...
- 【C#/WPF】调节图像的对比度(Contrast)
关于对比度: 调节对比度直观感受是,高对比度的图像明暗关系更明显,色彩更鲜艳:低对比度的图像表面像是蒙上一层灰,色彩不鲜艳. 需求: 制作一个面板,一个滑动条,拖动滑动条可以修改目标图片的对比度. 资 ...
- 【C#】遍历List列表的同时,移除访问到的元素
需求:遍历List列表,当访问的元素符合某一条件时,将该元素移除出列表. 注意点:使用foreach循环遍历无法做到边读边修改,所以要使用for循环. 例子: // 倒序遍历. for (int i ...
- swd 适配器接口线序
1 vref 2 gnd 3 swdio FP1 4 swclk PF0 5 nrst 6 swo PF2
- Spring引入配置文件
1.spring.xml加载映射的配置配置文件 <!--采用这种方式简化配置文件--> <context:property-placeholder location="cl ...