Spring框架(2)---IOC装配Bean(xml配置方式)

IOC装配Bean

(1)Spring框架Bean实例化的方式提供了三种方式实例化Bean
    构造方法实例化(默认无参数,用的最多)
    静态工厂实例化
    实例工厂实例化

下面先写这三种方法的applicationContext.xml配置文件:

复制代码
 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     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 8        
 9     <!-- Bean的三种实例化方式================================================================================ -->    
10       <!-- 2.1  使用无参的构造器 -->
11      <bean id="bean1" class="com.study.spring.b_instance.Bean1"></bean>
12       <!-- 2.2使用静态工厂方法  factory-method 是工厂提供的静态方法 -->
13      <bean id="bean2" class="com.study.spring.b_instance.Bean2" factory-method="createInstance"></bean>
14      <!-- 2.3配置实例化工厂的方法 -->
15      <bean id="bean3Factory" class="com.study.spring.b_instance.Bean3Factory"></bean>
16      <bean id="bean3" factory-bean="bean3Factory" factory-method="getInstance"></bean>
17     <!-- end.Bean的三种实例化方式================================================================================ -->    
复制代码
Bean1类
public class Bean1 {
    
    //必须提供无参的构造函数  系统有默认无参的构造函数
}
Bean2类
复制代码
public class Bean2 {
    private static Bean2 Bean2 = new Bean2();

    private Bean2() {
    }

    public static Bean2 createInstance() {
        return Bean2;
    }
}
复制代码

 Bean3类

public class Bean3 {

}

Bean3Factory类

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

测试类InstanceDemo

复制代码
 1 import org.junit.Test;
 2 import org.springframework.context.ApplicationContext;
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class InstanceDemo {
 6     
 7     //实例化工厂方法
 8     @Test
 9     public void demo3(){
10        //加载配置文件 创建工厂
11         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
12         
13         Bean3 bean3 =(Bean3) applicationContext.getBean("bean3");
14         System.out.println(bean3);
15         
16     }
17     
18     //静态工厂方法
19     @Test
20     public void demo2(){
21        //加载配置文件 创建工厂
22         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
23         
24         Bean2 bean2 =(Bean2) applicationContext.getBean("bean2");
25         System.out.println(bean2);
26         
27     }
28     //构造方法得到bean对象
29     @Test
30     public void demo1(){
31        //加载配置文件 创建工厂
32         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
33         
34         Bean1 bean1 =(Bean1) applicationContext.getBean("bean1");
35         System.out.println(bean1);
36         
37     }
38 }
39 /*
40  * 这三个都得到类似于com.study.spring.b_instance.Bean1@7229c204 的内存地址
41  */
复制代码

(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.

  Beanscope属性

  <!-- 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属性的依赖注入

 前面已经知道如何获得对象,那我们接下来要知道如果给对象对象的属性赋值。

下面通过举例说明:

  Car 类
  Car2类
  CarInfo类
  CollectionBean类
  Employee类
  TestDi测试类
上面这几个类都不是最主要的,我们主要是来看配置文件怎么写,这才是最关键的: 

applicationContext.xml
复制代码
 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     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 8     
 9     
10     <!-- Bean的依赖注入===================================================================================== -->    
11      <!-- 4.1构造器注入 -->
12         <bean id="car" class="com.study.spring.e_di.Car">
13             <!-- 方式一.根据索引的位置 -->
14             <!-- <constructor-arg index="0" value="保时捷"></constructor-arg>
15              <constructor-arg index="1" value="1500000"></constructor-arg> -->
16              <!-- 方式二.根据名字配置 -->
17              <!-- <constructor-arg name="name" value="宝马"></constructor-arg>
18              <constructor-arg name="price" value="500000"></constructor-arg> -->
19              <!-- 方式三.根据类型配置 -->
20              <constructor-arg type="java.lang.String" value="奔驰"></constructor-arg>
21              <constructor-arg type="double" value="600000"></constructor-arg>
22         </bean>
23         
24       <!-- 4.2setter方法中注入 -->  
25       <bean id="car2" class="com.study.spring.e_di.Car2">
26           <property name="name" value="雪佛兰"></property>
27           <property name="price" value="100000"></property>
28       </bean>
29       
30       <bean id="employee" class="com.study.spring.e_di.Employee">
31           <property name="name" value="张三"></property>
32           <property name="car2" ref="car2"></property>
33       </bean>
34       
35       <!-- 引用p命名空间 --><!-- 如果要引用p命名,那在最上面sxd中就要配置  xmlns:p="http://www.springframework.org/schema/p"-->
36        <bean id="car22" class="com.study.spring.e_di.Car2" p:name="宝马" p:price="500000">
37       </bean>
38       <bean id="employee2" class="com.study.spring.e_di.Employee" p:name="李四" p:car2-ref="car22"></bean>
39       
40         <!-- 引入spEL表达式 -->
41       <bean id="carInfo" class="com.study.spring.e_di.CarInfo"></bean>
42         <bean id="car2_2" class="com.study.spring.e_di.Car2">
43             <property name="name" value="#{carInfo.name}"></property>
44             <property name="price" value="#{carInfo.caculatePrice()}"></property>
45         </bean>
46         
47      <!-- 复杂属性的依赖注入 -->    
48         <bean  id="collectionBean" class="com.study.spring.e_di.CollectionBean">
49             <!-- 简单属性的注入 -->
50             <property name="name" value="归谷"></property>
51             <property name="age" value="12"></property>
52             <!-- 注入list集合 -->
53              <property name="hobbies">
54                  <list>
55                      <value>吃饭</value>
56                      <value>睡觉</value>
57                      <value>敲代码</value>
58                  </list>
59              </property>
60              
61              <!-- 注入set集合 -->
62              <property name="numbers">
63                  <set>
64                      <value>10</value>
65                      <value>20</value>
66                      <value>30</value>
67                      <value>40</value>
68                      <value>50</value>
69                  </set>
70              </property>
71              <!-- 注入map集合 -->
72              <property name="map">
73                  <map>
74                      <entry key="birthday" value="2017-1-1"></entry>
75                      <entry key="address" value="杭州西湖"></entry>
76                      <entry key="sex" value="female"></entry>
77                  </map>
78              </property>
79              
80              <!-- 注入Properties -->
81              <property name="properties">
82                  <props>
83                      <prop key="compamy">杭州余杭未来科技城</prop>
84                      <prop key="pnum">200</prop>
85                  </props>
86              </property>
87         </bean>
88         
89     <!-- end  Bean的依赖注入===================================================================================== -->    
90     <import resource="classpath:bean1.xml"/>        
91     <import resource="classpath:bean2.xml"/>        
92     <!-- 这里导入是指如果在src下还有其它的beans.xml我们可以这样去调用 -->    
93         
94 </beans>
复制代码

 

有关applicationContext.xml这个配置文件里的内容一定要看懂,我写的还是比较基础和全面的。

有关命名空间p的使用我这里在解释下:    

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值