spring Bean的实例化

本文介绍了Spring IOC容器中Bean的三种实例化方式:构造器实例化、静态工厂实例化及实例工厂实例化,并通过示例代码详细展示了每种方式的具体应用。
Spring ioc容器如何实例化Bean,传统应用程序可以通过new和放射方式进行实例化bean,而spring ioc容器则需要根据bean定义的配置元数据使用反射机制来创建Bean,在spring IOC容器中主要有以下几种创建bean实例化方式;

1、使用构造器实例化bean    
    这是最简单的方式,spring ioc容器既能使用默认构造器,也能使用有参的构造器,两种方式来创建Bean

2、使用静态工厂方式实例化bean
    使用这种方式,除了指定必须的class属性,还要指定factory-method 属性,来指定实例化的Bean的方法,也允许指定方法参数,spring ioc容器将调用此属性来获取bean

3、使用实例工厂方法实例化Bean
    不能指定class属性。只能用factory-bean属性来指定工厂,factory-method指定实例化bean的方法,而且使用实例工厂方法,允许指定方法参数,方式和使用构造器方式是一抹一样的。


构造器实例化

    构造器实例化bean是最简单的方式,spring ioc 容器既能使用默认空构造器也能使用有参数的构造器两种方式创建bean,如下
    1、空构造器实例化
   
 <bean id="helloWorldWithArgs" class="com.zto.helloWorld"/>

    必须要有空构造器,如果不存在,在实例化过程中,将会抛出异常。
    2 、有参构造器来实例化
   <bean id="helloWorldNoWithArgs" class="com.zto.helloWorld">
           <!--指定构造器参数 -->    
        <constructor-arg index="0" value="hello spring"/>
    </bean>



示例:
定义接口

创建test1.xml配置文件,


   <bean id="helloWorldWithArgs" class="com.zto.helloWorld"/>
   <bean id="helloWorldNoWithArgs" class="com.zto.helloWorld">
           <!--指定构造器参数 -->    
        <constructor-arg index="0" value="hello spring"/>
   </bean>

分别配置一个有参构造和一个无参构造


package com.zto;

public interface HelloWorld {
    public void  sayHello();
}
实现接口,实现类中定义了一个属性 message,并添加get、set方法,同时增加该实现类的无参构造器和有参构造器
package com.zto.impl;

import com.zto.HelloWorld;

public class HelloWorldImpl implements HelloWorld {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public HelloWorldImpl() {
        this.message="hello World!";
    }

    public HelloWorldImpl(String message) {
        this.message = message;
    }

    @Override
    public void sayHello() {
        System.out.println(message);
    }

}

测试类 Main
    package com.zto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main2 {

    public static void main(String[] args) {
        say2();
        say1();
    }

    public static void say2() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test1.xml");
        HelloWorld hello =beanFactory.getBean("helloWorldWithNoArgs",HelloWorld.class);
        hello.sayHello();

    }
    public static void say1() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test1.xml");
        HelloWorld hello =beanFactory.getBean("helloWorldWithArgs",HelloWorld.class);
        hello.sayHello();

    }


}


静态工厂实例化

使用静态工厂的方式除了指定必须的class属性,还要指定factory-method属性来指定实例化bean的方法,而且使用静态工厂方法也允许是指定方法参数,spring ioc容器将调用此属性指定的方法来获取bean,


定义一个静态方法工厂
package com.zto.impl;

import com.zto.HelloWorld;

public class HelloWorldStaticFactory {
    public static HelloWorld newInstance(String message) {
        return new HelloWorldImpl(message);
    }
}


test1.xml增加配置文件
<!--使用有参数构造参数-->
<bean id ="helloServiceStaticFactory" class= "com.zto.helloworld"   factory-method="newInstance">
    <!--指定构造器-->
    <constructor-arg index="0" value="hello static factory"/>
</bean>

测试:

package com.zto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main2 {

    public static void main(String[] args) {
        say2();
        say1();
        say3();
    }

    public static void say2() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test1.xml");
        HelloWorld hello =beanFactory.getBean("helloWorldWithNoArgs",HelloWorld.class);
        hello.sayHello();

    }
    public static void say1() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test1.xml");
        HelloWorld hello =beanFactory.getBean("helloWorldWithArgs",HelloWorld.class);
        hello.sayHello();

    }
    public static void say3() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test1.xml");
        HelloWorld hello =beanFactory.getBean("helloWorldStaticFactory",HelloWorld.class);
        hello.sayHello();

    }


}

实例工厂实例化

使用实例工厂方式不能指定class属性,此时必须使用factory-bean属性来指定工厂bean,factory-method属性指定实例化bean的方法,而且使用实例工厂方法允许指定方法参数,方式和使用构造器方式一样,配置如下

创建一个实例工厂,里面拥有一个newInstance方法,返回HelloWorld 类
package com.zto.impl;

import com.zto.HelloWorld;

public class HelloWorldInstanceFactory {
    public HelloWorld newInstance(String message){
        return new HelloWorldImpl(message);
    }
}

test1.xml增加配置文件

<!-- 1、定义实例工厂bean -->
    <bean id="beanInstanceFactory" class="com.zto.impl.HelloWorldInstanceFactory" />
    <!-- 2、使用实例工厂bean创建Bean -->
    <bean id="helloWorldInstance" factory-bean="beanInstanceFactory"
        factory-method="newInstance">
        <constructor-arg index="0" value="Hello Instance Factory"></constructor-arg>
    </bean>

首先配置实例工厂的bean,之后使用factory-bean引入改实例工厂,并制定该实例下的newInstance方法为实例工厂方法。

测试Main:

package com.zto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main2 {

    public static void main(String[] args) {
        say2();
        say1();
        say3();
        say4();
    }

    public static void say2() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test1.xml");
        HelloWorld hello =beanFactory.getBean("helloWorldWithNoArgs",HelloWorld.class);
        hello.sayHello();

    }
    public static void say1() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test1.xml");
        HelloWorld hello =beanFactory.getBean("helloWorldWithArgs",HelloWorld.class);
        hello.sayHello();

    }
    public static void say3() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test1.xml");
        HelloWorld hello =beanFactory.getBean("helloWorldStaticFactory",HelloWorld.class);
        hello.sayHello();

    }

    public static void say4() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test1.xml");
        HelloWorld hello =beanFactory.getBean("helloWorldInstance",HelloWorld.class);
        hello.sayHello();

    }


}




















评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值