定義:通過構造函數來完成依賴關系的設定
優缺點:
在構造對象的同時,完成依賴關系的建立
如果關聯的對象很多,那和不得不在構造方法上加入過多的參數
基中有index:如果指定索引從0開始,type用來指定類型
實體類:
package com.pb.entity;/** * 班級類 * @author Administrator * */public class Grade { private int id; //班級編號 private String name; //班級名稱 public Grade() { super(); // TODO Auto-generated constructor stub } public Grade(int id, String name) { super(); this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }package com.pb.entity;/** * 學生類 * @author Administrator * */public class Student { private String name; //學生名稱 private Integer age; //學生年齡 private Grade grade; //所屬班級 public Student() { super(); } public Student(String name, Integer age, Grade grade) { super(); this.name = name; this.age = age; this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Grade getGrade() { return grade; } public void setGrade(Grade grade) { this.grade = grade; } }使用構造方法注入
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!--班級類 --><bean id="grade" class="com.pb.entity.Grade"><!-- 使用構造方法注入 --><constructor-arg><value>1001</value></constructor-arg><constructor-arg><value>計算應用一班</value></constructor-arg></bean><!--學生類 --><bean id="student" class="com.pb.entity.Student"><!-- 使用構造方法注入 --><constructor-arg><value>張三</value></constructor-arg><constructor-arg><value>23</value></constructor-arg><!-- 使用ref注入班級bean --><constructor-arg ref="grade"></constructor-arg></bean></beans>
測試類:
package com.pb.demo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.pb.entity.Student;public class Demo1 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); Student stu = context.getBean("student", Student.class); System.out.println("學生姓名:" + stu.getName() + "學生年齡:" + stu.getAge() + "學生班級編號: " + stu.getGrade().getId() + "學生班級名稱: " + stu.getGrade().getName()); }}二、屬性注入其它不變只更改配置文件
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!--班級類 --><bean id="grade" class="com.pb.entity.Grade"><property name="id"><value>1001</value></property><property name="name" value="計算機應用一班"></property></bean><!-- 學生類 --><bean id="student" class="com.pb.entity.Student"><property name="name" value="張三" /><property name="age" value="18"/><property name="grade" ref="grade"/></bean></beans>三、注入NULL和空字符串值
<value></value>表示空字符串
<null></null>表示為null值
<bean id="student" class="com.pb.entity.Student"><!-- 姓名注入空字符串 <value></value>表示空字符串 --><property name="name"><value></value></property><!--年齡注入為NULL值 --><property name="age"><null></null></property><property name="grade" ref="grade"/></bean>四、使用p 命名空間注入bean官方推薦的注入方式
需要在XML上加入
xmlns:p="http://www.springframework.org/schema/p"
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!--班級類 使用p命名空間注入 --> <bean id="grade" class="com.pb.entity.Grade" p:id="1001" p:name="外語一班"> </bean> <!-- 學生類 使用p命名空間注入 --> <bean id="student" class="com.pb.entity.Student" p:name="張三" p:age="23" p:grade-ref="grade"> </bean></beans>
效果一目了然
五、自動裝配需要使用autowire屬性來配置
可以在每個bean中使用autowire來配置
也可以在<beans>中使用autowire全局配置表示這個beans下的都使用自動裝配,
缺點:不清晰,有問題比較難以查找
autowire:
no(默認值):不進行自動裝配,必須顯示指定依賴對象
byName: 根據屬性名自動裝配。自動查找與屬性名相同的id,如果找到,則自動注入,否則什么都不做
byType:根據屬性的類型自動裝配,Spring自動查找與屬性類型相同的Bean,如果剛好找到唯一的那個,則自動注入,如果找到多個與屬性類型相同的Bean,則拋出異常,如果沒有找到就什么都不做。
constructor:和byType類似,不過它針對構造方法,如果找到一個Bean和構造方法的參數類型相匹配,則通過構造注入該依賴對象,如果找不到,就拋出異常
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!--班級類 使用p命名空間注入 --> <bean id="grade" class="com.pb.entity.Grade" p:id="1001" p:name="外語一班" > </bean> <!-- 學生類 使用p命名空間注入 --> <bean id="student" class="com.pb.entity.Student" p:name="張三" p:age="23" autowire="byName"> </bean></beans>
自動裝配使得配置文件可以非常簡潔,但同時也造成組件之間的依賴關系不明確,容易引發一些潛在的錯誤,謹慎使用
新聞熱點
疑難解答