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

来源:互联网 发布:飞利浦呼吸机读卡软件 编辑:程序博客网 时间:2024/04/30 22:37

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

1默认构造

1.1 说明

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

1.2 xml配置

<bean id="" class="">  必须提供默认构造方法

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配置

<?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="UsersServiceId" class="com.scx.test.UsersServiceImpl"></bean></beans>

使用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配置

<bean id=""  class=""  factory-method="">

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 UsersServiceImpl();    }}

xml配置

<?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">    <!--         >id      为bean的别名,用于之后从spring容器获得实例时使用的        >class   为需要创建工厂的全限定类名        >factory-method 为工厂的静态方法     -->    <bean id="UsersServiceId" class="com.scx.factory.static_test.MyStaticFactory"    factory-method="createUsersService"></bean></beans>

使用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配置

<bean name="" class=""></bean><!-- 创建工厂实例--><bean name="" factory-bean="" factory-method=""></bean><!-- factory-bean:确定工厂实例  factory-method:确定普通方法 -->

3.3 目标类

创建目标类的过程

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

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

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

XML配置

<?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 name="MyFactoryId" class="com.scx.factory.static_test.MyFactory"></bean>    <bean name="UsersServiceId" factory-bean="MyFactoryId" factory-method="createUsersService"></bean></beans>

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();    }}

运行结果:
这里写图片描述

1 0