接下来回顾一下Spring中的一些知识点,以便于更好的的分析源码,本小节分析一下Spring实例化bean的三种方式。
Spring实例化Bean的方式大致上可以分为三种,构造函数实例化,工厂方法实例化,静态工厂方法实例化。
1.构造函数实例化(无参构造函数和有参构造函数)
- bean
package com.lyc.cn.v2.day01; /** 1. @author: LiYanChao 2. @create: 2018-09-27 14:23 */ public class Dog { /** 姓名 **/ private String name; /** 年龄 **/ private int age; /** * 默认构造函数 */ public Dog() { } /** * 构造函数 * @param name 姓名 * @param age 年龄 */ public Dog(String name, int age) { this.name = name; this.age = age; } public void sayHello() { System.out.println("大家好, 我叫" + getName() + ", 我今年" + getAge() + "岁了"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
- xml
<?xml version="1.0" encoding="UTF-8"?> <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.xsd" profile="dev"> <!-- ====================实例化bean的方式Begin==================== --> <!-- 默认构造实例化 --> <bean id="dog1" class="com.lyc.cn.v2.day01.Dog"/> <!-- 指定构造器实例化 --> <bean id="dog2" class="com.lyc.cn.v2.day01.Dog"> <!-- 指定构造器参数 index对应构造器中参数的位置 --> <!-- 也可以通过指定参数类型,指定参数名称来注入属性--> <constructor-arg index="0" value="小明"/> <constructor-arg index="1" value="3"/> </bean> <!-- ====================实例化bean的方式End==================== --> </beans>
2.工厂方法
- Factory
package com.lyc.cn.v2.day01; /** * 工厂方法实例化 * @author LiYanChao * @create: 2018-09-07 23:40 */ public class DogFactory { public Dog newInstance(String name, int age) { return new Dog(name, age); } }
- xml
<!-- 实例工厂方法实例化 --> <bean id="dogFactory" class="com.lyc.cn.v2.day01.DogFactory"/> <!-- 不能指定class属性,此时必须使用factory-bean属性来指定工厂Bean,factory-method属性指定实例化Bean的方法 --> <bean id="dog4" factory-bean="dogFactory" factory-method="newInstance"> <constructor-arg index="0" value="小明"/> <constructor-arg index="1" value="3"/> </bean>
3.静态工厂方法
- Factory
package com.lyc.cn.v2.day01; /** * 静态工厂实例化 * @author LiYanChao * @create: 2018-09-07 23:40 */ public class DogStaticFactory { // 静态工厂方法 public static Dog newInstance(String name, int age) { // 返回需要的Bean实例 return new Dog(name, age); } }
- xml
<!-- 静态工厂方法实例化 --> <bean id="dog3" class="com.lyc.cn.v2.day01.DogStaticFactory" factory-method="newInstance"> <!-- 指定构造器参数 index对应构造器中参数的位置 --> <constructor-arg index="0" value="小明"/> <constructor-arg index="1" value="3"/> </bean>
以上就是实例化Bean方式的Bean,Factory和xml配置,比较简单,而且都有注释,不一一讲解了,接下来新建一个测试类,看一下运行效果。
4. 测试
package com.lyc.cn.v2.day01; import com.lyc.cn.v2.day01.inner.Outer; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; /** * @author: LiYanChao * @create: 2018-09-07 23:40 */ public class MyTest { private XmlBeanFactory xmlBeanFactory; @Before public void initXmlBeanFactory() { System.setProperty("spring.profiles.active", "dev"); System.out.println("\n========测试方法开始=======\n"); xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("v2/day01.xml")); } @After public void after() { System.out.println("\n========测试方法结束=======\n"); } @Test public void test1() { // 默认构造器 System.out.println("默认构造器"); Dog dog1 = xmlBeanFactory.getBean("dog1", Dog.class); dog1.sayHello(); } @Test public void test2() { // 指定构造器 System.out.println("有参构造器"); Dog dog2 = xmlBeanFactory.getBean("dog2", Dog.class); dog2.sayHello(); } @Test public void test3() { // 静态工厂 System.out.println("静态工厂"); Dog dog3 = xmlBeanFactory.getBean("dog3", Dog.class); dog3.sayHello(); } @Test public void test4() { // 实例工厂 System.out.println("实例工厂"); Dog dog4 = xmlBeanFactory.getBean("dog4", Dog.class); dog4.sayHello(); } }
5.测试结果
========测试方法开始======= 默认构造器 大家好, 我叫null, 我今年0岁了 ========测试方法结束======= ========测试方法开始======= 有参构造器 大家好, 我叫小明, 我今年3岁了 ========测试方法结束======= ========测试方法开始======= 静态工厂 大家好, 我叫小明, 我今年3岁了 ========测试方法结束======= ========测试方法开始======= 实例工厂 大家好, 我叫小明, 我今年3岁了 ========测试方法结束=======