【Spring实战】—— 6 内部Bean
来源:互联网 发布:win8 怎么安装ubuntu 编辑:程序博客网 时间:2024/05/29 13:35
本篇文章讲解了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="25" /> <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="30"/> <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(); }}
0 0
- 【Spring实战】—— 6 内部Bean
- Spring实战笔记——装配Bean
- spring实战——装配Bean
- 【Spring】Spring实战(二)——装配Bean
- Spring注入内部Bean
- spring中的内部bean
- Spring内部Bean
- Spring内部bean实例
- spring 内部Bean
- Spring内部bean实例
- Spring实战笔记——Bean的高级装配
- Spring实战学习笔记——bean的作用域
- Spring实战-装配bean
- 【Spring实战】—— 4 Spring中bean的init和destroy方法讲解
- spring—Bean配置
- spring—Bean配置
- spring实战-自动装配bean
- spring实战-注解装配bean
- java开发环境搭建
- 抽象类与接口
- 使用sublime结合sshfs在windows下进行linux服务器远程同步开发
- AsyncTask执行的注意事项
- Ubuntu系统安装搜狗、谷歌、五笔等输入法教程
- 【Spring实战】—— 6 内部Bean
- Copying to tmp table 解决方式
- Linux下打包压缩war,解压war和jar命令
- 让div高度跟随窗口高度自适应的方法
- Android Studio 下安卓 jni 开发错误 undefined reference to __android_log_print
- 移动端之在不同尺寸大小的手机上展示同一效果解决方案(修正二) by FungLeo
- 几款视频类库的对比
- "围观"设计模式(5)--迪米特法则(Lod,Law of Demeter)或最少知道原则(Least Knowledge Principle)
- Android 继承BaseActivity的典型用法