【Spring实战】—— 6 内部Bean
本篇文章讲解了Spring的通过内部Bean设置Bean的属性。
类似内部类,内部Bean与普通的Bean关联不同的是:
1 普通的Bean,在其他的Bean实例引用时,都引用同一个实例。
2 内部Bean,每次引用时都是新创建的实例。
鉴于上述的场景,内部Bean是一个很常用的编程模式。
下面先通过前文所述的表演者的例子,描述一下主要的类:
package com.spring.test.setter; import com.spring.test.action1.PerformanceException;
import com.spring.test.action1.Performer; public class Instrumentalist implements Performer{
private String song;
private int age;
private Instrument instrument;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public Instrument getInstrument() {
return instrument;
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public Instrumentalist(){}
public Instrumentalist(String song,int age,Instrument instrument){
this.song = song;
this.age = age;
this.instrument = instrument;
}
public void perform() throws PerformanceException {
System.out.println("Instrumentalist age:"+age);
System.out.print("Playing "+song+":");
instrument.play();
}
}
其他代码,如下:
package com.spring.test.setter;
public interface Instrument {
public void play();
}
package com.spring.test.setter;
public class Saxophone implements Instrument {
public Saxophone(){}
public void play() {
System.out.println("TOOT TOOT TOOT");
}
}
package com.spring.test.action1;
public interface Performer {
void perform() throws PerformanceException;
}
如果使用 设值注入 需要设定属性和相应的setter getter方法。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="kenny" class="com.spring.test.setter.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="age" value="" />
<property name="instrument">
<bean class="com.spring.test.setter.Saxophone"/>
</property>
</bean>
</beans>
如果使用 构造注入 需要构造函数。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="kenny-constructor" class="com.spring.test.setter.Instrumentalist">
<constructor-arg value="Happy New Year"/>
<constructor-arg value=""/>
<constructor-arg>
<bean class="com.spring.test.setter.Saxophone"/>
</constructor-arg>
</bean>
</beans>
应用上下文使用方法:
public class test {
public static void main(String[] args) throws PerformanceException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
Instrumentalist performer = (Instrumentalist)ctx.getBean("kenny");
performer.perform();
Instrumentalist performer2 = (Instrumentalist)ctx.getBean("kenny-constructor");
performer2.perform();
}
}
【Spring实战】—— 6 内部Bean的更多相关文章
- spring实战五之Bean的自动检测
在spring实战四中,使用在Spring中增加<context:annotation-config>的方式告诉Spring,我们打算使用基于注解的自动装配,希望Spring特殊对待我们所 ...
- Spring中的内部Bean
简介 当一个bean仅被用作另一个bean的属性时,它能被声明为一个内部bean,为了定义inner bean,在Spring 的 基于XML的 配置元数据中,可以在 <property/> ...
- spring实战二之Bean的自动装配(非注解方式)
Bean的自动装配 自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bea ...
- spring实战三装配bean之Bean的作用域以及初始化和销毁Bean
1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...
- Spring的引用内部Bean属性和给级联属性
第一个是内部Bean的配置: 首先是要理解其中的原理,再去操作就很简单了,下面老表就给大家说一下自己的观点(有点简单,但是老表我第一次学习的时候看着视频上的代码确实有点懵逼 ...
- Spring实战之装配Bean
1.1Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当 ...
- spring实战四之Bean的自动装配(注解方式)
使用注解装配: 从spring2.5开始,Spring启用了使用注解自动装配Bean的属性,使用注解方式自动装配与在XML中使用 autowire 属性自动装配并没有太大区别,但是使用注解方式允许更细 ...
- 【spring bean】 spring中bean之间的引用以及内部bean
在spring中会有如下的几种情况: 1.在当前容器中,(即在spring.xml这一个配置文件中),一个bean引用了另一个bean. 使用 1> <ref bean="另 ...
- Spring Bean的生命周期,《Spring 实战》书中的官方说法
连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...
随机推荐
- 30、Flask实战第30天:cms模版抽离和个人信息页面完成
cms模版抽离 新建一个cms_base.html文件作为基础模板,把cms_index.html的内容拷贝到cms_base.html中. 编辑 cms_base.html,把在不同页面会变动的部分 ...
- 【BZOJ 1018】【SHOI 2008】堵塞的交通traffic
http://www.lydsy.com/JudgeOnline/problem.php?id=1018 线段树维护连通性. 把每一列看成一个节点,对于线段树上的每一个节点,维护8个信息,前6个字面意 ...
- python基础之单例模式
单例模式: 什么是单例模式? 基于某种方法实例化多次得到实例是同一个 实现方法: ip = '1.1.1.1' port = 3306 # 假装来自配置文件 #方法一:定义类方法进行判断 class ...
- [转]JSP中常见的Tomcat报错错误解析(一)
1**:请求收到,继续处理2**:操作成功收到,分析.接受3**:完成此请求必须进一步处理4**:请求包含一个错误语法或不能完成5**:服务器执行一个完全有效请求失败 100——客户必须继续发出请求1 ...
- [转] FileSystemXmlApplicationContext、ClassPathXmlApplicationContext和XmlWebApplicationContext简介
今天在用Spring时遇到一个问题,提示找不到applicationContext.xml文件.原来是在加载这个文件时调用的方法不太合适,所以造成了程序找不到项目下的xml配置文件. 我们常用的加载c ...
- 关于智能硬件设备shell安全设计
问题描述: 在对某些智能硬件设备进行测试时,发现有些设备直接提供了Linux shell,并且登录账号默认是root权限!在登录到设备后,在bin目录下可以看到很多命令行程序,这些程序大部门用户用不到 ...
- JavaScript 开发的45个技巧
JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...
- appium+python自动化49-yaml管理定位元素
前言 如何高效管理定位元素,这个是很有学问的问题,也是面试必问的[以下纯属个人观点,勿喷!]. 有的人用xml管理页面定位元素,这种逼格略高,但是小编认为学习成本大,贼麻烦. 有的人提到用excel管 ...
- 前端对比插件JS
https://github.com/kpdecker/jsdiff demo http://kpdecker.github.io/jsdiff/ 缺点:文件大于10M的就比较不了了 用于比对两段HT ...
- 一步一步在Windows下搭建React Native Android开发环境
搭建JAVA开发环境 依据操作系统分为x86或x64位的.下载jdk1.8以上的版本号. 本机安装时的java版本号:jdk-8u45-windows-x64.exe 配置JAVA的环境变量 JAVA ...