IoC 構造器注入
<!-- 聲明TelePhone -->
<bean id="phone" class="model.TelePhone">
<!-- index指定構造器的第幾個參數,name指定參數名,value指定參數值 -->
<constructor-arg name="cpu" index="0" value="高通"></constructor-arg>
<constructor-arg name="ram" index="1" value="迅閃"></constructor-arg>
</bean>
測試方法:
@org.junit.Test
public void test5() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
TelePhone t = ac.getBean("phone",TelePhone.class);
t.show();
}
bean對應的java類 TelePhone.java
/**
* 手機類,模擬構造器注入
* @author haifeng
*
*/
public class TelePhone {
private String cpu;
private String ram;
public TelePhone() {
super();
}
public TelePhone(String cpu, String ram) {
this.cpu = cpu;
this.ram = ram;
}
public void show() {
System.out.println( "TelePhone [cpu:" + cpu + ", ram:" + ram + "]");
}
}
SET方式注入對象:
注入的對象需要在beans中聲明,使用ref進行注入
<!-- 聲明Computer -->
<bean id="student" class="model.Student">
<property name="name" value="haydn"></property>
<property name="computer" ref="computer"></property>
<property name="phone" ref="phone"></property>
</bean>
測試方法:
@org.junit.Test
public void test6() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = ac.getBean("student",Student.class);
stu.show();
}
bean的對應JAVA類,Student.java
/**
* student實體類,模擬注入類對象
* @author haifeng
*
*/
public class Student {
private String name;
private Computer computer;
private TelePhone phone;
public void setName(String name) {
this.name = name;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
public void setPhone(TelePhone phone) {
this.phone = phone;
}
public void show() {
System.out.println("學生姓名:" + name);
computer.show();
phone.show();
}
}
List set map propetries 注入
<!-- 注入集合 -->
<bean id="message" class="bean.Message">
<!-- 字符串注入null值,方式一:不寫該配置,方式2:不寫value,加個null標籤 -->
<property name="name" ><null/></property>
<property name="age" value="23"></property>
<!-- 注入list -->
<property name="friends">
<list>
<value>摩嚴</value>
<value>白子畫</value>
<value>殺阡陌</value>
<value>花千骨</value>
<value>洛十一</value>
<value>糖寶</value>
</list>
</property>
<!-- 注入set -->
<property name="cities">
<set>
<value>北京</value>
<value>上海</value>
</set>
</property>
<!-- 注入MAP -->
<property name="score">
<map>
<entry key="語文" value="98"> </entry>
<entry key="數學" value="99"></entry>
<entry key="英語" value="100"></entry>
</map>
</property>
<!-- 注入配置文件 -->
<property name="dbParams">
<props>
<prop key="url">localhost</prop>
<prop key="username">root</prop>
<prop key="password">123</prop>
</props>
</property>
<!-- 表達式注入,類似EL表達式用#{bean對象名.屬性}標記, -->
<property name="password" value="#{dbProps.password}"></property>
</bean>
<!-- spring標籤注入 ,其他bean中使用ref調用,set Map Propties都可以,使用時需要加入命名空間-->
<util:list id="someList">
<value>摩嚴</value>
<value>白子畫</value>
<value>殺阡陌</value>
<value>花千骨</value>
<value>洛十一</value>
<value>糖寶</value>
</util:list>
<!-- 注入list -->
<property name="friends" ref="someList"></property>
<!-- 加載properties文件創建Properties對象,location="classpath:dbcp.properties" location表示指定文件路徑
classpath表示絕對路徑 -->
<util:properties id="dbProps" location="classpath:dbcp.properties">
</util:properties>
測試類JAVA代碼:
@org.junit.Test
public void test7() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Message msg = ac.getBean("message",Message.class);
msg.show();
}
bean的對應JAVA類,Message.java
/**
* 消息類,模擬各種類型對象注入
* @author haifeng
*
*/
public class Message {
private String name;
private int age;
private List<String> friends;
private Set<String> cities;
private Map<String,Integer> score;
private Properties dbParams;
private String password;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
public void setCities(Set<String> cities) {
this.cities = cities;
}
public void setScore(Map<String, Integer> score) {
this.score = score;
}
public void setDbParams(Properties dbParams) {
this.dbParams = dbParams;
}
public void setPassword(String password) {
this.password = password;
}
public void show() {
System.out.println( "Message [name:" + name + ", age:" + age +", friends"+
friends.toString()+",cities:" +cities.toString()+",score:"+score.toString()+
",properties:"+dbParams.toString()+"password:"+password+"]");
}
/**
* map的遍歷
*/
public void showMap(){
Set<Entry<String,Integer>> map = score.entrySet();
for(Entry<String,Integer> e : map) {
System.out.println(e.getKey()+e.getValue());
}
}
/**
* protries的遍歷
*/
public void showProp(){
Set<Object> keys = dbParams.keySet();
for(Object o : keys) {
System.out.println(dbParams.get(o));
}
}
}
自動裝配(bean對象注入簡配)
autowire:
byName:按名稱匹配,beanid名與setXXX名字匹配植入,否則不注入
byType:按類型匹配,bean的class與類型待注入的bean屬性類型一致時注入,否則不注入
<!-- 自動裝配,autowire:byName根據名稱,byType根據類型,不寫默認會自動裝配,但可讀性很差 -->
<!-- 自動裝配的bean的id需要和待注入的bean的屬性name一致,注入的名字需要和set方法的名字一致(不含set,首字母小寫) -->
<!-- byType和名字沒關係,根據類型自動裝配 -->
<!-- 但是byType的bean中有相同類型的bean出現則會出錯,而且還要以單例模式聲明要注入的bean -->
<bean id="student" class="model.Student" autowire="byName">
<property name="name" value="haydn"></property>
</bean>