Bean的裝配方式可以理解為將Bean依賴注入到Spring容器中茂洒,Bean的裝配方式即Bean的依賴注入方式搜变。Spring容器支持基于XML配置的裝配采缚、基于注解的裝配以及自動裝配等多種裝配方式。本節(jié)將主要講解基于XML配置的裝配和基于注解的裝配挠他。
1.基于XML配置的裝配
通過2.3節(jié)的學(xué)習(xí)之后扳抽,我們知道Spring提供了兩種基于XML配置的裝配方式:構(gòu)造方法注入和屬性setter方法注入。
目錄結(jié)構(gòu)
1.1創(chuàng)建Bean的實現(xiàn)類
在ch3應(yīng)用src目錄中,創(chuàng)建包assemble贸呢,在assemble包下創(chuàng)建類ComplexUser镰烧。在類ComplexUser中分別使用構(gòu)造方法注入和setter注入
ComplexUser.java
package assemble;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ComplexUser {
private String uname;
private List<String> hobbyList;
private Map<String,String> residenceMap;
private Set<String> aliasSet;
private String[] array;
public ComplexUser(String uname, List<String> hobbyList, Map<String, String> residenceMap, Set<String> aliasSet,
String[] array) {
super();
this.uname = uname;
this.hobbyList = hobbyList;
this.residenceMap = residenceMap;
this.aliasSet = aliasSet;
this.array = array;
}
public ComplexUser() {
super();
// TODO Auto-generated constructor stub
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public List<String> getHobbyList() {
return hobbyList;
}
public void setHobbyList(List<String> hobbyList) {
this.hobbyList = hobbyList;
}
public Map<String, String> getResidenceMap() {
return residenceMap;
}
public void setResidenceMap(Map<String, String> residenceMap) {
this.residenceMap = residenceMap;
}
public Set<String> getAliasSet() {
return aliasSet;
}
public void setAliasSet(Set<String> aliasSet) {
this.aliasSet = aliasSet;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
@Override
public String toString() {
return "ComplexUser [uname=" + uname + ", hobbyList=" + hobbyList + ", residenceMap=" + residenceMap
+ ", aliasSet=" + aliasSet + ", array=" + Arrays.toString(array) + "]";
}
}
1.2配置Bean
在Spring配置文件中,使用實現(xiàn)類ComplexUser配置Bean的兩個實例楞陷。
<!-- 使用構(gòu)造方法注入方式裝配ComplexUser實例user1 -->
<bean id="user1" class="assemble.ComplexUser">
<constructor-arg index="0" value="cxk" />
<constructor-arg index="1">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>籃球</value>
</list>
</constructor-arg>
<constructor-arg index="2">
<map>
<entry key="dalian" value="大連" />
<entry key="shenyang" value="沈陽" />
<entry key="shanghia" value="上海" />
</map>
</constructor-arg>
<constructor-arg index="3">
<set>
<value>100</value>
<value>101</value>
<value>102</value>
</set>
</constructor-arg>
<constructor-arg index="4">
<array>
<value>aaaaa</value>
<value>bbbbb</value>
</array>
</constructor-arg>
</bean>
<!-- 使用屬性setter方法注入方式裝配ComplexUser實例user2 -->
<bean id="user2" class="assemble.ComplexUser">
<property name="uname" value="wtf" />
<property name="hobbyList">
<list>
<value>看書</value>
<value>學(xué)spring</value>
</list>
</property>
<property name="residenceMap">
<map>
<entry key="dalian" value="大連" />
<entry key="shenyang" value="沈陽" />
<entry key="shanghia" value="上海" />
</map>
</property>
<property name="aliasSet">
<set>
<value>103</value>
<value>104</value>
<value>105</value>
</set>
</property>
<property name="array">
<array>
<value>ccccc</value>
<value>ddddd</value>
</array>
</property>
</bean>
1.3測試基于XML配置的裝配方式
在ch3應(yīng)用的test包中怔鳖,創(chuàng)建測試類TestAssemble
TestAssemble.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import assemble.ComplexUser;
public class TestAssemble {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml");
ComplexUser u1 = (ComplexUser)appCon.getBean("user1");
System.out.println(u1);
ComplexUser u2 = (ComplexUser)appCon.getBean("user2");
System.out.println(u2);
}
}
運行結(jié)果
2.基于注解的裝配
在Spring框架中定義了一些列的注解,常用注解的使用見3.5+固蛾;
下面通過一個實例講解@Component()
注:在Spring 4.0以上版本结执,配置注解指定包中的注解進行掃描前,需要事先導(dǎo)入Spring Aop的JAR包spring-aop-5.0.2.RELEASE.jar
目錄結(jié)構(gòu)
2.1創(chuàng)建Bean的實現(xiàn)類
在ch3應(yīng)用src目錄中艾凯,創(chuàng)建包annotation献幔,在annotation包下創(chuàng)建類AnnotationUser.java。
AnnotationUser.java
package annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component()
/*
* 相當(dāng)于@Component("annotationUser")或
* @Component(value="annotationUser"),annotationUser為Bean的id趾诗,默認為類名首字母的小寫
*/
public class AnnotationUser {
@Value("基于注解裝配")
private String uname;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
@Override
public String toString() {
return "AnnotationUser [uname=" + uname + "]";
}
}
2.2配置注解
現(xiàn)在有了Bean的實現(xiàn)類蜡感,但還不能進行測試,因為Spring容器并不知道去哪里掃描Bean對象恃泪。需要在配置文件中配置注解郑兴,這里我們在src目錄中新建一個annotationContext.xml,注解配置方式如下:
annotationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用context命名空間贝乎,通過Spring掃描指定包下所有的Bean實現(xiàn)類情连,通過注釋解析 -->
<context:component-scan base-package="annotation"/>
</beans>
2.3測試Bean實例
在ch3英語的test包中,創(chuàng)建測試類TestAnnotation糕非,具體代碼如下
TestAnnotation.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import annotation.AnnotationUser;
import assemble.ComplexUser;
public class TestAnnotation {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext appCon = new ClassPathXmlApplicationContext("annotationContext.xml");
AnnotationUser au = (AnnotationUser)appCon.getBean("annotationUser");
System.out.println(au.getUname());
}
}
運行結(jié)果