Spring学习笔记之Bean的装配

来源:互联网 发布:登录页面html模板源码 编辑:程序博客网 时间:2024/05/22 11:50

这篇文章将开启我学习Spring框架,Spring的配置非常简单,只需要一个配置文件且名字随意。使用Spring来加载bean是非常方便的。

声明Bean

为了减少依赖,这里先声明接口

public interface Performer {public void perform();public void setName(String name);}


public interface ISong {public void info();}


用Bean实现接口

public class Song implements ISong{private String name;private String time;public Song(){this.name="Just One";this.time="3:12";}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}public void info() {System.out.println("歌曲名字:"+name);System.out.println("歌曲时间长度:"+time);}}

public class PerformerImpl implements Performer{private String name;private ISong song;public PerformerImpl(String name,ISong song){this.name=name;this.song=song;}public void setName(String name){this.name=name;}public void perform() {System.out.println("演唱者:"+name);System.out.println("歌曲信息:");this.song.info();}}


public class Stage {private static Stage stage=new Stage();private Stage(){}public static Stage instance(){return stage;}public void info(){System.out.println("舞台信息");}}


然后在配置文件里声明bean

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" 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   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><bean id="stage" class="bean.Stage" factory-method="instance"></bean><bean id="song" class="bean.Song"></bean><bean id="performImpl" class="bean.PerformerImpl"><constructor-arg value="yukjin" /><constructor-arg ref="song" /></bean></beans>

测试代码

public class TestOne {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");Performer performer=(Performer)ctx.getBean("performImpl");performer.perform();performer.setName("luzhen");System.out.println("########################################");Performer anotherPerformer=(Performer)ctx.getBean("performImpl");anotherPerformer.perform();Stage stage=(Stage)ctx.getBean("stage");stage.info();}}



运行结果

演唱者:yukjin
歌曲信息:
歌曲名字:Just One
歌曲时间长度:3:12
########################################
演唱者:luzhen
歌曲信息:
歌曲名字:Just One
歌曲时间长度:3:12
舞台信息



上面的这些主要实践了以下几个点:

配置文件使用bean标签来声明一个Bean,最基本的需要配置id属性和class属性,id用来标示一个bean,而class告诉Spring这个bean具体是哪个类。

使用constructor-arg子标签来使用构造器方式初始化bean,value属性用来传基本类型参数,如果参数是一个对象,则使用ref属性引用一个bean,属性值是一个已经声明了的bean id。

使用工厂方法加载bean,需要在bean标签factory-method指定工厂方法。

从输出结果可以看出,对第一个performer修改名字后,影响到了anotherPerformer,验证了Spring默认加载bean的方式是单例。如果需要修改这种默认方式,可以通过指定bean的scope属性来设置bean的作用域。

Spring支持如下定义域。

singleton 在每一个spring容器中,一个bean定义只有一个实例对象(默认)

prototype 允许bean的定义可以被实例化任意次(每次调用产生一个新实例)

request 在一次http请求中,每个bean定义对应一个实例。该作用域仅在基于web的spring上下文中才有效。

session 在一http session中,每个bean定义对应一个实例。该作用域仅在基于web的spring上下文中才有效

global-session 在一个全局http session中,每个bean定义对应一个实例仅作用在portlet上下文中


这里通过设置performer的scope属性为prototype得到下面结果

演唱者:yukjin
歌曲信息:
歌曲名字:Just One
歌曲时间长度:3:12
########################################
演唱者:yukjin
歌曲信息:
歌曲名字:Just One
歌曲时间长度:3:12
舞台信息


注入bean属性

bean的属性通常是私有的,因此每个属性会有相应的set/get方法。使用Spring可以方便的对bean的属性进行注入。

通过bean的子标签property用来注入属性值,它的name表示属性名,如果属性值为基本类型则使用value设置,否则使用ref。如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" 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   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><bean id="stage" class="bean.Stage" factory-method="instance" init-method="init" destroy-method="destroy"></bean><bean id="song" class="bean.Song" ></bean><bean id="f" class="bean.Person"><property name="name" value="lu XX"/></bean><bean id="performImpl" class="bean.PerformerImpl" scope="prototype" ><constructor-arg value="yukjin" /><constructor-arg ref="song" /><property name="sex" value="male"/><property name="father" ref="f"/></bean></beans>

我为performer注入了性别以及父亲(对象)。


装配集合

Spring提供了4中集合配置元素

<list> 装配list类型的值,允许重复

<set> 装配set类型的值,不允许重复

<map> 装配map类型的值,键值可以为任意类型

<props> 装配properties类型的值,键值为String类型


例如我将performer father属性改为parent并将类型改为list,并通过如下方式初始化。

<bean id="f" class="bean.Person"><property name="name" value="lu XX" /></bean><bean id="m" class="bean.Person"><property name="name" value="wang XX" /></bean><bean id="performImpl" class="bean.PerformerImpl" scope="prototype"><constructor-arg value="yukjin" /><constructor-arg ref="song" /><property name="sex" value="male" /><property name="father"><list><ref bean="f" /><ref bean="m" /></list></property></bean>

打印结果

演唱者:yukjin
性别:male
歌曲信息:
歌曲名字:Just One
歌曲时间长度:3:12
家庭信息:
姓名:lu XX
姓名:wang XX


如果list中是基本类型可以使用value设值。

0 0
原创粉丝点击