Spring可以通过调用构造器来创建Bean、调用静态工厂方法创建Bean、调用实例工厂方法创建Bean。
构造器创建Bean实例
之前几篇文章中已经讲述了构造注入的实例,这里略微阐述一下,构造器创建Bean,如果配置文件中配置了构造注入的方式的话,就会根据配置文件中配置的方式获取构造方法,然后创建实例,如果没有进行配置那么Spring会直接调用无参的构造方法创建该类的实例。
静态工厂的方法创建Bean实例
定义一个Person接口
- package com.siti.spring20160301;
- public interface Person {
- public void saySth();
- }
- package com.siti.spring20160301;
- public class WangYang implements Person{
- private String descrip;
- public String getDescrip() {
- return descrip;
- }
- public void setDescrip(String descrip) {
- this.descrip = descrip;
- }
- @Override
- public void saySth() {
- System.out.println(this.descrip + "wangyang");
- }
- }
- package com.siti.spring20160301;
- public class WY implements Person{
- private String descrip;
- public String getDescrip() {
- return descrip;
- }
- public void setDescrip(String descrip) {
- this.descrip = descrip;
- }
- @Override
- public void saySth() {
- System.out.println(this.descrip + "wy");
- }
- }
BeanFactory类,是一个静态工厂类,该类的getBean要是一个静态方法,不然的话会抛出异常,通过这个getBean方法获得对应的实例。
- package com.siti.spring20160301;
- public class BeanFactory {
- public static Person getBean(String arg){
- if("wangyang".equals(arg)){
- return new WangYang();
- }else if("wy".equals(arg)){
- return new WY();
- }
- return null;
- }
- }
接下来在配置文件中,下面的constructor-arg标签中的值为上面的静态工厂类的getBean方法传入参数arg. Spring利用反射获得bean:wangyang = BeanFactory.getBean("wangyang"); 这样就将WangYang这个类实例化了。
- <?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">
- <!-- bean definitions here -->
- <bean id = "wangyang" class = "com.siti.spring20160301.BeanFactory" factory-method="getBean">
- <constructor-arg value="wangyang"></constructor-arg>
- <property name="descrip" value="powerful"></property>
- </bean>
- <bean id = "wy" class = "com.siti.spring20160301.BeanFactory" factory-method="getBean">
- <constructor-arg value="wy"></constructor-arg>
- <property name="descrip" value="hard work"></property>
- </bean>
- </beans>
实例工厂方法创建Bean
实例工厂方法需要创建一个工厂的实例,然后由工厂实例去产生其他的实例对象。基本的实例如下所示,大部分的代码与上面的静态工厂类方法相同,只是略微改了一点地方而已。
定义Person接口,定义一个方法:saySth
- package com.siti.spring20160301shilifactory;
- public interface Person {
- public void saySth();
- }
WangYang和WY分别实现这个接口。
- package com.siti.spring20160301shilifactory;
- public class WangYang implements Person{
- private String descrip;
- public String getDescrip() {
- return descrip;
- }
- public void setDescrip(String descrip) {
- this.descrip = descrip;
- }
- @Override
- public void saySth() {
- System.out.println(this.descrip + "wangyang");
- }
- }
- package com.siti.spring20160301shilifactory;
- public class WY implements Person{
- private String descrip;
- public String getDescrip() {
- return descrip;
- }
- public void setDescrip(String descrip) {
- this.descrip = descrip;
- }
- @Override
- public void saySth() {
- System.out.println(this.descrip + "wy");
- }
- }
- package com.siti.spring20160301shilifactory;
- public class BeanFactory {
- public Person getBean(String arg){
- if("wangyang".equals(arg)){
- return new WangYang();
- }else if("wy".equals(arg)){
- return new WY();
- }
- return null;
- }
- }
配置文件:
- <?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">
- <!-- 实例工厂 -->
- <bean id = "factory" class = "com.siti.spring20160301shilifactory.BeanFactory"></bean>
- <bean id = "wangyang" factory-bean="factory" factory-method="getBean">
- <constructor-arg value="wangyang"></constructor-arg>
- <property name="descrip" value="powerful - "></property>
- </bean>
- <bean id = "wy" factory-bean="factory" factory-method="getBean">
- <constructor-arg value="wy"></constructor-arg>
- <property name="descrip" value="hard work - "></property>
- </bean>
- </beans>
静态工厂方法不需要实例化BeanFactory,但是实例工厂方法需要实例化BeanFactory。在配置文件中静态工厂是通过class来定位Factory而实例工厂则是通过factory-bean来定位Factory的。
但是他们都是通过factory-method来指定调用的方法,都是通过constructor-arg标签来指定前面调用的方法的那个参数,也都是通过property来为创建的实例对象注入属性值的。