4、Spring IOC容器 Bean对象实例化的3种方式

一、构造器实例化(默认方式):要求Bean对象中要提供无参构造

TypeDao:bean对象

public class TypeDao {
    private String str;
    //无参构造
    public TypeDao() {
    }
    public TypeDao(String str) {
        this.str = str;
    }

    public void test(){
        System.out.println("TypeDao Test...");
    }
}

配置文件:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
    构造器实例化
        对应的Bean对象需要提供空构造
    -->
    <bean id="typeDao" class="com.xxxx.dao.TypeDao"></bean>

</beans>

实例化测试:

BeanFactory factory = new ClassPathXmlApplicationContext("spring02.xml");
        TypeDao typeDao = (TypeDao) factory.getBean("typeDao");
        typeDao.test();

如果没有提供无参构造则会报错:
在这里插入图片描述

二、静态工厂实例化:

在这里插入图片描述
Bean对象:

public class TypeService {
    public void test(){
        System.out.println("TypeService Test...");
    }
}

静态工厂类:


/**
 * Created by Jack on 2020.12.18.
 * 定义静态工厂类
 */
public class StaticFactory {
    /**
     * 定义对应的静态方法
     * @return
     */
    public static TypeService createService(){
        return new TypeService();
    }
}

spring配置文件:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
    静态工厂实例化
    1、定义工厂类及对应的静态方法
    2、配置bean对象对应的工厂类及静态方法

    id:需要被实例化的bean对象的id
    class:静态工厂类的路径
    factory-method:静态工厂类中实例化bean对象的方法
    -->
    <bean id="typeService" class="com.xxxx.factory.StaticFactory" factory-method="createService"></bean>

</beans>

实例化测试:

 //静态工厂实例化
        TypeService typeService = (TypeService) factory.getBean("typeService");
        typeService.test();

三、实例化工厂实例化

Bean对象:

public class TypeController {
    public void test(){
        System.out.println("TypeController Test...");
    }
}

实例化工厂:

/**
 * Created by Jack on 2020.12.18.
 * 定义实例化工厂
 */
public class InstanceFactory {
    /**
     * 定义方法
     * @return
     */
    public TypeController createController(){
        return new TypeController();
    }

}

spring配置文件:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
        实例化工厂
        1、定义工厂类及对应的方法
        2、配置工厂对象
        3、配置bean对象对应的工厂对象及工程方法

        factory-bean:工厂对象对应的id属性值
        factory-method:工厂类中的方法
    -->
    <!--工厂对象-->
    <bean id="instanceFactory" class="com.xxxx.factory.InstanceFactory"></bean>
    <!--bean对象-->
    <bean id="typeController" factory-bean="instanceFactory" factory-method="createController"></bean>
</beans>

实例化测试:

//实例化工厂实例化
        TypeController typeController = (TypeController) factory.getBean("typeController");
        typeController.test();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值