欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring基于XML装配bean的三种实例化方式

程序员文章站 2022-05-24 17:43:15
...

说明

好久没有写博客了,觉得一个星期至少写一篇吧,所以就动手了,而且我Spring的基础不行呀,毕竟看视频自学都是很杂的,有时候听的课都不是很系统的,所以导致有些知识点不全什么的,所以在真正实践起来发现自己的问题之后又要重新学一遍,确实没有人指导你学习的方向走的弯路会比较多。不过不怕,大不了回头重新来,而且有些知识点再听一遍跟你听第一遍的感觉完全是不一样的,会感觉轻松很多,又理解得更深刻了。

实例化方式

首先我们应该知道Spring中基于XML装配bean时有三种实例化方式,分别是

  1. 默认构造
  2. 静态工厂
  3. 实例工厂
    下面就一个个来说一下,具体说第二,三方式

service及service实现类

在这里我们先有一个UserService以及UseServiceImpl类
待会我们会通过装配bean得到UseService类的一个实例
首先是UserService的接口代码

package com.shanmu;

interface UserService{
	void saveUser();
}

下面是UseServiceImpl实现了UserService接口

package com.shanmu;

public class UseServiceImpl implements UserService{

	@Override
	public void saveUser(){
		System.out.print("i am a user");
	}
}

这里就不去写一个User类了,所以直接输出就可以了。

默认构造

我们一般采用Spring构造bean的时候都是采用在spring.xml(src目录下的配置文件,这里我取名为spring)中通过bean标签配置,一般刚入门的人会采用下面的配置方法来得到一个UseServiceImpl实例

<bean name=“userService” class="com.shanmu.UseServiceImpl"></bean>

这样的话在我们的主程序中就可以直接通过得到bean的方式来得到UseServiceImpl实例,这里写一个main函数

package com.shanmu

public class TestMain(){

	public static void main(String[] args){
		//不采用spring
		UserService userService =new UseServiceImpl();
		userService.saveUser();

	    //采用spring默认构造方法
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		UserService userService =(UserService)context.getBean("userService");
		userService.saveUser();
	}
}

所以,我们知道spring就是用来帮我们创建对象的

静态工厂

静态工厂是用于生成实例对象的,工厂类中的所有方法必须是静态的
首先我们需要一个工厂类,这个工厂类中有一个静态方法帮我们得到了UseServiceImpl实例,所以我们后面只需要直接调用静态方法即可
下面是工厂类,通过这个类我们就可以得到UserService

package com.shanmu;

public class MyBeanFactory(){
	public static UserService createService(){
		return new UserServiceImpl();
	}
}

所以在spring.xml中可以这样配置

<bean name=“userService” class="com.shanmu.MyBeanFactory" factory-method="CreateService">
</bean>

下面是TestMain,同样会有使用spring以及不使用spring的对比

package com.shanmu

public class TestMain(){

	public static void main(String[] args){
		//不采用spring
		UserService userService=MyBeanFactory.createService();
		userService.saveUser();

	    //采用spring默认构造方法
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		UserService userService =(UserService)context.getBean("userService");
		userService.saveUser();
	}
}

实例工厂

实例工厂一样是需要有一个工厂类,但是与静态工厂不一样的地方在于里面的方法都是“非静态的”,所以我们需要得到一个工厂类的实例才能得到一个UseServiceImpl实例
下面看一下实例工厂的代码

package com.shanmu;

public class MyBeanFactory(){
	public UserService createService(){
		return new UserServiceImpl();
	}
}

我们需要在spring.xml中这样配置

<bean name=“myBeanFactory” class="com.shanmu.MyBeanFactory">
</bean>
<bean name=“userService” factory-bean="myBeanFactory" factory-method="createService">
</bean>

下面写一个TestMain方法

package com.shanmu

public class TestMain(){

	public static void main(String[] args){
		//不采用spring
		MyBeanFactory myBeanFactory=new MyBeanFactory();
		UserService userService=myBeanFactory.createService();
		userService.saveUser();

	    //采用spring默认构造方法
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		UserService userService =(UserService)context.getBean("userService");
		userService.saveUser();
	}
}

结果

i am a user

分析

会发现在我们修改bean的三种实例化方式的时候,使用spring方式的TestMain方法其实一直都没有什么变化,变化的一直都是不使用spring方式的TestMain方法。