主页 > 其他  > 

Spring中Bean的四种实例化方法

Spring中Bean的四种实例化方法
Bean的四种实例化方法

Bean是Spring核心的概念,另外一个核心的概念是AOP。官网上,Bean的解释是:

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. 翻译: 在Spring中,构建应用程序主干并且由Spring IoC容器管理的对象被称为beans,bean是一个由Spring容器实例化,组装和管理的对象。

本文是在学习B站up主动力学习Spring课程后总结得出的,主要关注Spring中Bean的实例化部分,主要介绍的就是它的四种实例化方法。

1.通过构造方法实例化

默认情况下,会调用Bean的无参数构造方法。 首先定义一个带构造方法的类。

public class User { public User() { System.out.println("User类的无参数构造方法执行。"); } }

然后在Spring的XML配置文件中定义好该类型的bean

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// .springframework.org/schema/beans" xmlns:xsi="http:// .w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:// .springframework.org/schema/beans http:// .springframework.org/schema/beans/spring-beans.xsd"> <bean id="userBean" class="com.powernode.spring6.bean.User"/> </beans>

最后就能加载XML配置文件初始化Bean了,注意,该bean对象的实例化是在加载XML配置文件时就实例化完成了。后续只是获取该实例化好的对象。

public class SpringInstantiationTest { @Test public void testConstructor(){ //该Bean对象在这里就实例化完成 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); //这里只是获取该Bean User user = applicationContext.getBean("userBean", User.class); System.out.println(user); } }

运行结果:

2.通过简单工厂模式实例化

第一步:定义一个不带构造方法的Bean:

public class Vip { }

第二步:定义简单工厂方法中的工厂类

public class VipFactory { //注意简单工厂方法定义的是静态方法,方便直接被调用。 public static Vip get(){ return new Vip(); } }

第三步:在Spring配置文件中创建该Bean方法,使用factory-method属性指定方法

<bean id="vipBean" class="com.powernode.spring6.bean.VipFactory" factory-method="get"/>

最后就可以通过加载Spring的XML配置文件获取该bean对象。

@Test public void testSimpleFactory(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); Vip vip = applicationContext.getBean("vipBean", Vip.class); System.out.println(vip); } 3.通过factory-bean实例化

这种方式本质上是通过工厂方法模式来实例化。 第一步:定义一个Bean

public class Order { }

第二步:定义具体工厂类,工厂类中定义实例方法。

public class OrderFactory { public Order get(){ return new Order(); } }

第三步:要使用工厂类来获取目标bean,因为工厂类是具体类,所以要使用其中的方法就要先有该类的实例对象,这里就先将该工厂类定义成一个Bean,再通过调用其中的方法获取目标Order这个Bean出来。下面是Spring的XML配置文件。

<bean id="orderFactory" class="com.powernode.spring6.bean.OrderFactory"/> <bean id="orderBean" factory-bean="orderFactory" factory-method="get"/>

最后测试:

@Test public void testSelfFactoryBean(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); Order orderBean = applicationContext.getBean("orderBean", Order.class); System.out.println(orderBean); } 4.通过FactoryBean接口实例化

以上的第三种方式中,factory-bean是我们自定义的,factory-method也是我们自己定义的。 在Spring中,当你编写的类直接实现FactoryBean接口之后,factory-bean不需要指定了,factory-method也不需要指定了。 factory-bean会自动指向实现FactoryBean接口的类,factory-method会自动指向getObject()方法。 第一步:定义一个Bean

public class Person { }

第二步:编写一个工厂方法类实现FactoryBean接口,表面该工厂是一个Bean工厂。并主要重写其中的getObject()方法,并且如果要在创建对象之前有什么逻辑需求,也就可以在这个getObject()方法返回前进行添加。

public class PersonFactoryBean implements FactoryBean<Person> { @Override public Person getObject() throws Exception { return new Person(); } @Override public Class<?> getObjectType() { return null; } @Override public boolean isSingleton() { // true表示单例 // false表示原型 return true; } }

第三步:这时,就只要在Spring XML配置文件中配置一个FactoryBean就好了。Spring会自动识别出它实现了FactoryBean接口,并调用其中的getObject方法。

<bean id="personBean" class="com.powernode.spring6.bean.PersonFactoryBean"/>

以上,就是Bean的四种实例化方式,感谢“动力节点”的教学。

标签:

Spring中Bean的四种实例化方法由讯客互联其他栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Spring中Bean的四种实例化方法