Sping4之依賴註入
阿新 • • 發佈:2017-09-24
錘子 getbean 構造 sch ner cto spin ret work
Spring的依賴註入可以是我們不需要去管理bean,網上看到一個回答很精辟:
現在你需要買一把錘子:
1、自己做一把,解釋成java就是,調用者創建被調用著,也就是自己去創造一個造錘子的方法,然後自己調用;
2、自己找到生產錘子的工廠,然後問工廠買。就是工廠模式;
3、可以打電話給商店,讓商店給自己送一把錘子過來,這就是sping的依賴註入;
第一種方法調用者創建被調用者,兩者之前無法實現松耦合;
第二種方法調用者無須關心被調用者具體實現過程,只需要找到符合某種標準(接口)的實例,即可使用;
第三種調用者無須自己定位工廠,程序運行到需要被調用者時,系統自動提供被調用者實例。調用者和被調用者通過spring管理。他們的關系由spring維護
model
package com.hongcong.model; public class People { private int id; private String name; private int age; public People() { super(); // TODO Auto-generated constructor stub } public People(int id, String name, int age) { super();this.id = id; this.name = name; this.age = age; } 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; }public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--屬性註入 --> <bean id="people1" class="com.hongcong.model.People"> <property name="id" value="1"/> <property name="name" value="小一"/> <property name="age" value="11"/> </bean> <!-- 構造方法類型註入 --> <bean id="people2" class="com.hongcong.model.People"> <constructor-arg type="int" value="2"></constructor-arg> <constructor-arg type="String" value="小二"></constructor-arg> <constructor-arg type="int" value="22"></constructor-arg> </bean> <!-- 構造方法順序註入 --> <bean id="people3" class="com.hongcong.model.People"> <constructor-arg index="0" value="3"></constructor-arg> <constructor-arg index="1" value="小三"></constructor-arg> <constructor-arg index="2" value="33"></constructor-arg> </bean> <!-- 工廠註入 --> <bean id="peopleFactory" class="com.hongcong.factory.PeopleFactory"></bean> <bean id="people4" factory-bean="peopleFactory" factory-method="CreatePeople"></bean> <bean id="people5" class="com.hongcong.factory.PeopleFactory" factory-method="CreatePeopleByStatic"></bean> </beans>
執行類
package com.hongcong.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hongcong.model.People; public class test { public static void main(String[] args) { ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("beans.xml"); //屬性註入 People people1 = (People)ca.getBean("people1"); System.out.println(people1); //構造方法type註入 People people2 = (People)ca.getBean("people2"); System.out.println(people2); //構造方法順序註入 People people3 = (People)ca.getBean("people3"); System.out.println(people3); //非靜態工廠 People people4 = (People)ca.getBean("people4"); System.out.println(people4); //靜態工廠註入 People people5 = (People)ca.getBean("people5"); System.out.println(people5); } }
工廠
package com.hongcong.factory; import com.hongcong.model.People; public class PeopleFactory { public People CreatePeople(){ People people = new People(); people.setId(4); people.setName("小四"); people.setAge(44); return people; } public static People CreatePeopleByStatic(){ People people = new People(); people.setId(5); people.setName("小五"); people.setAge(55); return people; } }
Sping4之依賴註入