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

36 篇文章 1 订阅
16 篇文章 6 订阅

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

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

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值