• Spring第一课:基于XML装配bean(四),三种实例化方式:默认构造、静态工厂、实例工厂


    Spring中基于XML中的装配bean有三种方式:

    1.默认构造

    2.静态工厂

    3.实例工厂

    1.默认构造

        在我们在Spring的xml文件中直接通过:
        <bean id="名字" class="全限定类名" ></bean>
        来写的时候,默认走的是类的默认构造,当我们写了自己的构造方法(就算是无参的)
        也是会覆盖默认的构造的,在这种情况下,这种
        默认构造实例化bean的方式就不能够使用了!

    2.静态工厂

    同样是解耦中的service层和serviceImpl:

    service

    package com.d_inject.b_static_factory;
    
    public interface UserService {
    	boolean addUser();
    }
    

    serviceImpl

    package com.d_inject.b_static_factory;
    
    public interface UserService {
    	boolean addUser();
    }
    

    提供一个静态工厂:

    package com.d_inject.b_static_factory;
    /**
      * 注意静态工厂的方法是静态的。
     * 
     * @author mzy
     *
     */
    public class MyBeanFactory { 
    	/**
    	 * 创建实例
    	 * @return
    	 */
    	public static UserService createService() {
    		return new UserServiceImpl();
    	}
    }
    

    Spring的xml配置文件中写的关于使用静态工厂去创建serviceImpl对象的配置:

    <?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">
    	<!-- 
    	将静态工厂创建的实例交予Spring去做,
    	class 确定静态工厂的全限定名称
    	factory-method:确定静态方法
    	 -->
    	 <bean id="userService" class="com.d_inject.b_static_factory.MyBeanFactory" factory-method="createService"></bean>
    </beans>

    在我们没有学习Spring之前的时候手动使用静态工厂,创建对象:

    package com.d_inject.b_static_factory;
    
    import org.junit.jupiter.api.Test;
    
    public class TestStaticFactory {
    	/**
    	 * 在没有学习Spring之前,我们创建一个对象的方式
    	 * 自定义静态工厂去做创建的事情实现解耦。
    	 */
    	@Test
    	public void testMyBeanFactoryByCustom() {
    		UserService service = MyBeanFactory.createService();
    		service.addUser();
    	}
    	
    }
    

    测试通过ClassPathXmlApplicationContext,进行getBean:

    package com.d_inject.b_static_factory;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestStaticFactory {
    	/**
    	 * Spring工厂
    	 */
    	@Test
    	public void testMyBeanFactoryBySpring() {
    		String xmlPath = "com/d_inject/b_static_factory/beans.xml";
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    		// UserService service = (UserService) applicationContext.getBean("userService");
    		UserService service = applicationContext.getBean("userService", UserService.class);
    		service.addUser();
    	}
    }
    

    3.实例工厂:

    同样也是如上的service和serviceImpl:

    只是上面的静态工厂被改成了实例工厂,实例工厂中只能通过工厂对象来调用方法,不能使用静态方法:

    实例工厂:

    package com.d_inject.c_factory;
    /**
     * 	实例工厂:
     * 		必须要有工厂实例对象,通过实例对象创建对象。
     * 		提供所有的方法都是非静态的。
     * 
     * @author mzy
     *
     */
    public class MyBeanFactory { 
    	/**  
    	  * 创建实例
    	 * @return
    	 */
    	public UserService createService() {
    		return new UserServiceImpl();
    	}
    }
    

    Spring的配置文件:

    就先要通过bean配置能够获得工厂对象,再通过factory-bean和factory-method来确定具体的通过实例工厂的哪个方法获得对象:

    <?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="myBeanFactory" class="com.d_inject.c_factory.MyBeanFactory"></bean>
    	 <!-- 
    	 获得userService:
    	 	factory-bean:确定工厂实例< 从该配置文件的bean中找 >
    	 	factory-method:确定通过工厂实例调用的方法。
    	  -->
    	 <bean id="userService" factory-bean="myBeanFactory" factory-method="createService"></bean>
    </beans>

    最后,在没有学Spring之前,通过实例工厂创建对象的方式:

         /**
    	  * 实例工厂
    	  * 
    	  * 在没有学习Spring之前,我们创建一个对象的方式 自定义工厂
    	  * 实例工厂,实现解耦。
    	 */
    	@Test
    	public void testMyBeanFactoryByCustom() {
    		MyBeanFactory factory = new MyBeanFactory();
    		UserService service = factory.createService();
    		service.addUser();
    	}

    Spring通过实例工厂的方式:

        /**
    	 * Spring工厂
    	 */
    	@Test
    	public void testMyBeanFactoryBySpring() {
    		String xmlPath = "com/d_inject/c_factory/beans.xml";
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    		// UserService service = (UserService)
    		// applicationContext.getBean("userService");
    		UserService service = applicationContext.getBean("userService", UserService.class);
    		service.addUser();
    	}
  • 相关阅读:
    第四章 springboot + swagger
    第三章 springboot + jedisCluster
    第二章 第二个spring-boot程序
    mac下的一些命令
    Redis(二十一):Redis性能问题排查解决手册(转)
    TreeMap升序|降序排列和按照value进行排序
    关于java集合类TreeMap的理解(转)
    Redis(二十):Redis数据过期和淘汰策略详解(转)
    Redis(十九):Redis压力测试工具benchmark
    try、finally代码块有无return时的执行顺序
  • 原文地址:https://www.cnblogs.com/mzywucai/p/11053367.html
Copyright © 2020-2023  润新知