本篇讲述了Bean的基本配置方法,以及Spring中怎样运用Bean。java
主要内容以下:spring
在Spring框架中,能够经过ref来互相引用相同或不一样xml配置文件中定义的Bean。session
若是你想引用不一样xml配置文件中的bean,可使用’ref’标签,结合’bean’属性。框架
格式:<ref bean="someBean"/>ide
在下边的例子中,bean’ OutputHelper’在’ Spring-Common.xml’文件中被定义,经过使用’ref’标签,结合’bean’属性,能够引用’ Spring-Output.xml’文件中定义的两个bean(“CsvOutputGenerator” 和 “JsonOutputGenerator“)函数
配置文件:Spring-Output.xml以下测试
<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-2.5.xsd"> <bean id="CsvOutputGenerator" class="com.lei.output.impl.CsvOutputGenerator" /> <bean id="JsonOutputGenerator" class="com.lei.output.impl.JsonOutputGenerator" /> </beans>
配置文件: Spring-Common.xml以下this
<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-2.5.xsd"> <bean id="OutputHelper" class="com.lei.output.OutputHelper"> <property name="outputGenerator" > <ref bean="CsvOutputGenerator"/> </property> </bean> </beans>
若是你想引用相同xml配置文件中的bean,可使用’ref’标签,结合’local’属性。spa
格式:<ref local="someBean"/>prototype
配置文件:Spring-Output.xml以下
<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-2.5.xsd"> <bean id="OutputHelper" class="com.lei.output.OutputHelper"> <property name="outputGenerator" > <ref local="CsvOutputGenerator"/> </property> </bean> <bean id="CsvOutputGenerator" class="com.lei.output.impl.CsvOutputGenerator" /> <bean id="JsonOutputGenerator" class="com.lei.output.impl.JsonOutputGenerator" /> </beans>
实际上,’ref’标签中’bean’属性,既能够引用相同xml文件中的bean,也能够引用不一样xml文件中的bean,可是,考虑到项目的可读性,引用相同xml配置文件的bean时,应该尽可能使用’local’属性。
Spring中,一般有3种方法给Bean的属性注入value。
通常方法,缩写方法,”p” schema方法。
先看下边的Bean:FileNameGenerator.java,其中包含两个properties,name和type,咱们向两个properties注入value。
package com.lei.common; public class FileNameGenerator { private String name; private String type; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
<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-2.5.xsd"> <bean id="FileNameGenerator" class="com.lei.common.FileNameGenerator"> <property name="name"> <value>lei</value> </property> <property name="type"> <value>txt</value> </property> </bean> </beans>
<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-2.5.xsd"> <bean id="FileNameGenerator" class="com.lei.common.FileNameGenerator"> <property name="name" value="lei" /> <property name="type" value="txt" /> </bean> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="FileNameGenerator" class="com.lei.common.FileNameGenerator" p:name="lei" p:type="txt" /> </beans>
注意,这种方法须要在bean的配置文件xml中,加入如下声明
xmlns:p=”http://www.springframework.org/schema/p
如下Demo演示了一个Bean中嵌套了另外一个Bean,即所谓的内部嵌套Bean的配置方法,内部嵌套的Bean支持属性(property)注入和构造函数(constructor-arg)注入。
先看一下Customer.java 和Person.java
package com.lei.common; public class Customer { private Person person; public Customer(Person person) { this.person = person; } public void setPerson(Person person) { this.person = person; } @Override public String toString() { return "Customer [person=" + person + "]"; } }
package com.lei.common; public class Person { private String name; private String address; private int age; //getter and setter methods…此处省略 @Override public String toString() { return "Person [address=" + address + ", age=" + age + ", name=" + name + "]"; } }
配置Bean时,要在Customer的Bean中注入内部Bean,即Person。
1. 在Customer中,能够用’ref’属性引用Person的Bean,以下
<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-2.5.xsd"> <bean id="CustomerBean" class="com.lei.common.Customer"> <property name="person" ref="PersonBean" /> </bean> <bean id="PersonBean" class="com.lei.common.Person"> <property name="name" value="lei" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </beans>
2. 以上方法利用’ref’很好的引用了Person,可是,一旦Person仅仅被用在Customer下,也就是说不会被别的Bean引用,最好的方法就是在Customer的bean中声明一个内部Bean,以下
<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-2.5.xsd"> <bean id="CustomerBean" class="com.lei.common.Customer"> <property name="person"> <bean class="com.lei.common.Person"> <property name="name" value="lei" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </property> </bean> </beans>
3. 内部Bean也能够经过构造函数注入
<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-2.5.xsd"> <bean id="CustomerBean" class="com.lei.common.Customer"> <constructor-arg> <bean class="com.lei.common.Person"> <property name="name" value="lei" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </constructor-arg> </bean> </beans>
注意,以上2,3两种状况下,Person的Bean配置中,能够忽略id属性。
在Spring中,Bean的做用域决定了从Spring容器中返回的Bean实例的类型。
在Spring中,支持如下5中类型的做用域:
注:大多数状况下,你可能只须要处理Spring的核心做用域 — 单例模式(singleton)和原型模式(prototype),默认状况下,做用域是单例模式。
singleton和prototype区别
CustomerService.java以下
package com.lei.customer.services; public class CustomerService { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
若是是singleton状况下的配置以下
<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-2.5.xsd"> <bean id="customerService" class="com.lei.customer.services.CustomerService" /> </beans>
以上配置中,若是没有指定scope范围,默认状况下是sighleton模式。
运行下边的代码:
package com.lei.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lei.customer.services.CustomerService; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"}); CustomerService custA = (CustomerService)context.getBean("customerService"); custA.setMessage("Message by custA"); System.out.println("Message : " + custA.getMessage()); //retrieve it again CustomerService custB = (CustomerService)context.getBean("customerService"); System.out.println("Message : " + custB.getMessage()); } }
输出结果以下:
Message : Message by custA
Message : Message by custA
Protptype状况下的配置以下:
<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-2.5.xsd"> <bean id="customerService" class="com.lei.customer.services.CustomerService" scope="prototype"/> </beans>
再运行一下测试代码,输出结果以下:
Message : Message by custA
Message : null
设置scope为prototype后,测试代码中,每调用一次getBean()方法后,都会获得一个新的实例。
本节讲述怎样将值注入集合类型,包含如下四种主要的集合类型:
List —— <list/>
Set —— <set/>
Map —— <map/>
Properties —— <props/>
首先写一个Bean,一个Customer对象,包含四种集合属性,以下,
Customer.java
package com.lei.common; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Customer { private List<Object> lists; private Set<Object> sets; private Map<Object, Object> maps; private Properties pros; //...此处省略setter和getter }
<property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.lei.common.Person"> <property name="name" value="leiList" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </list> </property>
<property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.lei.common.Person"> <property name="name" value="leiSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property>
<property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.lei.common.Person"> <property name="name" value="leiMap" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </entry> </map> </property>
<property name="pros"> <props> <prop key="admin">admin@nospam.com</prop> <prop key="support">support@nospam.com</prop> </props> </property>
综上,全部的bean配置文件以下
<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-2.5.xsd"> <bean id="CustomerBean" class="com.lei.common.Customer"> <!-- java.util.List --> <property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.lei.common.Person"> <property name="name" value="leiList" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </list> </property> <!-- java.util.Set --> <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.lei.common.Person"> <property name="name" value="leiSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property> <!-- java.util.Map --> <property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.lei.common.Person"> <property name="name" value="leiMap" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </entry> </map> </property> <!-- java.util.Properties --> <property name="pros"> <props> <prop key="admin">admin@nospam.com</prop> <prop key="support">support@nospam.com</prop> </props> </property> </bean> <bean id="PersonBean" class="com.lei.common.Person"> <property name="name" value="lei1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> </beans>