zoukankan      html  css  js  c++  java
  • Spring的三种实例化Bean的方式

    Spring提供了三种实例化Bean的方式

    1.使用类构造器实例化

    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>

    2.使用静态工厂方法实例化

    我们在编码剖析Spring管理Bean的原理案例的基础上使用这种方式来实例化bean。
    首先我们要在cn.itcast.service.impl包中创建一个工厂类——PersonServiceBeanFactory.java,其代码如下:

    public class PersonServiceBeanFactory {
        public static PersonServiceBean createPersonServiceBean() {
            return new PersonServiceBean();
        }
    }

    然后修改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
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
        <bean id="personService2" class=" cn.itcast.service.impl.PersonServiceBeanFactory"
              factory-method="createPersonServiceBean" />
    
    </beans>

    最后,将SpringTest类的改为:

    public class SpringTest {
    
        @Test
        public void test() {
            // ApplicationContext是接口
            ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
            PersonService personService = (PersonService) ctx.getBean("personService2"); // 从Spring容器取得bean
            personService.save();
        }
    
    }

    测试test()方法,Eclipse控制台打印如下: 
    这里写图片描述

    3.使用实例工厂方法实例化

    我们同样在编码剖析Spring管理Bean的原理案例的基础上使用这种方式来实例化bean。
    首先我们要修改工厂类——PersonServiceBeanFactory.java的代码为:

    public class PersonServiceBeanFactory {
        public static PersonServiceBean createPersonServiceBean() {
            return new PersonServiceBean();
        }
    
        public PersonServiceBean createPersonServiceBean2() {
            return new PersonServiceBean();
        }
    }

    紧接着修改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
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
        <bean id="personService2" class=" cn.itcast.service.impl.PersonServiceBeanFactory"
              factory-method="createPersonServiceBean" />
        <bean id="personServiceBeanFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory"></bean>
        <bean id="personService3" factory-bean="personServiceBeanFactory" factory-method="createPersonServiceBean2"></bean>
    </beans>

    最后,将SpringTest类的改为:

    public class SpringTest {
    
        @Test
        public void test() {
            // ApplicationContext是接口
            ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
            PersonService personService = (PersonService) ctx.getBean("personService3"); // 从Spring容器取得bean
            personService.save();
        }
    
    }
    • 测试test()方法,Eclipse控制台打印如下: 
      这里写图片描述

    Spring提供了三种实例化Bean的方式,那么到底该使用哪种方式较稳妥呢?应根据实际情况决定,但可这样说,90%的可能都是采用第一种方式,即使用类构造器实例化bean。

  • 相关阅读:
    php method_exists( $object , string $method_name )
    php伪类型 (mixed)
    6.6-2-数组与数据结构(用数组及其函数实现堆栈等数据结构)
    6.6-1-php数组相关(2)
    2017.6.5-2-php数组相关(1)
    2017.6.5-1-php函数应用及流程控制
    CodePage
    bat批处理教程
    pip安装及源
    CentOS安装Python3
  • 原文地址:https://www.cnblogs.com/zbdouble/p/9227605.html
Copyright © 2011-2022 走看看