一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Spring框架---IOC裝配Bean

詳解Spring框架---IOC裝配Bean

2020-08-25 10:49雨點的名字 Java教程

本篇文章主要介紹了詳解Spring框架---IOC裝配Bean,提供了三種方式實例化Bean,具有一定的參考價值,有興趣的可以了解一下。

IOC裝配Bean

(1)Spring框架Bean實例化的方式提供了三種方式實例化Bean

  1. 構造方法實例化(默認無參數,用的最多)
  2. 靜態工廠實例化
  3. 實例工廠實例化

下面先寫這三種方法的applicationContext.xml配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
  <!-- Bean的三種實例化方式=================== -->
   <!-- 2.1 使用無參的構造器 -->
   <bean id="bean1" class="com.study.spring.b_instance.Bean1"></bean>
   <!-- 2.2使用靜態工廠方法 factory-method 是工廠提供的靜態方法 -->
   <bean id="bean2" class="com.study.spring.b_instance.Bean2" factory-method="createInstance"></bean>
   <!-- 2.3配置實例化工廠的方法 -->
   <bean id="bean3Factory" class="com.study.spring.b_instance.Bean3Factory"></bean>
   <bean id="bean3" factory-bean="bean3Factory" factory-method="getInstance"></bean>
  <!-- end.Bean的三種實例化方式==================== -->

Bean1類

?
1
2
3
4
public class Bean1 {
  
  //必須提供無參的構造函數 系統有默認無參的構造函數
}

Bean2類

?
1
2
3
4
5
6
7
8
9
10
public class Bean2 {
  private static Bean2 Bean2 = new Bean2();
 
  private Bean2() {
  }
 
  public static Bean2 createInstance() {
    return Bean2;
  }
}

Bean3類

?
1
2
3
public class Bean3 {
 
}

Bean3Factory類

?
1
2
3
4
5
6
7
8
9
10
public class Bean3Factory {
  
  private Bean3Factory(){
    
  }
   
  public Bean3 getInstance(){
    return new Bean3();
  }
}

測試類InstanceDemo

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InstanceDemo {
  
  //實例化工廠方法
  @Test
  public void demo3(){
    //加載配置文件 創建工廠
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
    Bean3 bean3 =(Bean3) applicationContext.getBean("bean3");
    System.out.println(bean3);
    
  }
  
  //靜態工廠方法
  @Test
  public void demo2(){
    //加載配置文件 創建工廠
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
    Bean2 bean2 =(Bean2) applicationContext.getBean("bean2");
    System.out.println(bean2);
    
  }
  //構造方法得到bean對象
  @Test
  public void demo1(){
    //加載配置文件 創建工廠
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
    Bean1 bean1 =(Bean1) applicationContext.getBean("bean1");
    System.out.println(bean1);
    
  }
}
/*
 * 這三個都得到類似于com.study.spring.b_instance.Bean1@7229c204 的內存地址
 */

 (2).Bean的其他配置:

一般情況下,裝配一個Bean時,通過指定一個id屬性作為Bean的名稱

id 屬性在IoC容器中必須是唯一的

id 的命名要滿足XML對ID屬性命名規范 必須以字母開始,可以使用字母、數字、連字符、下劃線、句話、冒號

如果Bean的名稱中含有特殊字符,就需要使用name屬性 例如: <bean name="#person" class="cn.itcast.bean.Person"/>

因為name屬性可以相同,所以后出現Bean會覆蓋之前出現的同名的Bean

id和name的區別:

id遵守XML約束的id的約束.id約束保證這個屬性的值是唯一的,而且必須以字母開始,可以使用字母、數字、連字符、下劃線、句話、冒號

name沒有這些要求

如果bean標簽上沒有配置id,那么name可以作為id.

Bean的scope屬性

?
1
2
3
<!-- 3.Bean的scope屬性==================== -->
  <bean id="product" class="com.study.spring.c_scope.Product" scope="singleton"></bean>
<!-- end.Bean的scope屬性=========== -->

 * singleton :單例的.(默認的值.)

 * prototype :多例的.

* request :web開發中.創建了一個對象,將這個對象存入request范圍,request.setAttribute();

* session :web開發中.創建了一個對象,將這個對象存入session范圍,session.setAttribute();

* globalSession :一般用于Porlet應用環境.指的是分布式開發.不是porlet環境,globalSession等同于session;

3.Bean屬性的依賴注入

前面已經知道如何獲得對象,那我們接下來要知道如果給對象對象的屬性賦值。

詳解Spring框架---IOC裝配Bean

下面通過舉例說明: 

Car 類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Car {
 
  private String name;
 
  private double price;
 
  public Car(String name, double price) {
    super();
    this.name = name;
    this.price = price;
  }
 
  @Override
  public String toString() {
    return "Car [name=" + name + ", price=" + price + "]";
  }
}

Car2類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Car2 {
  private String name;
 
  private double price;
 
  public void setName(String name) {
    this.name = name;
  }
 
  public void setPrice(double price) {
    this.price = price;
  }
 
  @Override
  public String toString() {
    return "Car2 [name=" + name + ", price=" + price + "]";
  }
 
}

CarInfo類

?
1
2
3
4
5
6
7
8
9
10
public class CarInfo {
  
  public String getName(){
    return "哈弗H6";
  }
  
  public double caculatePrice(){
    return 110000;
  }
}

CollectionBean類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
 
public class CollectionBean {
  private String name;
 
  private Integer age;
 
  private List<String> hobbies;
 
  private Set<Integer> numbers;
 
  private Map<String, String> map;
 
  private Properties properties;
 
  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 List<String> getHobbies() {
    return hobbies;
  }
 
  public void setHobbies(List<String> hobbies) {
    this.hobbies = hobbies;
  }
 
  public Set<Integer> getNumbers() {
    return numbers;
  }
 
  public void setNumbers(Set<Integer> numbers) {
    this.numbers = numbers;
  }
 
  public Map<String, String> getMap() {
    return map;
  }
 
  public void setMap(Map<String, String> map) {
    this.map = map;
  }
 
  public Properties getProperties() {
    return properties;
  }
 
  public void setProperties(Properties properties) {
    this.properties = properties;
  }
 
  @Override
  public String toString() {
    return "CollectionBean [name=" + name + ", age=" + age + ", hobbies=" + hobbies + ", numbers=" + numbers
        + ", map=" + map + ", properties=" + properties + "]";
  }
  
}

Employee類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Employee {
 
  private String name;
 
  private Car2 car2;
 
  public void setName(String name) {
    this.name = name;
  }
 
  public void setCar2(Car2 car2) {
    this.car2 = car2;
  }
 
  @Override
  public String toString() {
    return "Employee [name=" + name + ", car2=" + car2 + "]";
  }
 
}

TestDi測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestDi {
  
  @Test
  public void demo6() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 
    CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
 
    System.out.println(collectionBean);
  }
  
  
  @Test
  public void demo5() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 
    Car2 car2 = (Car2) applicationContext.getBean("car2_2");
 
    System.out.println(car2);
  }
  
  
  @Test
  public void demo4() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 
    Employee e = (Employee) applicationContext.getBean("employee2");
 
    System.out.println(e);
  }
  
  @Test
  public void demo3() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 
    Employee e = (Employee) applicationContext.getBean("employee");
 
    System.out.println(e);
  }
 
  @Test
  public void demo2() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 
    Car2 car2 = (Car2) applicationContext.getBean("car2");
 
    System.out.println(car2);
  }
 
  @Test
  public void demo1() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 
    Car car = (Car) applicationContext.getBean("car");
 
    System.out.println(car);
  }
}

上面這幾個類都不是最主要的,我們主要是來看配置文件怎么寫,這才是最關鍵的:

applicationContext.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  
  
  <!-- Bean的依賴注入=========== -->
   <!-- 4.1構造器注入 -->
    <bean id="car" class="com.study.spring.e_di.Car">
      <!-- 方式一.根據索引的位置 -->
      <!-- <constructor-arg index="0" value="保時捷"></constructor-arg>
       <constructor-arg index="1" value="1500000"></constructor-arg> -->
       <!-- 方式二.根據名字配置 -->
       <!-- <constructor-arg name="name" value="寶馬"></constructor-arg>
       <constructor-arg name="price" value="500000"></constructor-arg> -->
       <!-- 方式三.根據類型配置 -->
       <constructor-arg type="java.lang.String" value="奔馳"></constructor-arg>
       <constructor-arg type="double" value="600000"></constructor-arg>
    </bean>
    
   <!-- 4.2setter方法中注入 -->
   <bean id="car2" class="com.study.spring.e_di.Car2">
     <property name="name" value="雪佛蘭"></property>
     <property name="price" value="100000"></property>
   </bean>
   
   <bean id="employee" class="com.study.spring.e_di.Employee">
     <property name="name" value="張三"></property>
     <property name="car2" ref="car2"></property>
   </bean>
   
   <!-- 引用p命名空間 --><!-- 如果要引用p命名,那在最上面sxd中就要配置 xmlns:p="http://www.springframework.org/schema/p"-->
    <bean id="car22" class="com.study.spring.e_di.Car2" p:name="寶馬" p:price="500000">
   </bean>
   <bean id="employee2" class="com.study.spring.e_di.Employee" p:name="李四" p:car2-ref="car22"></bean>
   
    <!-- 引入spEL表達式 -->
   <bean id="carInfo" class="com.study.spring.e_di.CarInfo"></bean>
    <bean id="car2_2" class="com.study.spring.e_di.Car2">
      <property name="name" value="#{carInfo.name}"></property>
      <property name="price" value="#{carInfo.caculatePrice()}"></property>
    </bean>
    
   <!-- 復雜屬性的依賴注入 -->
    <bean id="collectionBean" class="com.study.spring.e_di.CollectionBean">
      <!-- 簡單屬性的注入 -->
      <property name="name" value="歸谷"></property>
      <property name="age" value="12"></property>
      <!-- 注入list集合 -->
       <property name="hobbies">
         <list>
           <value>吃飯</value>
           <value>睡覺</value>
           <value>敲代碼</value>
         </list>
       </property>
       
       <!-- 注入set集合 -->
       <property name="numbers">
         <set>
           <value>10</value>
           <value>20</value>
           <value>30</value>
           <value>40</value>
           <value>50</value>
         </set>
       </property>
       <!-- 注入map集合 -->
       <property name="map">
         <map>
           <entry key="birthday" value="2017-1-1"></entry>
           <entry key="address" value="杭州西湖"></entry>
           <entry key="sex" value="female"></entry>
         </map>
       </property>
       
       <!-- 注入Properties -->
       <property name="properties">
         <props>
           <prop key="compamy">杭州歸谷</prop>
           <prop key="pnum">200</prop>
         </props>
       </property>
    </bean>
    
  <!-- end Bean的依賴注入============ -->
  <import resource="classpath:bean1.xml"/>   
  <import resource="classpath:bean2.xml"/>   
  <!-- 這里導入是指如果在src下還有其它的beans.xml我們可以這樣去調用 -->
    
</beans>

有關applicationContext.xml這個配置文件里的內容一定要看懂,我寫的還是比較基礎和全面的。

有關命名空間p的使用我這里在解釋下:   

p:<屬性名>="xxx" 引入常量值

p:<屬性名>-ref="xxx" 引用其它Bean對象

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/qdhxhz/p/6511887.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人午夜片一一在线观看 | 亚洲国产成人久久精品hezyo | 欧美日韩国产手机在线观看视频 | 国产乱码免费卡1卡二卡3卡四 | 国产精品视频视频久久 | 隔壁老王国产精品福利 | 极品丝袜乱系列在线阅读 | 欧美白虎逼 | 国产全部视频 | 欧美怡红院视频一区二区三区 | 男男双性生子产乳高辣h | 亚洲国产精品久久久久久 | 色婷婷综合久久久 | 午夜福利视频极品国产83 | 亚洲第一天堂无码专区 | 俄罗斯大逼 | 成在线人免费视频一区二区三区 | 欧美a级在线 | 久久中文字幕免费高清 | 色网在线视频 | 精品久久久久香蕉网 | 2020年精品国产午夜福利在线 | 国产欧美一区二区三区精品 | 亚洲AV永久无码精品老司机蜜桃 | 丰满岳乱妇在线观看视频国产 | 久久综久久美利坚合众国 | 国产精品污双胞胎在线观看 | 五月桃花网婷婷亚洲综合 | 倩女还魂在线观看完整版免费 | 日韩亚洲国产欧美精品 | 色777777女人色 | 色老妈 | 亚洲精品资源 | 亚洲色图欧美偷拍 | 欧美日韩国产最新一区二区 | 亚洲欧美日韩综合一区久久 | 特级老女人淫片高清视频 | 亚洲成色爱我久久 | 91国产在线播放 | 欧美成人精品福利在线视频 | 国产一区二区免费福利片 |