天天看点

spring创建对象的三种方式

我们紧接着上一篇博文 https://blog.csdn.net/liuying1802028915/article/details/90344742

来研究spring创建对象的方式都有哪几种,

答案是一共有三种:

1.使用构造方式创建对象

2.使用静态工厂方法创建对象

3.使用实例工厂方法创建对象

接下来我们一一来讲解一下:

1.使用构造方法创建对象

    我们上一篇写spring helloworld 程序的时候使用的就是这一种使用构造方法创建对象

配置文件如下:

<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-3.0.xsd">

    <bean id="helloBean" lazy-init="true" class="com.taoj.entity.HelloWorld">
        <property name="name" value="Yiibai" />
    </bean>

</beans>
           

这种方式就是使用无参构造方法进行创建,然后使用set方法对属性进行注入。

这里应该还有一种那就是使用有参构造方法进行创建

下面是HelloWorld类:

package com.taoj.entity;

public class HelloWorld {

    private String name;

    public HelloWorld(){
        System.out.println("无参构造方法进行创建");
    }

    public HelloWorld(String name){
        System.out.println("有参构造开始");
        this.name = name;
        System.out.println("有参构造结束");
    }

    public void setName(String name) {
        this.name = name;
    }

    public void sqyHello(){
        System.out.println("setName开始");
        System.out.println("hello "+ name);
        System.out.println("setName结束");
    }
}
           

下面是配置文件:

<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-3.0.xsd">

    <bean id="helloBean" lazy-init="true" class="com.taoj.entity.HelloWorld">
        <!--<property name="name" value="Yiibai" />-->
        <constructor-arg name="name" value="火星"></constructor-arg>
    </bean>

</beans>
           

不适用property了,使用<constructor-arg> 来配置。

2.使用静态工厂方法穿件对象

首先新建一个静态工厂类方法

package com.taoj.factory;

import com.taoj.entity.HelloWorld;

public class HelloWorldFactory {
    public static HelloWorld getInstance(){
        return new HelloWorld();
    }
}
           

然后进行如下配置:

<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-3.0.xsd">

    <bean id="hello2" class="com.taoj.factory.HelloWorldFactory" factory-method="getInstance"></bean>

</beans>
           

这种方式是使用静态工厂方法创建对象,测试如下:

package com.taoj.test;

import com.taoj.entity.HelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldSpring2 {
    public static void main(String[] args) {
        System.out.println("用静态工厂方法创建对象开始");
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
        HelloWorld hello = (HelloWorld) context.getBean("hello2");
        hello.setName("火星");
        hello.sqyHello();
        System.out.println("用静态工厂方法创建对象结束");

    }
}
           

3.第三种就是使用实例工厂方法创建对象

首先新建一个实例工厂类

package com.taoj.factory;

import com.taoj.entity.HelloWorld;

public class HelloWorldFactory3 {

    public HelloWorld getInstance(){
        return new HelloWorld();
    }
}
           

接下来是配置文件

<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-3.0.xsd">

    <bean id="helloWorldFactory3" class="com.taoj.factory.HelloWorldFactory3"></bean>
    <bean id="hello3" factory-bean="helloWorldFactory3" factory-method="getInstance"></bean>

</beans>
           

接下来是测试类

package com.taoj.test;

import com.taoj.entity.HelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldSpring3 {

    public static void main(String[] args) {
        System.out.println("使用实例工厂方法创建对象开始");
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");
        HelloWorld hello3 = (HelloWorld) context.getBean("hello3");
        hello3.setName("火星我爱你");
        hello3.sqyHello();
        System.out.println("使用实例工厂方法创建对象结束");
    }
}
           

以上就是三种spring创建对象的方式,我在这里记录一下,方便以后自己查询。

此时我在想你。。。火星小姐姐

继续阅读