zoukankan      html  css  js  c++  java
  • 一步一步深入spring(2)-三种方式来实例化bean

    一步一步深入spring(1)--搭建和测试spring的开发环境中提到了一种实例化bean的方式,也是最基本的使用构造器实例化bean

    1.使用构造器实例化bean:这是最简单的方式,Spring IoC容器即能使用默认空构造器

    2.使用静态工厂方式实例化Bean,使用这种方式除了指定必须的class属性,还要指定factory-method属性来指定实例化Bean的方法,而且使用静态工厂方法也允许指定方法参数,spring IoC容器将调用此属性指定的方法来获取Bean,配置如下所示:

    (1)先编写一个静态工厂类方法:

     1 package junit.test;
     2 
     3 import com.yangyang.PersonService;
     4 import com.yangyang.impl.PersonServiceImpl;
     5 
     6 public class PersonServiceBeanFactory {
     7      //工厂方法
     8     public static PersonService newInstance(){
     9         //返回需要的实例
    10         return new PersonServiceImpl();
    11     }
    12 
    13 }
    View Code

    (2)编写配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
     3     "http://www.springframework.org/dtd/spring-beans.dtd">
     4 
     5 <beans>
     6   <!-- id 表示你这个组件的名字,class表示组件类 -->
     7 <!--     <bean id="personService" class="com.yangyang.impl.PersonServiceImpl"></bean> -->
     8   <!-- 通过使用静态工厂方式实例化Bean -->
     9     <bean id="personService2" class="junit.test.PersonServiceBeanFactory" factory-method="newInstance"></bean>
    10 </beans>
    View Code

    (3)编写单元测试

    1     ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/beans.xml");
    2         //从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现”
    3         //通过使用静态工厂方式实例化Bean
    4         PersonService personService2=(PersonService) ctx.getBean("personService2",PersonService.class);
    5         personService2.sayHello();
    View Code

    也能像方法一的一样出现hello world字样,实例化bean成功

    3.使用实例工厂方法实例化Bean:使用这种方式不能指定class属性,此时必须使用factory-bean属性来指定工厂Beanfactory-method属性指定实例化Bean的方法,而且使用实例工厂方法允许指定方法参数,方式和使用构造器方式一样,配置如下:

    (1)先编写实例工厂类:

     1 package junit.test;
     2 
     3 import com.yangyang.PersonService;
     4 import com.yangyang.impl.PersonServiceImpl;
     5 
     6 public class PersonServiceBeanFactory {
     7     //实例化工厂方法
     8     public PersonService newInstance(){
     9         return new PersonServiceImpl();
    10     }
    11 
    12 }
    View Code

    (2)编写配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
     3     "http://www.springframework.org/dtd/spring-beans.dtd">
     4 
     5 <beans>
     6   <!-- id 表示你这个组件的名字,class表示组件类 -->
     7     <!--1、定义实例工厂Bean  --> 
     8     <bean id="beanInstanceFactory" class="junit.test.PersonServiceBeanFactory"/>
     9     <!-- 2、使用实例工厂Bean创建Bean  -->
    10     <bean id="personService3"  factory-bean="beanInstanceFactory" factory-method="newInstance"></bean>
    11 </beans>
    View Code

    (3)编写单元测试:

    1     //读取配置文件实例化一个IoC容器
    2         ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/beans.xml");
    3         //通过实例工厂类实例化Bean
    4         PersonService personService2=(PersonService) ctx.getBean("personService3",PersonService.class);
    5         personService2.sayHello();
    View Code

    也能像方法一的一样出现hello world字样,实例化bean成功

    ----------------------------------------------------------------------------------------
    如果您觉得阅读本文对您有帮助,请微信扫码关注作者,与我进行交流!欢迎各位转载,转载文章之后须在文章页面明显位置给出作者和原文连接,谢谢。
  • 相关阅读:
    HDU 2236 无题Ⅱ
    Golden Tiger Claw(二分图)
    HDU 5969 最大的位或 (思维,贪心)
    HDU 3686 Traffic Real Time Query System (图论)
    SCOI 2016 萌萌哒
    Spring Boot支持控制台Banner定制
    构建第一个Spring Boot程序
    Spring Boot重要模块
    Java fastjson JSON和String互相转换
    BCompare 4 Windows激活方法【试用期30天重置】
  • 原文地址:https://www.cnblogs.com/shunyang/p/3236271.html
Copyright © 2011-2022 走看看