本章类容来源于Spring in Action3第二章。
内容:
- 声明bean
- 构造器注入和setter方法注入
- 装配bean
- 控制bean的创建和销毁
观众朋友们好,这里是上海电视台,欢迎收看中国达人秀节目。
本章类容来源于Spring in Action3第二章。
内容:
public interface Performer { public void perform(); }所有参与中国达人秀的同学都要实现该接口。
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" > </beans>
public class Juggler implements Performer { private int beanBags = 3; public Juggler() { } public Juggler(int beanBags) { this.beanBags = beanBags; } public void perform() { System.out.println("juggler " + beanBags + " bags"); } }
<bean id="duke" class="in.action3.chapter2.Juggler"></bean>好了,我们使用下面的代码,给duke一个排练的机会
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/inject-service.xml"); Performer performer = (Performer)ctx.getBean("duke"); performer.perform(); }输出juggler 3 bags。
<bean id="duke" class="in.action3.chapter2.Juggler"> <constructor-arg value="15"></constructor-arg> </bean>如果不配置<constructor-arg/>,Spring使用默认的构造方法,配置了<constructor-arg/>,Spring将调用Juggler的另一个构造方法。
public class PoeticJuggler extends Juggler { private Poem poem ; public PoeticJuggler(Poem poem) { super(); this.poem = poem; } public PoeticJuggler(int bags, Poem poem) { super(bags); this.poem = poem; } @Override public void perform() { super.perform(); System.out.println("while reciting ...."); poem.recite(); } }PoeticJuggler持有Poem接口的引用,Poem接口定义:
public interface Poem { public void recite(); }我们定义一首诗Sonnet29,实现Poem接口:
public class Sonnet29 implements Poem { public void recite() { System.out.println("every body, now, I will recite Sonnet29, when ......"); } }在XML配置中,将Sonnet29声明为一个bean:
<bean id="sonnet29" class="in.action3.chapter2.Sonnet29"></bean>现在我们来声明一个PoeticJuggler的bean,讲sonnet29注入给它:
<bean id="poeticDuke" class="in.action3.chapter2.PoeticJuggler"> <constructor-arg value="15"></constructor-arg> <constructor-arg ref="sonnet29"></constructor-arg> </bean>poeticDuke排练下:
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/inject-service.xml"); Performer performer = (Performer)ctx.getBean("poeticDuke"); performer.perform(); }
public class Stage { private Stage(){}; private static class StageSingletonHolder{ static Stage instance = new Stage(); } public static Stage getInstance(){ return StageSingletonHolder.instance; } }题外话:
<bean id="stage" class="in.action3.chapter2.Stage" factory-method="getInstance"></bean>
<bean id="ticket" class="in.action3.chapter2.Ticket" scope="prototype"></bean>通过scope属性来指定bean的作用域。
<bean id="stage" class="in.action3.chapter2.Stage" factory-method="getInstance" init-method="turnOnLights" destroy-method="turnOffLights"></bean>如果上下文中有很多Bean拥有相同名字的初始化和销毁方法,可以使用 beans元素的default-init-method和default-destory-method属性。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-init-method="turnOnLights" default-destroy-method="turnOffLights" >
public class Instrumentalist implements Performer { private Instrument instrument; private String song; public Instrument getInstrument() { return instrument; } public void setInstrument(Instrument instrument) { this.instrument = instrument; } public String getSong() { return song; } public void setSong(String song) { this.song = song; } public void perform() { System.out.println("表演的歌曲名字是:"+song); instrument.play(); } }定义乐器接口:
public interface Instrument { public void play(); }定义萨克斯乐器:
public class Saxophone implements Instrument { public void play() { System.out.println("TOOT TOOT TOOT"); } }Spring 配置:
<bean id="saxophone" class="in.action3.chapter2.Saxophone"></bean> <bean id="kenney" class="in.action3.chapter2.Instrumentalist"> <!-- setter注入简单的值 --> <property name="song" value="Jinle bells"></property> <!-- setter注入bean --> <property name="instrument" ref="saxophone"></property> </bean>执行代码:
public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring/inject-service.xml"); Performer performer = (Performer)ctx.getBean("kenney"); performer.perform(); ctx.close(); }输出:
表演的歌曲名字是:Jinle bells TOOT TOOT TOOT
<bean id="kenney" class="in.action3.chapter2.Instrumentalist"> <!-- setter注入简单的值 --> <property name="song" value="Jinle bells"></property> <!-- setter注入bean --> <property name="instrument" > <!-- 注入内部Bean,该bean是kenney私有的 --> <bean class="in.action3.chapter2.Saxophone"></bean> </property> </bean>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >重新定义kenney bean配置
<bean id="saxophone" class="in.action3.chapter2.Saxophone"></bean> <bean id="kenney" class="in.action3.chapter2.Instrumentalist" p:song="Jinle bells" p:instrument-ref="saxophone"> </bean>使用<porperty>还是p取决于你, 他们是等价的,p的主要优点是简洁。
public class OneManBand implements Performer { private Collection<Instrument> instruments; public Collection<Instrument> getInstruments() { return instruments; } public void setInstruments(Collection<Instrument> instruments) { this.instruments = instruments; } public void perform() { for (Instrument instrument : instruments) { instrument.play(); } } }
<bean id="hank" class="in.action3.chapter2.OneManBand"> <property name="instruments"> <list> <ref bean="saxophone"/> <ref bean="guitar"/> <ref bean="harmonica"/> </list> </property> </bean>
public class OneManBand implements Performer { private Map<String, Instrument> instruments; public Map<String, Instrument> getInstruments() { return instruments; } public void setInstruments(Map<String, Instrument> instruments) { this.instruments = instruments; } public void perform() { for (Entry<String, Instrument> entry : instruments.entrySet()) { System.out.println("乐器:"+entry.getKey()+" 演奏:"); entry.getValue().play(); } } }配置:
<bean id="hank" class="in.action3.chapter2.OneManBand"> <property name="instruments"> <map> <entry key="saxophone" value-ref="saxophone"></entry> <entry key="guitar" value-ref="guitar"></entry> <entry key="harmonica" value-ref="harmonica"></entry> </map> </property> </bean>
public class OneManBand implements Performer { private Properties instruments; public Properties getInstruments() { return instruments; } public void setInstruments(Properties instruments) { this.instruments = instruments; } public void perform() { for (Entry<Object, Object> entry : instruments.entrySet()) { System.out.println("乐器:"+entry.getKey()+" 演奏:"+ entry.getValue()); } } }配置:
<bean id="hank" class="in.action3.chapter2.OneManBand"> <property name="instruments"> <props> <prop key="saxophone">saxophone</prop> <prop key="guitar">guitar</prop> <prop key="harmonica">harmonica</prop> </props> </property> </bean>
<property name="someNullProperty"><null/></property>
相关推荐
1.4 装配Bean-自动装配(重要!) Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。 自动装配(autowiring):Spring自动满足bean之间的依赖。 1 ...
本篇文章主要介绍了详解Spring框架---IOC装配Bean,提供了三种方式实例化Bean,具有一定的参考价值,有兴趣的可以了解一下。
spring入门学习-3、Bean装配(XML).pdf
Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃...它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transaction Management
spring-aop-4.0.0.RELEASE工具类,用于spring注解装配bean
博客地址:https://blog.csdn.net/u010476739/article/details/76732201 spring装配bean的方式实例
spring-beans:Bean工厂与装配 spring-context:上下文,即IOC容器 spring-context-support:对IOC的扩展,以及IOC子容器 spring-context-indexer:类管理组件和Classpath扫描 spring-expression:表达式语句 切面编程: ...
主要给大家介绍了关于spring装配bean的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
NULL 博文链接:https://364232252.iteye.com/blog/2369853
springframework 是sping 里面的一个开源框架...它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transaction Management,等等......
主要介绍了JSP Spring 自动化装配Bean实例详解的相关资料,需要的朋友可以参考下
(3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 <bean class="xxx" ></bean> (4)constructor:与 byType 方式类似,不同之处是构造注入 <bean class="xxx" ></bean> 本例,演示 byName...
它们被Spring IOC容器初始化,装配,和管理。这些beans通过容器中配置的元数据创建。比如,以XML文件中<bean/> 的形式定义。Spring 框架定义的beans都是单件beans。在bean tag中有个属性”singleton”,如果它被赋为...
主要介绍了spring在IoC容器中装配Bean详解,具有一定借鉴价值,需要的朋友可以参考下
本篇文章主要介绍了 Spring的自动装配Bean的三种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...
今天小编就为大家分享一篇关于在Spring中自动装配Bean的属性,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
主要介绍了spring定义和装配bean详解,具有一定参考价值,需要的朋友可以了解下。
主要介绍了Spring @Conditional条件化装配bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
目的:Spring容器已经成功获取了UserController实例,并通过调用实例中的方法执行了各层中的输出语句。 运行结果为: User [id=1, name=张三, password=123] userDao say hello world! UserService say hello world ...