依赖注入的两种方式:构造器注入&getset注入
演示代码结构:
HelloBean 无构造函数,有getset方法。
package com.glodon.springdemo1;
public class HelloBean {
private HelloBeanFeature1 helloBeanFeature1;
public void setHelloBeanFeature1(HelloBeanFeature1 helloBeanFeature1) {
this.helloBeanFeature1 = helloBeanFeature1;
}
public HelloBeanFeature1 getHelloBeanFeature1() {
return helloBeanFeature1;
}
}
特征1 有构造函数,有getset方法
package com.glodon.springdemo1;
import java.util.List;
public class HelloBeanFeature1 {
public HelloBeanFeature1(int num, String feature, List<String> list) {
this.num = num;
this.feature = feature;
this.list = list;
}
private int num;
private String feature;
private List<String> list;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getFeature() {
return feature;
}
public void setFeature(String feature) {
this.feature = feature;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
特征2 有构造函数,有getset方法
package com.glodon.springdemo1;
import java.util.List;
public class HelloBeanFeature2 {
private String featureName;
private String featureNum;
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public HelloBeanFeature2() {
}
public String getFeatureName() {
return featureName;
}
public void setFeatureName(String featureName) {
this.featureName = featureName;
}
public String getFeatureNum() {
return featureNum;
}
public void setFeatureNum(String featureNum) {
this.featureNum = featureNum;
}
public HelloBeanFeature2(String featureName, String featureNum, List<String> list) {
this.featureName = featureName;
this.featureNum = featureNum;
this.list = list;
}
}
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="helloBean" class="com.glodon.springdemo1.HelloBean">
<property name="helloBeanFeature1" ref="helloBeanFeature1"/>
</bean>
<bean id="helloBeanFeature1" class="com.glodon.springdemo1.HelloBeanFeature1">
<constructor-arg name="feature" value="myfature"/>
<constructor-arg name="num" value="12"/>
<constructor-arg name="list">
<list>
<value>my</value>
<value>name</value>
<value>is</value>
<value>zhangshuo</value>
</list>
</constructor-arg>
</bean>
<util:list id="featureList">
<value>my</value>
<value>feature</value>
<value>zhangshuo</value>
</util:list>
<bean id="helloBeanFeature2" class="com.glodon.springdemo1.HelloBeanFeature2"
p:featureName="zhangshuo" p:featureNum="26" p:list-ref="featureList"/>
</beans>
helloBean与helloBeanFeature1呈现了两种不同的注入方式,helloBean为getset注入,helloBeanFeature1为构造器注入。
构造器注入必须要求类具备相应的构造器。
helloBeanFeature2采用的也是getset注入,只是其使用的是命名空间的方式,命名空间是一种简单的写法。
P:表示property name =value; <util:>命名空间包含list set map等一堆元素,它可以被写作一个引用。