前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Spring实战】—— 6 内部Bean

【Spring实战】—— 6 内部Bean

作者头像
用户1154259
发布2018-01-17 17:36:00
5500
发布2018-01-17 17:36:00
举报

本篇文章讲解了Spring的通过内部Bean设置Bean的属性。   类似内部类,内部Bean与普通的Bean关联不同的是:   1 普通的Bean,在其他的Bean实例引用时,都引用同一个实例。   2 内部Bean,每次引用时都是新创建的实例。

  鉴于上述的场景,内部Bean是一个很常用的编程模式。

  下面先通过前文所述的表演者的例子,描述一下主要的类:

代码语言:javascript
复制
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();
    }
}

  其他代码,如下:

代码语言:javascript
复制
package com.spring.test.setter;

public interface Instrument {
    public void play();
}
代码语言:javascript
复制
package com.spring.test.setter;

public class Saxophone implements Instrument {
    public Saxophone(){}
    public void play() {
        System.out.println("TOOT TOOT TOOT");
    }
}
代码语言:javascript
复制
package com.spring.test.action1;

public interface Performer {
    void perform() throws PerformanceException;
}

  如果使用 设值注入 需要设定属性和相应的setter getter方法。

代码语言:javascript
复制
<?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>

  如果使用 构造注入 需要构造函数。

代码语言:javascript
复制
<?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>

  应用上下文使用方法:

代码语言:javascript
复制
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();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-01-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档