Spring 中Bean的实例化三种方式

在spring项目中,类的实例化交给了spring容器来管理,我们应该了解spring中bean的三种实例化方式。

这三种实例化方式分别为构造器实例化,静态工厂实例化,实例工厂实例化,其中最最最最最重要的也是最常见的实例化方式是构造器实例化!

 

构造器实例化

构造器实例化是指spring容器通过Bean对应的类中的默认的无参构造方法来实例化这个bean。直接上一份代码在解释

//测试是否实例化成功
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/config/ApplicationContext.xml");
       Service service = (Service)applicationContext.getBean("service");
        System.out.println(service);
    }
}


package com.test;

public class Service {


}

/*
<?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="service" class="com.test.Service"></bean>

</beans>
*/

//结果
//com.test.Service@32d992b2

创建一个Service类,一个Test类用来测试,在配置文件总声名service类对应的bean然后在测试类中获取配置文件对象,通过该对象获取service类的对象,输出是否实例化成功,结果表明实例化成功了,我们大部分都是使用这种方法实例化bean的。

 

静态工厂方式实例化

静态工厂的方式去实例化bean其实我没使用过,我只有入门的时候去了解然后练习过几遍后面就没有机会使用了,后面后面的这俩种方式了解知道就够了。还是上一份代码

//测试工厂实例化是否成功
package com.test;

public class Service {


}


package com.test;

public class Bean {

    public static Service createService(){
        return new Service();
    }
}


package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/config/ApplicationContext.xml");
       /*Service service = (Service)applicationContext.getBean("service");
        System.out.println(service);*/

        System.out.println(applicationContext.getBean("bean"));
    }
}


//配置文件

/*
<?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="service" class="com.test.Service"></bean>

    <bean id="bean" class="com.test.Bean" factory-method="createService"></bean>
</beans>
*/

//结果
//com.test.Service@2a5ca609

想要使用工厂方式实例化bean的话还需要创建工厂类,这个工厂类的唯一功能就是不断的产生对象,然后我们使用这个类的话就需要在配置文件中配置一个bean(一个类对应一个bean)  不同于普通的bean只需要id和class就行了,工厂bean还需要配置一个factory-method属性,然后在测试里中获取这个工厂对象产生需要的对象。

 

实例工厂方式实例化

实例工厂方式实例化与上面的方法的区别就是工厂方法没有用static修饰,也就是实例方法来创建bean实例的方式。继续上一份代码

//测试是否实现bean的实例化

package com.test;

public class Service {


}


package com.test;

public class Bean {

    public  Service createService(){
        return new Service();
    }
}


package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/config/ApplicationContext.xml");
       /*Service service = (Service)applicationContext.getBean("service");
        System.out.println(service);*/

        System.out.println(applicationContext.getBean("factorybean"));
    }
}


//配置文件

/*
<?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="service" class="com.test.Service"></bean>

    <bean id="bean" class="com.test.Bean"></bean>

    <bean id ="factorybean" factory-bean="bean" factory-method="createService"></bean>
</beans>
*/

//结果
com.test.Service@2a5ca609

证明实例化成功了,以上就是spring容器实例化bean的三种方式,在三种中第一种最常用,第二种第三种我是没使用过,只了解,而且我也不怎么明天第二第三种区别在哪里,在我写过的项目中也都是使用注解的方式。

 

 

 

 

 

 

 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值