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

    1构造函数实例化

    2静态工厂方法实例化

    3实例工厂方法实例化

    service接口:

    package service;
    
    public interface PersonService {
    
        public void save();
    
    }

    PersonServiceBean:

    package service.impl;
    
    import service.PersonService;
    
    public class PersonServiceBean implements PersonService {
    
        public void save(){
            System.out.println("我是save()方法");
        }
    }

    PersonServiceBeanFactory:

    package service.impl;
    
    public class PersonServiceBeanFactory {
        /*
         * 静态工厂方法
         */
        public static PersonServiceBean createPersonServiceBean(){
            return new PersonServiceBean();
        }
        /*
         * 实例工厂方法
         */
        public  PersonServiceBean createPersonServiceBean2(){
            return new PersonServiceBean();
        }
    }

    applicationContext.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-3.1.xsd">
        
            <!-- 默认构造方法 -->
            <bean id = "personService" class = "service.impl.PersonServiceBean"></bean>
            
            <!-- 静态工厂方式 -->
            <bean id = "personService2" class = "service.impl.PersonServiceBeanFactory" 
                                   factory-method = "createPersonServiceBean"></bean>
                                   
            <!-- 实例工厂方式 -->
             <bean id = "personServiceFactory" class = "service.impl.PersonServiceBeanFactory"></bean>
            <bean id = "personService3" factory-bean = "personServiceFactory" 
                                   factory-method = "createPersonServiceBean2"></bean>
       
    </beans>

    测试类:

    package test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import service.PersonService;
    import service.impl.PersonServiceBean;
    
    /**
    * @ClassName: FirstTest 
    * @Description: 测试实spring三种实例化方式
    * @author 无名
    * @date 2016-02-10
    * @version 1.0
     */
    public class FirstTest
    {
        @Test
        public void testCreateBean(){
            String conf = "applicationContext.xml";
            ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
            
            //利用构造器来实例化
            PersonService ps = ac.getBean("personService",PersonServiceBean.class);
            ps.save();
            
            //利用静态工厂方法
            PersonService ps02 = (PersonService)ac.getBean("personService2");
            ps02.save();
            
            //实例工厂方法
            PersonService ps03 = (PersonService)ac.getBean("personService3");
            ps03.save();
            
        }
    }
  • 相关阅读:
    jsoup
    【伪装位置神器】神行者AnyLocation 1.3.0001可用于微信,陌陌
    MD5 哈希等各种加密方式 都是对这个对象进行各种运算, 然后得出1个字符串
    【html】param 以及 embed 的有关 flash 属性详解
    【css】绝对定位的元素在 ie6 下不显示
    【javascript】浮点数运算问题分析及解决方法
    【jquery】邮箱自动补全 + 上下翻动
    【javascript】设为首页——setHome
    【javascript】js 检验密码强度
    【jquery】jquery 自定义滚动条
  • 原文地址:https://www.cnblogs.com/rixiang/p/5185876.html
Copyright © 2011-2022 走看看