Spring框架入门之基于xml文件配置bean详解

关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0)

一、Spring中的依赖注入方式介绍

  依赖注入有三种方式

  • 属性注入
  • 构造方法注入
  • 工厂方法注入(很少使用,不推荐,本文不再介绍)

  属性注入

  通过 setter 方法注入Bean 的属性值或依赖的对象。属性注入使用 <property>元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值 。属性注入是实际应用中最常用的注入方式。HelloWorld类中的setName()方法,对应上边代码中的name属性,例如:把setName()方法名改为setName2(),property中的name属性值为name时则会报错,需要将name属性改为name2。


  构造方法

  构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。
  构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性。使用value属性值或value子节点为属性赋值。可以同时使用索引 index 和type属性对应为哪个属性赋值。index的值表示构造函数中参数的位置。type表示成员属性的类型,例如type=“double”

二、属性注入和构造方法注入详解:

1.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:util="http://www.springframework.org/schema/util"
  6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  7         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
  8     
  9     <!-- 
 10         配置bean
 11         class:bean的全类名,通过反射的方式在IOC容器中创建bean 
 12         id:表示容器中的bean,id唯一
 13     -->
 14     <!-- 通过setter注入配置bean的属性 -->
 15     <bean id="helloWorld" class="me.spring.beans.HelloWorld">
 16         <property name="name" value="Spring"></property>
 17     </bean>
 18     <!-- 通过构造方法配置bean的属性 -->
 19     <bean id="car" class="me.spring.beans.Car">
 20         <constructor-arg value="Audi"></constructor-arg>
 21         <constructor-arg value="ShangHai"></constructor-arg>
 22         <constructor-arg value="300000"></constructor-arg>
 23     </bean>
 24 
 25     <!-- 
 26         使用构造器注入的属性值可以指定参数的类型和参数的位置,以区分重载的构造器
 27         如果字面值包含特殊字符,可以使用<![CDATA[]]>包裹起来
 28         属性值也可以使用value子节点进行配置
 29      -->
 30     <bean id="car2" class="me.spring.beans.Car">
 31         <constructor-arg value="Baoma"></constructor-arg>
 32         <constructor-arg type="java.lang.String">
 33             <value><![CDATA[<Beijing>]]></value>
 34         </constructor-arg>
 35         <constructor-arg value="240" type="int"></constructor-arg>
 36     </bean>
 37     
 38     <!-- 可以使用property的ref属性建立bean之间的引用关系 -->
 39     <bean id="person" class="me.spring.beans.Person">
 40         <property name="name" value="Tom"></property>
 41         <property name="age" value="24"></property>
 42         <!-- 
 43         <property name="car" ref="car2"></property>
 44          -->
 45          
 46         <!--  
 47         <property name="car">
 48             <ref bean="car2"/>
 49         </property>
 50         -->
 51         
 52         <!-- 内部bean,不能被外部引用 -->
 53         <property name="car">
 54             <bean class="me.spring.beans.Car">
 55                 <constructor-arg value="Ford"></constructor-arg>
 56                 <constructor-arg value="ChangAn"></constructor-arg>
 57                 <constructor-arg value="2354395" type="double"></constructor-arg>
 58             </bean>
 59         </property>
 60     </bean>
 61     
 62     <bean id="person2" class="me.spring.beans.Person">
 63         <constructor-arg value="Jerry"></constructor-arg>
 64         <constructor-arg value="25"></constructor-arg>
 65         <constructor-arg ref="car2"></constructor-arg>
 66 
 67         <!-- 测试赋值null -->
 68         <!--  
 69         <constructor-arg><null/></constructor-arg>
 70         -->
 71         <!-- 
 72             为级联属性赋值 
 73             注意:属性需要初始化后才可以为级联属性赋值,和Struts2不同
 74             这里必须依据person的setter和getter方法,不能为car2
 75         -->
 76         <property name="car.price" value="4546"></property>
 77     </bean>
 78     
 79     <!-- 测试如何配置集合属性 -->
 80     <bean id="person3" class="me.spring.beans.collections.Person">
 81         <property name="name" value="Mike"></property>
 82         <property name="age" value="34"></property>
 83         <property name="cars">
 84         <!-- 使用list结点为属性为list的属性赋值 -->
 85             <list>
 86                 <ref bean="car"/>
 87                 <ref bean="car2"/>
 88                 <bean class="me.spring.beans.Car">
 89                     <constructor-arg value="Ford"></constructor-arg>
 90                     <constructor-arg value="ChangAn"></constructor-arg>
 91                     <constructor-arg value="2354395" type="double"></constructor-arg>
 92                 </bean>
 93             </list>
 94         </property>
 95     </bean>
 96     <bean id="newPerson" class="me.spring.beans.collections.NewPerson">
 97         <property name="name" value="Rose"></property>
 98         <property name="age" value="23"></property>
 99         <property name="cars">
100         <!-- 使用map结点及map的entry子节点配置Map类型的成员变量 -->
101             <map>
102                 <entry key="AA" value-ref="car"></entry>
103                 <entry key="BB" value-ref="car2"></entry>
104             </map>
105         </property>
106     </bean>
107     
108     <!-- 配置properties属性值 -->
109     <bean id="dataSource" class="me.spring.beans.collections.DataSource">
110         <property name="properties">
111         <!-- 使用props和prop子节点来为properties属性值赋值 -->
112             <props>
113                 <prop key="user">root</prop>
114                 <prop key="password">123456</prop>
115                 <prop key="jdbcURL">jdbc:mysql://localhost:3306/test</prop>
116                 <prop key="driverClass">com.mysql.jdbc.Driver</prop>
117             </props>
118         </property>
119     </bean>
120     
121     <!-- 配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间 -->
122     <util:list id="cars">
123         <ref bean="car"/>
124         <ref bean="car2"/>
125     </util:list>
126     
127     <bean id="person4" class="me.spring.beans.collections.Person">
128         <property name="name" value="Jack"></property>
129         <property name="age" value="34"></property>
130         <property name="cars" ref="cars"></property>
131     </bean>
132     
133     <!-- 通过p命名空间为bean的属性赋值,需要导入p命名空间,相对于传统的配置较为简洁 -->
134     <bean id="person5" class="me.spring.beans.collections.Person" p:name="Queen" p:age="45" p:cars-ref="cars"></bean>
135 </beans>

2.相关的实体类:

(1)me.spring.beans 包下:

  1 package me.spring.beans;
  2 
  3 public class Car {
  4 
  5     private String brand;
  6     private String corp;
  7     private double price;
  8     private int maxSpeed;
  9     public Car(String brand, String corp, double price) {
 10         super();
 11         this.brand = brand;
 12         this.corp = corp;
 13         this.price = price;
 14     }
 15     
 16     public Car(String brand, String corp, int maxSpeed) {
 17         super();
 18         this.brand = brand;
 19         this.corp = corp;
 20         this.maxSpeed = maxSpeed;
 21     }
 22 
 23     public void setPrice(double price) {
 24         this.price = price;
 25     }
 26     
 27     public double getPrice() {
 28         return price;
 29     }
 30     @Override
 31     public String toString() {
 32         return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
 33     }
 34     
 35 }
 36 
 37 package me.spring.beans;
 38 
 39 public class Person {
 40 
 41     private String name;
 42     private int age;
 43     private Car car;
 44 
 45     public String getName() {
 46         return name;
 47     }
 48 
 49     public void setName(String name) {
 50         this.name = name;
 51     }
 52 
 53     public int getAge() {
 54         return age;
 55     }
 56 
 57     public void setAge(int age) {
 58         this.age = age;
 59     }
 60 
 61     public Car getCar() {
 62         return car;
 63     }
 64 
 65     public void setCar(Car car) {
 66         this.car = car;
 67     }
 68 
 69     public Person() {
 70         
 71     }
 72     public Person(String name, int age, Car car) {
 73         super();
 74         this.name = name;
 75         this.age = age;
 76         this.car = car;
 77     }
 78 
 79     @Override
 80     public String toString() {
 81         return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
 82     }
 83 
 84 }
 85 
 86 package me.spring.beans;
 87 
 88 public class HelloWorld {
 89 
 90     private String name;
 91     public void setName(String name) {
 92         this.name = name;
 93     }
 94     public void hello() {
 95         System.out.println("hello:" + name);
 96     }
 97 }
 98 
 99 package me.spring.beans;
100 
101 import org.springframework.context.ApplicationContext;
102 import org.springframework.context.support.ClassPathXmlApplicationContext;
103 
104 public class Main {
105 
106     public static void main(String[] args) {
107 //        HelloWorld helloWorld = new HelloWorld();
108 //        helloWorld.setName("Spring");
109 //        helloWorld.hello();
110         
111         //1.创建Spring的IOC容器对象
112         //ApplicationContext代表IOC容器
113         //ClassPathXmlApplicationContext是ApplicationContext的实现类,该实现类从类路径下加载配置文件
114         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
115         //2.从IOC容器中获取bean实例
116         //利用id定位到IOC容器中的bean
117         HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
118         //利用类型返回IOC容器中的bean,担忧求IOC容器中只能有一个该类型的bean
119         //HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
120         //3.调用hello方法
121         helloWorld.hello();
122         Car car = (Car) ctx.getBean("car");
123         System.out.println(car);
124         car = (Car) ctx.getBean("car2");
125         System.out.println(car);
126         Person person = (Person) ctx.getBean("person2");
127         System.out.println(person);
128     }
129     
130 }

(2)me.spring.beans.collections 测试集和属性

  1 package me.spring.beans.collections;
  2 
  3 import java.util.List;
  4 
  5 import me.spring.beans.Car;
  6 
  7 public class Person {
  8 
  9     private String name;
 10     private int age;
 11     
 12     private List<Car> cars;
 13 
 14     public String getName() {
 15         return name;
 16     }
 17 
 18     public void setName(String name) {
 19         this.name = name;
 20     }
 21 
 22     public int getAge() {
 23         return age;
 24     }
 25 
 26     public void setAge(int age) {
 27         this.age = age;
 28     }
 29 
 30 
 31     public Person() {
 32         
 33     }
 34 
 35     public List<Car> getCars() {
 36         return cars;
 37     }
 38 
 39     public void setCars(List<Car> cars) {
 40         this.cars = cars;
 41     }
 42 
 43     public Person(String name, int age, List<Car> cars) {
 44         super();
 45         this.name = name;
 46         this.age = age;
 47         this.cars = cars;
 48     }
 49 
 50     @Override
 51     public String toString() {
 52         return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
 53     }
 54 }
 55 
 56 package me.spring.beans.collections;
 57 
 58 import java.util.Map;
 59 
 60 import me.spring.beans.Car;
 61 
 62 public class NewPerson {
 63 
 64     private String name;
 65     private int age;
 66     private Map<String, Car> cars;
 67     public String getName() {
 68         return name;
 69     }
 70     public void setName(String name) {
 71         this.name = name;
 72     }
 73     public int getAge() {
 74         return age;
 75     }
 76     public void setAge(int age) {
 77         this.age = age;
 78     }
 79     public Map<String, Car> getCars() {
 80         return cars;
 81     }
 82     public void setCars(Map<String, Car> cars) {
 83         this.cars = cars;
 84     }
 85     
 86     public NewPerson() {
 87         // TODO Auto-generated constructor stub
 88     }
 89     public NewPerson(String name, int age, Map<String, Car> cars) {
 90         super();
 91         this.name = name;
 92         this.age = age;
 93         this.cars = cars;
 94     }
 95     @Override
 96     public String toString() {
 97         return "NewPerson [name=" + name + ", age=" + age + ", cars=" + cars + "]";
 98     }
 99     
100 }
101 
102 package me.spring.beans.collections;
103 
104 import java.util.Properties;
105 
106 public class DataSource {
107 
108     private Properties properties;
109 
110     public Properties getProperties() {
111         return properties;
112     }
113 
114     public void setProperties(Properties properties) {
115         this.properties = properties;
116     }
117 
118     @Override
119     public String toString() {
120         return "DataSource [properties=" + properties + "]";
121     }
122     
123 }
124 package me.spring.beans.collections;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
         Person person = (Person) ctx.getBean("person5");
        System.out.println(person);
         NewPerson newPerson = (NewPerson) ctx.getBean("newPerson");
        System.out.println(newPerson);
         DataSource dataSource = ctx.getBean(DataSource.class);
         System.out.println(dataSource.getProperties());
    }
}

 

posted on 2018-01-13 16:23  Ryan520  阅读(5342)  评论(0编辑  收藏  举报

导航