JAVA-Spring框架之IOC(DI)-注入方式(1)

spring框架概述
  spring是一個service層的框架,能夠整合許多其 他框架,進行工做
  spring的主要技術是IOC(DI)和AOPjava

  IOC(DI)-控制反轉(依賴注入)AOP - 面向切面編程spring

1. IOC(DI)編程

  所謂的IOC稱之爲控制反轉,簡單來講,就是將創 建對象的權力和對象的生命週期都由spring框架 來管理,今後開發過程當中便再也不關心對象的建立 和生命週期的管理,從而在須要建立對象的時候 ,由spring框架提供,這個由spring框架管理對象的建立和生命週期的機制就是IOC,建立對象的過程當中spring框架能夠根據配置對對象的屬性進行設置,這個過程就是依賴注入(DI)。
app

2.set方法注入框架

  一般的javabean屬性都會被私有化,而對外暴露set和get方法,此時spring能夠經過這樣的set方法將屬性的值注入對象。ide

  spring內置的能夠直接注入的類型:this

 1 package cn.tedu.beans;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.Properties;
 6 import java.util.Set;
 7 
 8 public class Hero {
 9         private int id;
10         private String name;
11         private List<String> jobs;
12         private Set<String> set;
13         private Map<String,String> map;
14         private Properties prop;
15         
16         public void setId(int id) {
17                 this.id = id;
18         }
19 
20         public void setName(String name) {
21                 this.name = name;
22         }
23 
24         public void setJobs(List<String> jobs) {
25                 this.jobs = jobs;
26         }
27 
28         public void setSet(Set<String> set) {
29                 this.set = set;
30         }
31 
32         public void setMap(Map<String, String> map) {
33                 this.map = map;
34         }
35 
36         public void setProp(Properties prop) {
37                 this.prop = prop;
38         }
39 
40         @Override
41         public String toString() {
42                 return "Hero [id=" + id + ", name=" + name + ", jobs=" + jobs
43                                 + ", set=" + set + ", map=" + map + ", prop=" + prop + "]";
44         }
45 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4         xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
 6         >
 7         
 8         <bean id="hero" class="cn.tedu.beans.Hero">
 9                 <property name="id" value="123"></property>
10                 <property name="name" value="亞瑟 "></property>
11                 <property name="jobs">
12                         <list>
13                                 <value>上單</value>
14                                 <value>打野</value>
15                                 <value>輔助</value>
16                                 <value>中單</value>
17                         </list>
18                 </property>
19                 <property name="set">
20                         <set>
21                                 <value>aaa</value>
22                                 <value>bbb</value>
23                                 <value>ccc</value>
24                                 <value>aaa</value>
25                         </set>
26                 </property>
27                 <property name="map">
28                         <map>
29                                  <entry key="addr" value="王者榮耀"></entry>
30                                  <entry key="addr" value="英雄聯盟"></entry>
31                                  <entry key="skill" value="風火輪"></entry>
32                                  <entry key="age" value="19"></entry>
33                         </map>
34                 </property>
35                 <property name="prop">
36                         <props>
37                                 <prop key="k1">v1</prop>
38                                 <prop key="k2">v2</prop>
39                                 <prop key="k3">v3</prop>
40                                 <prop key="k4">v4</prop>
41                         </props>
42                 </property>
43         </bean>
44 
45 </beans>

 

1 @Test
2 /**
3  * SpringDI set方式屬性注入 - Spring內置的可直接注入類型的注入
4  */
5 public void test1(){
6         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
7         Hero hero = (Hero) context.getBean("hero");
8         System.out.println(hero);
9 }

  非spring內置的能夠直接如注入的類型:spa

 1 package cn.tedu.beans;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.Properties;
 6 import java.util.Set;
 7 
 8 public class Hero {
 9         private int id;
10         private String name;
11         private List<String> jobs;
12         private Set<String> set;
13         private Map<String,String> map;
14         private Properties prop;
15         private Dog dog;
16         private Cat cat;
17         
18         
19         public void setId(int id) {
20                 this.id = id;
21         }
22 
23         public void setName(String name) {
24                 this.name = name;
25         }
26 
27         public void setJobs(List<String> jobs) {
28                 this.jobs = jobs;
29         }
30 
31         public void setSet(Set<String> set) {
32                 this.set = set;
33         }
34 
35         public void setMap(Map<String, String> map) {
36                 this.map = map;
37         }
38 
39         public void setProp(Properties prop) {
40                 this.prop = prop;
41         }
42 
43         public void setDog(Dog dog) {
44                 this.dog = dog;
45         }
46 
47         public void setCat(Cat cat) {
48                 this.cat = cat;
49         }
50 
51         @Override
52         public String toString() {
53                 return "Hero [id=" + id + ", name=" + name + ", jobs=" + jobs
54                                 + ", set=" + set + ", map=" + map + ", prop=" + prop + ", dog="
55                                 + dog + ", cat=" + cat + "]";
56         }
57 
58 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4         xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
 6         >
 7         
 8         <bean id="hero" class="cn.tedu.beans.Hero">
 9                 <property name="id" value="123"></property>
10                 <property name="name" value="亞瑟 "></property>
11                 <property name="jobs">
12                         <list>
13                                 <value>上單</value>
14                                 <value>打野</value>
15                                 <value>輔助</value>
16                                 <value>中單</value>
17                         </list>
18                 </property>
19                 <property name="set">
20                         <set>
21                                 <value>aaa</value>
22                                 <value>bbb</value>
23                                 <value>ccc</value>
24                                 <value>aaa</value>
25                         </set>
26                 </property>
27                 <property name="map">
28                         <map>
29                                  <entry key="addr" value="王者榮耀"></entry>
30                                  <entry key="addr" value="英雄聯盟"></entry>
31                                  <entry key="skill" value="風火輪"></entry>
32                                  <entry key="age" value="19"></entry>
33                         </map>
34                 </property>
35                 <property name="prop">
36                         <props>
37                                 <prop key="k1">v1</prop>
38                                 <prop key="k2">v2</prop>
39                                 <prop key="k3">v3</prop>
40                                 <prop key="k4">v4</prop>
41                         </props>
42                 </property>
43                 <property name="dog" ref="dog"></property>
44                 <property name="cat" ref="cat"></property>
45         </bean>
46 
47         <bean id="dog" class="cn.tedu.beans.Dog"></bean>
48         <bean id="cat" class="cn.tedu.beans.Cat"></bean>
49         
50 </beans>

1 @Test
2 /**
3  * SpringDI set方式屬性注入 - 非Spring內置的能夠直接注入類型的注入
4  */
5 public void test2(){
6         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
7         Hero hero = (Hero) context.getBean("hero");
8         System.out.println(hero);
9 }

  3.自動裝配code

    在spring的set方式實現的注入過程當中,支持自動裝配機制,所謂自動裝配機制,會根據要設置的javabean屬性的名字或者類型到spring中自動尋找對應id或類型的<bean>進行設置,從而省去一次配置的過程,簡化了配置。xml

  爲指定的<bean>開啓自動裝配:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4         xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
 6         >
 7         
 8         <!-- 
 9                 autowire設定自動裝配:
10                         byName:根據javabean中須要注入的屬性的名字 ,在spring容器中找對應id的<bean>將該<bean>的對象複製給 當前的屬性 
11                         byType:根據javabean中須要注入的屬性的類型,在spring容器中找對應class類型的<bean>將該<bean>的對象複製給 當前的屬性
12                         **byType方式 根據類型進行匹配,可能匹配到多個<bean>,此時會拋出異常。而byName是經過id來尋找<bean>,id沒有重複,不會有這方面的問題,因此推薦使用byName方式
13          -->
14         <bean id="teacher" class="cn.tedu.beans.Teacher" autowire="byName"></bean>
15         <bean id="dog" class="cn.tedu.beans.Dog"></bean>
16         <bean id="cat" class="cn.tedu.beans.Cat"></bean>
17         
18 </beans>

  爲全局配置自動裝配:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4         xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
 6         default-autowire="byName"
 7         >
 8         
 9         <!-- 
10                 autowire設定自動裝配:
11                         byName:根據javabean中須要注入的屬性的名字 ,在spring容器中找對應id的<bean>將該<bean>的對象複製給 當前的屬性 
12                         byType:根據javabean中須要注入的屬性的類型,在spring容器中找對應class類型的<bean>將該<bean>的對象複製給 當前的屬性
13                         **byType方式 根據類型進行匹配,可能匹配到多個<bean>,此時會拋出異常。而byName是經過id來尋找<bean>,id沒有重複,不會有這方面的問題,因此推薦使用byName方式
14          -->
15         <bean id="teacher" class="cn.tedu.beans.Teacher"></bean>
16         <bean id="dog" class="cn.tedu.beans.Dog"></bean>
17         <bean id="cat" class="cn.tedu.beans.Cat"></bean>
18         
19 </beans>
 1 package cn.tedu.beans;
 2 
 3 public class Teacher {
 4         private Dog dog;
 5         private Cat cat;
 6         public void setDog(Dog dog) {
 7                 this.dog = dog;
 8         }
 9         public void setCat(Cat cat) {
10                 this.cat = cat;
11         }
12         
13         @Override
14         public String toString() {
15                 return "Teacher [dog=" + dog + ", cat=" + cat + "]";
16         }
17 }
1 @Test
2 /**
3  * SpringDI 自動裝配
4  */
5 public void test4(){
6     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
7     Teacher teacher = (Teacher) context.getBean("teacher");
8     System.out.println(teacher);
9 }

  4.基於構造方法的注入:

    對象屬性設置的另外一種方式是在對象建立的過程當中經過構造方法的傳入並設置對象的屬性的。spring也能夠經過這樣的構造方法實現屬性的注入。

 

 1 package cn.tedu.beans;
 2 
 3 public class Student {
 4         private int id;
 5         private String name;
 6         private Dog dog;
 7         
 8         public Student(int id, String name, Dog dog) {
 9                 this.id = id;
10                 this.name = name;
11                 this.dog = dog;
12         }
13 
14         @Override
15         public String toString() {
16                 return "Student [id=" + id + ", name=" + name + ", dog=" + dog + "]";
17         }
18 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4         xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
 6         >
 7         
 8         <bean id="student" class="cn.tedu.beans.Student">
 9                 <!-- 
10                         index:爲構造方法的第幾個參數 進行配置
11                         name:爲構造方法的哪一個名字的參數進行配置
12                                  **index 和 name 能夠配置任何一個或同時配置 但要求一旦配置必須正確
13                                  **推薦優先使用index方式配置 防止沒有源碼形成name沒法匹配到對應參數
14                         type:該構造方法參數的類型
15                         value:該構造方法參數的值 ,用來指定基本值
16                         ref:該構造方法參數的值,用來指定引用其餘bean的值
17                  -->
18                 <constructor-arg index="0" name="id" value="999"/>
19                 <constructor-arg index="1" type="java.lang.String" value="張無忌"/>
20                 <constructor-arg name="dog" ref="dog"/>
21         </bean>
22         
23         <bean id="dog" class="cn.tedu.beans.Dog"></bean>
24         
25 </beans>
1 @Test
2 /**
3  * SpringDI 構造方法方式屬性注入
4  */
5 public void test3(){
6         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
7         Student student = (Student) context.getBean("student");
8         System.out.println(student);
9 }
相關文章
相關標籤/搜索