Spring(四)基于XML装配bean(实例化方式)

网友投稿 580 2022-09-01

Spring(四)基于XML装配bean(实例化方式)

Spring(四)基于XML装配bean(实例化方式)

基于xml装配bean 的实例化方式共有三种 1.默认构造 2.静态工厂 3.实例化工厂

1默认构造

1.1 说明

用于生成实例化对象,必须未重写bean的默认构造方法。

1.2 xml配置

必须提供默认构造方法

id 为bean的别名,用于之后从spring容器获得实例时使用的 class 为需要创建实例的全限定类名

1.3 目标类

创建目标类的过程

UsersService接口和实现类UsersServiceImpl 使用junit测试 重写UsersServiceImpl的构造方法 使用junit测试 测试代码如下: UsersService接口 仅仅为了测试 写了一个show方法

package com.scx.test;public interface UsersService { public void show();}

UsersService实现类UsersServiceImpl ,重写show方法,输出UsersService,使用的是默认的构造方法

package com.scx.test;public class UsersServiceImpl implements UsersService @Override public void show() { System.out.println("默认构造创建实例"); }}

xml配置

使用junit测试

package com.scx.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactory { @Test public void testConstract(){ String xmlPath="com/scx/test/applicationContext.xml"; ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath); UsersService usersService=applicationContext.getBean("UsersServiceId",UsersService.class); usersService.show();

运行结果:

成功输出了结果

重写UsersServiceImpl的构造方法

package com.scx.test;public class UsersServiceImpl implements UsersService @Override public void show() { System.out.println("UsersService"); } //重写UsersServiceImpl构造方法 public UsersServiceImpl(String str){ }}

再次使用junit测试

并未输出结果

比较可知:

若想使用默认构造实例化,必须未重写默认的构造方法

2静态工厂

2.1 说明

常用于Spring整合其它框架(工具)用于生成实例化对象,所有的方法必须是static

2.2 XML配置

id 为bean的别名,用于之后从spring容器获得实例时使用的 class 为需要创建工厂的全限定类名 factory-method 为工厂的静态方法

2.3 目标类

创建目标类的过程

UsersService接口和实现类UsersServiceImpl 静态工厂MyStaticFactory 使用junit测试

UsersService接口和实现类UsersServiceImpl代码和上面的一样就不再贴了

静态工厂MyStaticFactory ,其中的静态方法createUsersService,返回一个UsersServiceImpl 实例

package com.scx.factory.static_test;/* * 静态工厂创建实例 */public class MyStaticFactory { //静态方法 public static UsersServiceImpl createUsersService(){ return new

xml配置

使用junit测试

package com.scx.factory.static_test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactory { @Test public void testStaticFactory(){ String xmlPath="com/scx/factory/static_test/applicationContext.xml"; ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath); UsersService usersService=applicationContext.getBean("UsersServiceId",UsersService.class); usersService.show();

由于在静态工厂中的静态方法中,我们自己new 的UsersServiceImpl 对象,所以重写构造方法与否无影响。

运行结果:

3实例工厂

3.1 说明

提供的所有方法都是非静态的实例化之前必须先有工厂的实例化对象,通过实例化对象创建对象

3.2 XML配置

3.3 目标类

创建目标类的过程

UsersService接口和实现类UsersServiceImpl 实例工厂MyFactory 使用junit测试

UsersService接口和实现类UsersServiceImpl代码和上面的一样就不再贴了 实例工厂MyFactory

package com.scx.factory.static_test;/* * 实例工厂创建实例 */public class MyFactory { //普通方法 public UsersServiceImpl createUsersService(){ return new

XML配置

junit测试代码

package com.scx.factory.static_test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactory { @Test public void testFactory(){ String xmlPath="com/scx/factory/static_test/applicationContext.xml"; ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath); UsersService usersService=applicationContext.getBean("UsersServiceId",UsersService.class); usersService.show();

运行结果:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:PHP主流框架优缺点汇总(PHP主流框架)
下一篇:了解PHP核心知识点(php必背知识点)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~