目录
- 概述:
- spring 使用bean步骤:
- 一、配置形式:
- 二、配置bean的方式:
- 全类名举例:
- 工厂方法(静态工厂和实例工厂方法)
- FactoryBean
- 三、依赖注入的方式
- 1.属性注入xml配置举例
- 2.构造器注入xml配置举例
- 3.属性注入和构造器注入在代码中的使用
- 四、ioc容器:
- 1.ApplicationContext:
- 五、配置bean的各种情况
- 1.如果参数或属性有特殊字符:使用
- 2.如果参数或属性涉及到类的引用:
- 3.赋值为null的情况
- 4.更改级联属性
- 5.如果参数或属性涉及到List
- 6.如果参数或属性涉及到Map
- 7.如果参数或属性涉及到properties
- 8.配置单例的集合bean,以供多个bean进行引用
- 9.标签的使用
- 10.bean的作用域
- 11.bean之间的关系
- 12.外部文件
- 13.spel
- 六、自动装配(不常用)
- 1.什么是自动装配?
- 2.自动装配的常用形式
- 七、生命周期
概述:
spring 使用bean步骤:
步骤一、创建IOC容器(配置bean就发生在这里)
步骤二:从IOC容器获取bean:获取bean可以常用的两种方式:①利用id定位到IOC容器中的bean ②,利用运行时类 (利用类型返回IOC容器中的bean,要求容器中必须只能有一个该类型的bean)
①利用id定位到IOC容器中的bean
1 HelloWorld helloWorld2 = (HelloWorld) applicationContext.getBean("helloWorld2");// 利用id定位到IOC容器中的bean
②利用运行时类
1 HelloWorld helloWorld3 = applicationContext.getBean(HelloWorld.class)
步骤三:调用具体的方法;
一、配置形式:
- 基于xml形式(本篇):有配置文件,并且配置文件时xml形式;
- 基于注解的方式
二、配置bean的方式:
- 通过全类名(反射)(本篇)
- 通过工厂方法(静态工厂和实例工厂方法)
- FactoryBean
全类名举例:
<bean id="helloWorld2" class="com.lixm.configure.HelloWorld">
<property name="name" value="Spring"></property> <!-- name 为属性名 此处属性名为name value为属性的值 此处设置属性name的值为Spring 即属性注入-->
</bean>
com.lixm.configure.HelloWorld即为全类名
注意:该方式为通过反射来实现的,所以com.lixm.configure.HelloWorld必须提供空参构造器
工厂方法(静态工厂和实例工厂方法)
- 静态方法:直接调用某一个类的静态方法就可以返回bean的实例
1 public class StaticCarFactory {
2
3 private static Map<String, Car> cars = new HashMap<>();
4
5 static {
6 cars.put("audi", new Car("audi", 3000000, 80));
7 cars.put("ford", new Car("audi", 4000000, 82));
8 }
9
10 //静态工厂方法
11 public static Car getCar(String name) {
12 return cars.get(name);
13 }
14
15 }
- 实例工厂方法:创建工厂本身,再返回bean的实例
1 public class InstanceCarFactory {
2
3 private static Map<String, Car> cars = null;
4
5 public InstanceCarFactory() {
6 cars = new HashMap<>();
7 cars.put("audi", new Car("audi", 3000000, 80));
8 cars.put("ford", new Car("ford", 4000000, 82));
9 }
10
11 public Car getCar(String brand) {
12 return cars.get(brand);
13 }
14
15 }
- bean的配置
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6 <!-- 通过静态工厂方法来配置bean。注意不是配置静态工厂方法实例,而是配置bean实例 -->
7 <!--
8 factory-method:指向工厂方法的名字
9 constructor-arg:若果工厂方法需要传入参数,则使用constructor-arg来配置参数
10 -->
11 <bean id="car1" class="com.lixm.factory.StaticCarFactory" factory-method="getCar">
12 <constructor-arg value="audi" index="0"></constructor-arg>
13 </bean>
14 <!-- 配置实例工厂方法 -->
15
16 <!--
17 factory-bean:指向实例工厂方法的bean
18 factory-method:指向工厂方法的名字
19 constructor-arg:若果工厂方法需要传入参数,则使用constructor-arg来配置参数
20 -->
21 <bean id="instanceCarFactory" class="com.lixm.factory.InstanceCarFactory" >
22
23 </bean>
24 <bean id="car2" factory-bean="instanceCarFactory" factory-method="getCar">
25 <constructor-arg value="ford" index="0"></constructor-arg>
26 </bean>
27
28 </beans>
- 分别调用工厂方法和实例方法配置的bean的toString方法;
1 public static void main(String[] args) {
2 ApplicationContext context = new ClassPathXmlApplicationContext("bean-factory.xml");
3
4 Car car1 = (Car) context.getBean("car1");
5 System.out.println(car1);
6 Car car2 = (Car) context.getBean("car2");
7 System.out.println(car2);
8 ((ConfigurableApplicationContext) context).close();
9 }
运行结果为:
Car [brand=audi, price=3000000.0, tyrePerimeter=80.0]
Car [brand=ford, price=4000000.0, tyrePerimeter=82.0
FactoryBean
自定义的FactoryBean 需要实现FactoryBean
1 public static void main(String[] args) {
2 ApplicationContext context = new ClassPathXmlApplicationContext("bean-factorybean.xml");
3
4 Car car1 = (Car) context.getBean("car");
5 System.out.println(car1);
6
7 ((ConfigurableApplicationContext) context).close();
8 }
1 public class CarFactoryBean implements FactoryBean<Car> {
2
3 private String brand;
4
5 public String getBrand() {
6 return brand;
7 }
8
9 public void setBrand(String brand) {
10 this.brand = brand;
11 }
12
13 // 返回bean 的对象
14 @Override
15 public Car getObject() throws Exception {
16 // TODO Auto-generated method stub
17 return new Car("bmw", 5000000, 80);
18 }
19
20 // 返回bean的类型
21 @Override
22 public Class<?> getObjectType() {
23 // TODO Auto-generated method stub
24 return Car.class;
25 }
26
27 }
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6 <!-- 通过FactroyBean 来配置bean的shi'l -->
7 <!--
8 class:指向FactoryBean的全类名
9 property :配置FactoryBean的属性
10 但是实际返回的却是 FactoryBean的getObject() 返回的实例
11 -->
12 <bean id="car" class="com.lixm.factorybean.CarFactoryBean" p:brand="benz">
13 </bean>
14
15 </beans>
运行结果为:
Car [brand=bmw, price=5000000.0, tyrePerimeter=80.0]
返回的结果为getObject中的配置的值;
三、依赖注入的方式
- 属性注入
- 构造器注入
附上com.lixm.configure.HelloWorld类
1 package com.lixm.configure;
2
3 public class HelloWorld {
4
5 private String name;
6 private int age;
7
8 public String getName() {
9 return name;
10 }
11
12 public void setName(String name) {
13 this.name = name;
14 }
15
16 public void hello() {
17 System.out.println("hello " + name);
18 }
19
20 public int getAge() {
21 return age;
22 }
23
24 public void setAge(int age) {
25 this.age = age;
26 }
27
28 public HelloWorld() {
29 super();
30 }
31
32 public HelloWorld(String name, int age) {
33 super();
34 this.name = name;
35 this.age = age;
36 }
37
38 @Override
39 public String toString() {
40 return "HelloWorld [name=" + name + ", age=" + age + "]";
41 }
42
43 }
1.属性注入xml配置举例
使用property name为属性名称,value为属性值;
1 <bean id="helloWorld2" class="com.lixm.configure.HelloWorld">
2 <property name="name" value="Spring"></property> <!-- name 为属性名 此处属性名为name value为属性的值 此处设置属性name的值为Spring 即属性注入-->
3 </bean>
2.构造器注入xml配置举例
使用constructor-arg 标签 配置 参数值,使用index设置参数位置;使用type 限定属性类型。有的时候只通过参数位置很难确定具体的构造器,因为构造器重载就是根据参数列表(数量、类型),所以还需要参数类型;
<!-- 通过构造器方法来配置bean 属性可以指定参数的类型和位置 ! 以区分重载的构造器 -->
<bean id="helloWorld3" class="com.lixm.configure.HelloWorld">
<constructor-arg value="lixm" index="0"></constructor-arg>
<constructor-arg value="30" type="int"></constructor-arg>
</bean>
<!-- 通过构造器方法来配置bean -->
<bean id="helloWorld4" class="com.lixm.configure.HelloWorld">
<constructor-arg value="qianzd" index="0"></constructor-arg>
<constructor-arg value="1" index="1"></constructor-arg>
</bean>
属性也可以使用value子节点注入
1 <bean id="helloWorld5" class="com.lixm.configure.HelloWorld">
2 <!-- 如果有特殊字符 的情况可以使用<![CDATA[]]> -->
3 <!-- 属性可以使用value子节点注入-->
4 <constructor-arg index="0">
5 <value> <![CDATA[<qianzd>]]></value>
6 </constructor-arg>
7 <constructor-arg value="1" index="1"></constructor-arg>
8 </bean>
3.属性注入和构造器注入在代码中的使用
1 public class Main {
2 public static void main(String[] args) {
3
4 // 采用spring 方式
5 // 1.创建spring的IOC容器对象
6 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
7 // 2.从IOC容器中获取bean实例
8 HelloWorld helloWorld2 = (HelloWorld) applicationContext.getBean("helloWorld2");// 利用id定位到IOC容器中的bean
9 // 3.调用hello方法
10 System.out.println(helloWorld2.toString());
11
12 HelloWorld helloWorld3 = (HelloWorld) applicationContext.getBean("helloWorld3");// 利用id定位到IOC容器中的bean
13 System.out.println(helloWorld3.toString());
14 HelloWorld helloWorld4 = (HelloWorld) applicationContext.getBean("helloWorld4");// 利用id定位到IOC容器中的bean
15 System.out.println(helloWorld4.toString());
16 ((ConfigurableApplicationContext) applicationContext).close();
17 }
18
19 }
属性注入和构造器注入配置的bean的两种方式的xml文件
1 <?xml versinotallow="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 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
5
6 <!-- id 用来标识class -->
7
8 <!-- 配置bean
9 class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求bean中必须有无参数的构造器
10 id:标识容器中的bean,id唯一;
11 -->
12 <bean id="helloWorld2" class="com.lixm.configure.HelloWorld">
13 <property name="name" value="Spring"></property> <!-- name 为属性名 此处属性名为name value为属性的值 此处设置属性name的值为Spring 即属性注入-->
14 </bean>
15
16
17 <!-- 通过构造器方法来配置bean 属性可以指定参数的类型和位置 ! 以区分重载的构造器 -->
18 <bean id="helloWorld3" class="com.lixm.configure.HelloWorld">
19 <constructor-arg value="lixm" index="0"></constructor-arg>
20 <constructor-arg value="30" type="int"></constructor-arg>
21 </bean>
22 <!-- 通过构造器方法来配置bean -->
23 <bean id="helloWorld4" class="com.lixm.configure.HelloWorld">
24 <constructor-arg value="qianzd" index="0"></constructor-arg>
25 <constructor-arg value="1" index="1"></constructor-arg>
26 </bean>
27
28 </beans>
运行结果:
HelloWorld [name=Spring, age=0]
HelloWorld [name=lixm, age=30]
HelloWorld [name=qianzd, age=1]
四、ioc容器:
ioc容器的作用就是管理这些个时而被需要,时而被抛弃的bean;
常用的ioc容器有:
- ApplicationContext(一般使用这个)(本篇)
- BeanFactory
1.ApplicationContext:
主要实现类是:ClassPathXmlApplicationContext(从类路径下加载配置文件)、FileSystemXmlApplicationContext:从文件系统加载配置文件 和WebApplicationContext(专门为WEB应用而准备的,它允许从相对于web根目录的路径中完成初始化工作)
五、配置bean的各种情况
1.如果参数或属性有特殊字符:使用<![CDATA[]]>
1 public class Main {
2 public static void main(String[] args) {
3
4 // 采用spring 方式
5 // 1.创建spring的IOC容器对象
6 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
7 // 2.从IOC容器中获取bean实例
8 HelloWorld helloWorld5 = (HelloWorld) applicationContext.getBean("helloWorld5");// 利用id定位到IOC容器中的bean
9 System.out.println(helloWorld5.toString());
10 ((ConfigurableApplicationContext) applicationContext).close();
11 }
12
13 }
1 <?xml versinotallow="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 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
5
6 <!-- 通过构造器方法来配置bean 属性可以指定参数的类型和位置 ! 以区分重载的构造器 -->
7 <bean id="helloWorld5" class="com.lixm.configure.HelloWorld">
8 <!-- 如果有特殊字符 的情况可以使用<![CDATA[]]> -->
9 <!-- 属性可以使用value子节点注入-->
10 <constructor-arg index="0">
11 <value> <![CDATA[<qianzd>]]></value>
12 </constructor-arg>
13 <constructor-arg value="1" index="1"></constructor-arg>
14 </bean>
15
16 </beans>
运行结果为:
HelloWorld [name= <qianzd>, age=1]
2.如果参数或属性涉及到类的引用:
①可以使用property的 ref 建立bean直接的引用关系;
②可以使用内部类的方式,内部bean只能在内部使用,不能被外部引用
Person类:
1 package com.lixm.configure;
2
3 public class Person {
4 private String name;
5 private String sex;
6
7 public String getName() {
8 return name;
9 }
10
11 public void setName(String name) {
12 this.name = name;
13 }
14
15 public String getSex() {
16 return sex;
17 }
18
19 public void setSex(String sex) {
20 this.sex = sex;
21 }
22
23 @Override
24 public String toString() {
25 return "Person [name=" + name + ", sex=" + sex + "]";
26 }
27
28 }
Helloworld做出调整:
1 package com.lixm.configure;
2
3 public class HelloWorld {
4
5 private String name;
6 private int age;
7 private Person person;
8
9 public String getName() {
10 return name;
11 }
12
13 public void setName(String name) {
14 this.name = name;
15 }
16
17 public void hello() {
18 System.out.println("hello " + name);
19 }
20
21 public int getAge() {
22 return age;
23 }
24
25 public void setAge(int age) {
26 this.age = age;
27 }
28
29 public Person getPerson() {
30 return person;
31 }
32
33 public void setPerson(Person person) {
34 this.person = person;
35 }
36
37 public HelloWorld() {
38 super();
39 }
40
41 public HelloWorld(String name, int age) {
42 super();
43 this.name = name;
44 this.age = age;
45 }
46
47 public HelloWorld(String name, int age, Person person) {
48 super();
49 this.name = name;
50 this.age = age;
51 this.person = person;
52 }
53
54 @Override
55 public String toString() {
56 return "HelloWorld [name=" + name + ", age=" + age + ", person=" + person + "]";
57 }
58
59
60 }
方式一,使用property的 ref 建立bean直接的引用关系;
1 public static void main(String[] args) {
2
3 // 采用spring 方式
4 // 1.创建spring的IOC容器对象
5 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
6 // 2.从IOC容器中获取bean实例
7 HelloWorld helloWorld6 = (HelloWorld) applicationContext.getBean("helloWorld6");// 利用id定位到IOC容器中的bean
8 System.out.println(helloWorld6.toString());
9 ((ConfigurableApplicationContext) applicationContext).close();
10 }
1 <?xml versinotallow="1.0" encoding="UTF-8"?>
2 <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-4.3.xsd">
3
4 <!-- 类的引用 -->
5 <bean id="person" class="com.lixm.configure.Person">
6 <property name="name" value="lixiuming"></property>
7 <property name="sex" value="女"></property>
8 </bean>
9 <bean id="helloWorld6" class="com.lixm.configure.HelloWorld">
10 <property name="name" value="Spring"></property>
11 <property name="age" value="10"></property>
12 <!-- 可以使用property的 ref 建立bean直接的引用关系 -->
13 <property name="person" ref="person"></property>
14 </bean>
15
16 </beans>
运行结果为:
HelloWorld [name=Spring, age=10, persnotallow=Person [name=lixiuming, sex=女]]
方式二,使用内部bean的方式,内部bean只能在内部使用,不能被外部引用
1 public static void main(String[] args) {
2
3 // 采用spring 方式
4 // 1.创建spring的IOC容器对象
5 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
6 // 2.从IOC容器中获取bean实例
7 HelloWorld helloWorld8 = (HelloWorld) applicationContext.getBean("helloWorld8");// 利用id定位到IOC容器中的bean
8 System.out.println(helloWorld8.toString());
9 ((ConfigurableApplicationContext) applicationContext).close();
10 }
1 <?xml versinotallow="1.0" encoding="UTF-8"?>
2 <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-4.3.xsd">
3
4 <!-- 类的引用 -->
5 <bean id="helloWorld8" class="com.lixm.configure.HelloWorld">
6 <constructor-arg value="Spring" type="java.lang.String"></constructor-arg>
7 <constructor-arg value="10" index="1"></constructor-arg>
8 <!--内部bean只能在内部使用,不能被外部引用 -->
9 <constructor-arg index="2">
10 <bean id="person1" class="com.lixm.configure.Person">
11 <property name="name" value="Qianzd"></property>
12 <property name="sex" value="男"></property>
13 </bean>
14 </constructor-arg>
15 </bean>
16
17 </beans>
运行结果为
HelloWorld [name=Spring, age=10, persnotallow=Person [name=Qianzd, sex=男]]
属性注入方式的内部类可以:
1 <bean id="helloWorld7" class="com.lixm.configure.HelloWorld">
2 <property name="name" value="Spring"></property>
3 <property name="age" value="10"></property>
4 <!--内部bean只能在内部使用,不能被外部引用-->
5 <property name="person">
6 <bean id="person1" class="com.lixm.configure.Person">
7 <property name="name" value="Qianzd"></property>
8 <property name="sex" value="男"></property>
9 </bean>
10 </property>
11 </bean>
3.赋值为null的情况
1 public static void main(String[] args) {
2
3 // 采用spring 方式
4 // 1.创建spring的IOC容器对象
5 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
6 // 2.从IOC容器中获取bean实例
7 HelloWorld helloWorld9 = (HelloWorld) applicationContext.getBean("helloWorld9");// 利用id定位到IOC容器中的bean
8 System.out.println(helloWorld9.toString());
9 ((ConfigurableApplicationContext) applicationContext).close();
10 }
1 <?xml versinotallow="1.0" encoding="UTF-8"?>
2 <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-4.3.xsd">
3
4 <!-- 测试赋值null -->
5 <bean id="helloWorld9" class="com.lixm.configure.HelloWorld">
6 <constructor-arg value="Spring" type="java.lang.String"></constructor-arg>
7 <constructor-arg value="10" index="1"></constructor-arg>
8 <constructor-arg index="2">
9 <null />
10 </constructor-arg>
11 </bean>
12
13 </beans>
运行结果为:
HelloWorld [name=Spring, age=10, persnotallow=null]
4.更改级联属性
给级联属性赋值<property name="person.sex" value="11222"></property>
1 public static void main(String[] args) {
2
3 // 采用spring 方式
4 // 1.创建spring的IOC容器对象
5 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
6 // 2.从IOC容器中获取bean实例
7 HelloWorld helloWorld10 = (HelloWorld) applicationContext.getBean("helloWorld10");// 利用id定位到IOC容器中的bean
8 System.out.println(helloWorld10.toString());
9 ((ConfigurableApplicationContext) applicationContext).close();
10 }
1 <?xml versinotallow="1.0" encoding="UTF-8"?>
2 <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-4.3.xsd">
3
4 <bean id="person" class="com.lixm.configure.Person">
5 <property name="name" value="lixiuming"></property>
6 <property name="sex" value="女"></property>
7 </bean>
8 <!-- 测试级联属性 -->
9 <bean id="helloWorld10" class="com.lixm.configure.HelloWorld">
10 <property name="name" value="Spring"></property>
11 <property name="age" value="10"></property>
12 <property name="person" ref="person"></property>
13 <!-- 为级联属性值。注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常 -->
14 <property name="person.sex" value="11222"></property>
15 </bean>
16 </beans>
运行结果为:
HelloWorld [name=Spring, age=10, persnotallow=Person [name=lixiuming, sex=11222]]
5.如果参数或属性涉及到List
使用list节点为list类型的属性赋值; 在标签里面包含一些元素可以通过<value> 指定简单的常量值,<ref> 指定对其他Bean的引用 通过<bean> 指定内置bean定义 通过<null/> 指定空元素.
HelloWorld调整
1 public static void main(String[] args) {
2
3 // 采用spring 方式
4 // 1.创建spring的IOC容器对象
5 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
6 // 2.从IOC容器中获取bean实例
7 HelloWorld helloWorld11 = (HelloWorld) applicationContext.getBean("helloWorld11");// 利用id定位到IOC容器中的bean
8 System.out.println(helloWorld11.toString());
9 ((ConfigurableApplicationContext) applicationContext).close();
10 }
1 <?xml versinotallow="1.0" encoding="UTF-8"?>
2 <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-4.3.xsd">
3 <bean id="person" class="com.lixm.configure.Person">
4 <property name="name" value="lixiuming"></property>
5 <property name="sex" value="女"></property>
6 </bean>
7 <bean id="person1" class="com.lixm.configure.Person">
8 <property name="name" value="tom"></property>
9 <property name="sex" value="男"></property>
10 </bean>
11 <bean id="person2" class="com.lixm.configure.Person">
12 <property name="name" value="joy"></property>
13 <property name="sex" value="女"></property>
14 </bean>
15 <bean id="person3" class="com.lixm.configure.Person">
16 <property name="name" value="lucy"></property>
17 <property name="sex" value="女"></property>
18 </bean>
19 <!-- 测试list -->
20 <bean id="helloWorld11" class="com.lixm.configure.HelloWorld">
21 <property name="name" value="Spring"></property>
22 <property name="age" value="10"></property>
23 <property name="person" ref="person"></property>
24 <!-- 为级联属性值。注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常 -->
25 <property name="person.sex" value="11222"></property>
26 <property name="persons">
27 <!-- 使用list节点为list类型的属性赋值 在标签里面包含一些元素可以通过<value> 指定简单的常量值,<ref> 指定对其他Bean的引用 通过<bean> 指定内置bean定义 通过<null/> 指定空元素 -->
28 <list>
29 <ref bean="person1"></ref>
30 <ref bean="person2"></ref>
31 <ref bean="person3"></ref>
32 <null />
33 <bean id="person1" class="com.lixm.configure.Person">
34 <property name="name" value="Jerry"></property>
35 <property name="sex" value="男"></property>
36 </bean>
37 </list>
38 </property>
39 </bean>
40 </beans>
运行结果为:
HelloWorld [name=Spring, age=10, persnotallow=Person [name=lixiuming, sex=11222], persnotallow=[Person [name=tom, sex=男], Person [name=joy, sex=女], Person [name=lucy, sex=女], null, Person [name=Jerry, sex=男]]]
6.如果参数或属性涉及到Map
1 public static void main(String[] args) {
2
3 // 采用spring 方式
4 // 1.创建spring的IOC容器对象
5 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
6 // 2.从IOC容器中获取bean实例
7 HelloWorld helloWorld12 = (HelloWorld) applicationContext.getBean("helloWorld12");// 利用id定位到IOC容器中的bean
8 System.out.println(helloWorld12.toString());
9 ((ConfigurableApplicationContext) applicationContext).close();
10 }
1 <?xml versinotallow="1.0" encoding="UTF-8"?>
2 <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-4.3.xsd">
3 <bean id="person" class="com.lixm.configure.Person">
4 <property name="name" value="lixiuming"></property>
5 <property name="sex" value="女"></property>
6 </bean>
7 <!-- 测试Map -->
8 <bean id="helloWorld12" class="com.lixm.configure.HelloWorld">
9 <property name="name" value="Spring"></property>
10 <property name="age" value="10"></property>
11 <property name="person" ref="person"></property>
12 <!-- 为级联属性值。注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常 -->
13 <property name="person.sex" value="11222"></property>
14 <property name="testMap">
16 <map>
17 <entry key="aaa" value="123123"></entry>
18 <entry key="bbb" value-ref="person"></entry>
19 <entry key="ccc" value-ref="person"></entry>
20 </map>
21 </property>
22 </bean>
23 </beans>
运行结果为:
HelloWorld [name=Spring, age=10, persnotallow=Person [name=lixiuming, sex=11222], persnotallow=null, testMap={aaa=123123, bbb=Person [name=lixiuming, sex=11222], ccc=Person [name=lixiuming, sex=11222]}]
7.如果参数或属性涉及到properties
使用props 和prop子节点来为 Properties 属性赋值
对HelloWorld进行调整
1 package com.lixm.configure;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.Properties;
6
7 public class HelloWorld {
8
9 private String name;
10 private int age;
11 private Person person;
12 private List<Person> persons;
13 private Map<String, Object> testMap;
14 private Properties properties;
15
16 public String getName() {
17 return name;
18 }
19
20 public void setName(String name) {
21 this.name = name;
22 }
23
24 public void hello() {
25 System.out.println("hello " + name);
26 }
27
28 public int getAge() {
29 return age;
30 }
31
32 public void setAge(int age) {
33 this.age = age;
34 }
35
36 public Person getPerson() {
37 return person;
38 }
39
40 public void setPerson(Person person) {
41 this.person = person;
42 }
43
44 public List<Person> getPersons() {
45 return persons;
46 }
47
48 public void setPersons(List<Person> persons) {
49 this.persons = persons;
50 }
51
52 public Map<String, Object> getTestMap() {
53 return testMap;
54 }
55
56 public void setTestMap(Map<String, Object> testMap) {
57 this.testMap = testMap;
58 }
59
60 public Properties getProperties() {
61 return properties;
62 }
63
64 public void setProperties(Properties properties) {
65 this.properties = properties;
66 }
67
68 public HelloWorld() {
69 super();
70 }
71
72 public HelloWorld(String name, int age) {
73 super();
74 this.name = name;
75 this.age = age;
76 }
77
78 public HelloWorld(String name, int age, Person person) {
79 super();
80 this.name = name;
81 this.age = age;
82 this.person = person;
83 }
84
85 public HelloWorld(String name, int age, Person person, List<Person> persons) {
86 super();
87 this.name = name;
88 this.age = age;
89 this.person = person;
90 this.persons = persons;
91 }
92
93 public HelloWorld(String name, int age, Person person, List<Person> persons, Map<String, Object> testMap) {
94 super();
95 this.name = name;
96 this.age = age;
97 this.person = person;
98 this.persons = persons;
99 this.testMap = testMap;
100 }
101
102 public HelloWorld(String name, int age, Person person, List<Person> persons, Map<String, Object> testMap,
103 Properties properties) {
104 super();
105 this.name = name;
106 this.age = age;
107 this.person = person;
108 this.persons = persons;
109 this.testMap = testMap;
110 this.properties = properties;
111 }
112
113 @Override
114 public String toString() {
115 return "HelloWorld [name=" + name + ", age=" + age + ", person=" + person + ", persons=" + persons
116 + ", testMap=" + testMap + ", properties=" + properties + "]";
117 }
118
119 }
1 public static void main(String[] args) {
2
3 // 采用spring 方式
4 // 1.创建spring的IOC容器对象
5 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
6 // 2.从IOC容器中获取bean实例
7 HelloWorld helloWorld13 = (HelloWorld) applicationContext.getBean("helloWorld13");// 利用id定位到IOC容器中的bean
8 System.out.println(helloWorld13.toString());
9 ((ConfigurableApplicationContext) applicationContext).close();
10 }
1 <?xml versinotallow="1.0" encoding="UTF-8"?>
2 <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-4.3.xsd">
3 <bean id="person" class="com.lixm.configure.Person">
4 <property name="name" value="lixiuming"></property>
5 <property name="sex" value="女"></property>
6 </bean>
7 <!-- 测试Properties -->
8 <bean id="helloWorld13" class="com.lixm.configure.HelloWorld">
9 <property name="name" value="Spring"></property>
10 <property name="age" value="10"></property>
11 <property name="person" ref="person"></property>
12 <!-- 为级联属性值。注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常 -->
13 <property name="person.sex" value="11222"></property>
14 <!-- 使用props 和prop子节点来为 Properties 属性赋值 -->
15 <property name="properties">
16 <props>
17 <prop key="user">root</prop>
18 <prop key="password">1234</prop>
19 <prop key="jdbcUrl">jdbc:mysql:///test</prop>
20 <prop key="drivceClass">com.mysql.jdbc.driver</prop>
21 </props>
22 </property>
23 </bean>
24 </beans>
运行结果为:
HelloWorld [name=Spring, age=10, persnotallow=Person [name=lixiuming, sex=11222], persnotallow=null, testMap=null, properties={user=root, password=1234, jdbcUrl=jdbc:mysql:///test, drivceClass=com.mysql.jdbc.driver}]
8.配置单例的集合bean,以供多个bean进行引用
需要引入命名空间util,其他不展开了写了;
1 <?xml versinotallow="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
3 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
4 <!-- 配置单例的集合bean,以供多个bean进行引用 引入命名空间util -->
5 <util:list id="persons">
6 <ref bean="person1"></ref>
7 <ref bean="person2"></ref>
8 <ref bean="person3"></ref>
9 <null />
10 <bean id="person1" class="com.lixm.configure.Person">
11 <property name="name" value="Jerry"></property>
12 <property name="sex" value="男"></property>
13 </bean>
14 </util:list>
15 <util:map id="testMaps">
16 <entry key="aaa" value="123123"></entry>
17 <entry key="bbb" value-ref="person"></entry>
18 <entry key="ccc" value-ref="person"></entry>
19 </util:map>
20 <util:properties id="properties">
21 <prop key="user">root</prop>
22 <prop key="password">1234</prop>
23 <prop key="jdbcUrl">jdbc:mysql:///test</prop>
24 <prop key="drivceClass">com.mysql.jdbc.driver</prop>
25 </util:properties>
26
27 <bean id="person1" class="com.lixm.configure.Person">
28 <property name="name" value="tom"></property>
29 <property name="sex" value="男"></property>
30 </bean>
31 <bean id="person2" class="com.lixm.configure.Person">
32 <property name="name" value="joy"></property>
33 <property name="sex" value="女"></property>
34 </bean>
35 <bean id="person3" class="com.lixm.configure.Person">
36 <property name="name" value="lucy"></property>
37 <property name="sex" value="女"></property>
38 </bean>
39 <bean id="person" class="com.lixm.configure.Person">
40 <property name="name" value="lixiuming"></property>
41 <property name="sex" value="女"></property>
42 </bean>
43 </beans>
9.标签的使用
通过P命名空间为bean的属性赋值,需要先导入P命名空间,相对于传统的配置更加简洁,配置如下
1 <bean id="person4" class="com.lixm.configure.Person" p:name="aaaaaaaaa" p:sex="aaa">
2
3 </bean>
10.bean的作用域
使用bean 的scope 属性来配置bean的作用域;
作用域常用的有两种:singleton(单例,也是是默认值) 和prototype(原型的);singleton是容器初始化创建bean实例时,在整个容器的生命周期内,只创建了这一个bean;prototype 是容器初始化时不创建bean的实例,而是在每次请求创建bean实例时创建,并且把bean返回;
这里加个Car类
1 package com.lixm.scopes;
2
3 public class Car {
4
5 private String brand;
6 private double price;
7
8 private double tyrePerimeter;
9
10 public String getBrand() {
11 return brand;
12 }
13
14 public void setBrand(String brand) {
15 this.brand = brand;
16 }
17
18 public double getPrice() {
19 return price;
20 }
21
22 public void setPrice(double price) {
23 this.price = price;
24 }
25
26 public double getTyrePerimeter() {
27 return tyrePerimeter;
28 }
29
30 public void setTyrePerimeter(double tyrePerimeter) {
31 this.tyrePerimeter = tyrePerimeter;
32 }
33
34 public Car() {
35 System.out.println("constructor....");
36 }
37
38 public Car(String brand, double price, double tyrePerimeter) {
39 super();
40 this.brand = brand;
41 this.price = price;
42 this.tyrePerimeter = tyrePerimeter;
43 }
44
45 @Override
46 public String toString() {
47 return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter=" + tyrePerimeter + "]";
48 }
49
50 }
1 public static void main(String[] args) {
2 ApplicationContext context = new ClassPathXmlApplicationContext("bean-scope.xml");
3 Car car1 = (Car) context.getBean("car");
4 Car car2 = (Car) context.getBean("car");
5 System.out.println(car1 == car2);
6
7 ((ConfigurableApplicationContext) context).close();
8 }
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6
7 <bean id="car" class="com.lixm.autowire.Car" scope="singleton" p:brand="bmw" p:price="300000">
8 </bean>
9 </beans>
constructor....
true
返回结果是true ,也就是说car1 和car2的地址值不相同;容器创建了两个。
如果scope设置为prototype,如下
1 <bean id="car" class="com.lixm.autowire.Car" scope="prototype" p:brand="bmw" p:price="300000">
2 </bean>
运行结果为:
constructor....
constructor....
false
man方法运行结果时false;也就是说car1 和car2的地址值相同;也就是说car1和car2是同一个
11.bean之间的关系
- bean的继承(parent属性)
存在一种情况,就是当一个bean配置完成后,配置第二个bean时,需要用到第一个bean的部分属性;如下
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6
7 <bean id="address1" class="com.lixm.autowire.Address" p:city="beijin" p:street="wangfujing"></bean>
8 <bean id="address2" class="com.lixm.autowire.Address" p:city="beijin" p:street="wudaokou"></bean>
9
10 </beans>
address1 和 address2中 city都为beijin;那么我们可以使用 bean的parent属性 来实现bean的继承
这里用到了Address类 和Person类
View Code
View Code
1 public static void main(String[] args) {
2 ApplicationContext context = new ClassPathXmlApplicationContext("bean-relations.xml");
3 Address address1 = (Address) context.getBean("address1");
4 Address address2 = (Address) context.getBean("address2");
5 System.out.println(address1);
6 System.out.println(address2);
7 ((ConfigurableApplicationContext) context).close();
8
9
10 }
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6
7 <bean id="address1" class="com.lixm.autowire.Address" p:city="beijin" p:street="wangfujing"></bean>
8 <bean id="address2" parent="address1" p:street="wudaokou"></bean>
9 </beans>
运行结果都为
Address [city=beijin, street=wangfujing]
Address [city=beijin, street=wudaokou]
- 抽象bean(abstract属性)
abstract 抽象bean:bean的abstract 属性为true 不能被IOC容器实例化,只能用来被继承;若一个bean的class属性没有指定,则该bean必须是一个抽象bean,如:
1 <bean id="address3" parent="address1" p:city="beijin" p:street="wudaokou" abstract="true"></bean>
- 依赖(depends-on属性)
比如说要求再配置person时,必须有一个关联的car,也就说person这个bean依赖于Car这个bean
1 <bean id="person" class="com.lixm.autowire.Person" p:name="Tom" depends-on="car"></bean>
此时若没有id=car的属性,那么,getBean时会报错( [bean-relations.xml]: 'person' depends on missing bean 'car'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ');
如果依赖多个 使用,或者空格来配置bean的名称
1 <bean id="person2" class="com.lixm.autowire.Person" p:name="Tom" depends-on="car,address2" p:car-ref="car"></bean>
12.外部文件
这里我们使用C3P0作为我们的数据源;所以需要引入3个jar包;c3p0-0.9.5.5; c3p0-oracle-thin-extras-0.9.5.5.jar ;mchange-commons-java-0.2.19.jar
需要在src下添加一个db.properties文件(演示的project为Java项目,所以在src下创建即可);
- 外部文件的一般配置方式;
1 <?xml versinotallow="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"
6 xsi:schemaLocation="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-4.3.xsd">
8
9 <!-- 外部文件的一般配置方式 -->
10 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:user="lixm" p:password="123456" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql:///test">
11 </bean>
12 </beans>
- 加载properties文件的配置方式
需要额外加入 context 的命名空间
<?xml versinotallow="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-4.3.xsd">
<!-- 加载properties 文件 -->
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:user="${user}" p:password="${password}" p:driverClass="${driverclass}" p:jdbcUrl="${jdbcurl}">
</bean>
</beans>
1 public static void main(String[] args) {
2 ApplicationContext context = new ClassPathXmlApplicationContext("bean-properties.xml");
3 ComboPooledDataSource dataSource = (ComboPooledDataSource) context.getBean("dataSource");
4 System.out.println(dataSource);
5
6 ((ConfigurableApplicationContext) context).close();
7 }
13.spel
sple 可以为属性赋值,可以应用类的静态属性,可以做运算,可以医用其他bean及其属性等;
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6 <!-- 使用spe为属性赋值 (字面值) -->
7 <bean id="address" class="com.lixm.autowire.Address" p:city="#{'上海'}" p:street="#{'南京路'}">
8 </bean>
9 <!-- 使用SPEL 引用类的静态属性 -->
10 <bean id="car" class="com.lixm.autowire.Car" p:brand="#{'bmw'}" p:price="300000" p:tyrePerimeter="#{T(java.lang.Math).PI*80}">
11 </bean>
12
13 <bean id="personSpel" class="com.lixm.autowire.Person" >
14 <!-- 使用spel 引用其他Bean -->
15 <property name="car" value="#{car}"></property>
16 <!-- 使用spel中引用其他bean的属性 -->
17 <property name="city" value="#{address.city}"></property>
18 <!-- 使用spel中使用运算符 -->
19 <property name="into" value="#{car.price>=300000?'金领':'白领'}"></property>
20 <property name="name" value="#{'小明'}"></property>
21 </bean>
22 </beans>
1 public static void main(String[] args) {
2 ApplicationContext context = new ClassPathXmlApplicationContext("bean-spel.xml");
3 Address address = (Address) context.getBean("address");
4 Car car = (Car) context.getBean("car");
5 Person personSpel = (Person) context.getBean("personSpel");
6 System.out.println(address);
7 System.out.println(car);
8 System.out.println(personSpel);
9 ((ConfigurableApplicationContext) context).close();
10 }
运行结果为:
Address [city=上海, street=南京路]
Car [brand=bmw, price=300000.0, tyrePerimeter=251.32741228718345]
Person [name=小明, address=null, car=Car [brand=bmw, price=300000.0, tyrePerimeter=251.32741228718345], city=上海, into=金领]
六、自动装配(不常用)
1.什么是自动装配?
看一个例子:
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6 <bean id="car" class="com.lixm.autowire.Car" p:brand="bmw" p:price="300000"></bean>
7 <bean id="address" class="com.lixm.autowire.Address" p:city="beijin" p:street="wangfujing"></bean>
8 <bean id="person" class="com.lixm.autowire.Person" p:name="lixm" p:car-ref="car" p:address-ref="address"></bean><!-- 手动装配 -->
9 <bean id="person1" class="com.lixm.autowire.Person" p:name="lixm" autowire="byName"></bean><!-- 自动装配 -->
10 <bean id="person2" class="com.lixm.autowire.Person" p:name="lixm" autowire="byType"></bean><!-- 自动装配 -->
11 </beans>
手动装配就是 需要手动赋值p:car-ref="car" p:address-ref="address"
自动装配就是 自动的把 Person 中 car ,容器中存在的对应的car的bean装到Person 中的car中,同理address也是如此;
2.自动装配的常用形式
byName: 根据bean的名称(比如id="car" 或者id="address" )和当前bean(比如id="car" 或者id="address" )的setter风格的属性名进行自动装配,若有匹配的,则进行自动装配,若没有匹配的,则不装配;
1 public static void main(String[] args) {
2 ApplicationContext context = new ClassPathXmlApplicationContext("beans-autowired.xml");
3
4 Person person1 = (Person) context.getBean("person1");// 自动装配(byName)
5 System.out.println(person1);
6
7 ((ConfigurableApplicationContext) context).close();
8
9 }
<?xml versinotallow="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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car1" class="com.lixm.autowire.Car" p:brand="bmw" p:price="300000"></bean>
<bean id="address" class="com.lixm.autowire.Address" p:city="beijin" p:street="wangfujing"></bean>
<bean id="person1" class="com.lixm.autowire.Person" p:name="lixm" autowire="byName"></bean><!-- 自动装配 -->
</beans>
此时运行结果为:
Person [name=lixm, address=Address [city=beijin, street=wangfujing], car=null]
这里的car没有值;也就是说,没有匹配上;
如果把配置文件修改成如下:
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6 <bean id="car" class="com.lixm.autowire.Car" p:brand="bmw" p:price="300000"></bean>
7 <bean id="car1" class="com.lixm.autowire.Car" p:brand="benz" p:price="300000"></bean>
8 <bean id="address" class="com.lixm.autowire.Address" p:city="beijin" p:street="wangfujing"></bean>
9 <bean id="person1" class="com.lixm.autowire.Person" p:name="lixm" autowire="byName"></bean><!-- 自动装配 -->
10
11 </beans>
那么运行结果为:
Person [name=lixm, address=Address
[city=beijin, street=wangfujing], car=Car [brand=bmw, price=300000.0]]
此时,car自动装配成功;
byType:根据bean的类型和当前bean的属性的类型进行自动装配,若IOC容器中国有1个以上类型匹配的bean,则抛异常。也就是说:
1 public static void main(String[] args) {
2 ApplicationContext context = new ClassPathXmlApplicationContext("beans-autowired.xml");
3
4 Person person2 = (Person) context.getBean("person2");// 自动装配(byName)
5 System.out.println(person2);
6
7 ((ConfigurableApplicationContext) context).close();
8
9 }
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6 <bean id="car" class="com.lixm.autowire.Car" p:brand="bmw" p:price="300000"></bean>
7 <bean id="car1" class="com.lixm.autowire.Car" p:brand="bmw" p:price="300000"></bean>
8 <bean id="address" class="com.lixm.autowire.Address" p:city="beijin" p:street="wangfujing"></bean>
9 <bean id="person2" class="com.lixm.autowire.Person" p:name="lixm" autowire="byType"></bean><!-- 自动装配 -->
10 </beans>
配置文件配置了car 和 car1 那么,在main方法运行的时候 报错;
七、生命周期
bean的生命周期:构造器 --> set/get方法 (属性赋值) --> 初始化方法(创建bean调用初始化方法)-->使用bean方法-->销毁(关闭容器)
使用 init-method配置初始化方法 destory-method配置指定的销毁方法;
附上Car类
1 public class Car {
2
3 public Car() {
4 super();
5 System.out.println("Constor");
6 }
7
8 private String brand;
9
10 public void setBrand(String brand) {
11 System.out.println("setBrand");
12 this.brand = brand;
13 }
14
15 public void init() {
16 System.out.println("init..");
17 }
18
19 public void destory() {
20 System.out.println("destory..");
21 }
22 }
1 <?xml versinotallow="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 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6 <bean id="car" class="com.lixm.cycle.Car" p:brand="Audi" init-method="init" destroy-method="destory"></bean>
7 </beans>
1 public static void main(String[] args) {
2 ApplicationContext context = new ClassPathXmlApplicationContext("bean-cyle.xml");
3
4 Car car = (Car) context.getBean("car");
5 System.out.println(car);
6 ((ConfigurableApplicationContext) context).close();
7 }
运行结果:
Constor
setBrand
init..
com.lixm.cycle.Car@87a85e1
destory..
标签:xml,基于,name,age,HelloWorld,bean,public,String From: https://blog.51cto.com/u_10632206/7373446