spring:使用标签xml静态工厂方法获取bean

发布于:2025-06-12 ⋅ 阅读:(20) ⋅ 点赞:(0)

在spring可以直接通过配置文件获取bean对象,如果获取的bean对象还有若干设置,需要自动完成,可以通过工厂方法获取bean对象。

静态工厂类,其中InterfaceUserDao和InterfaceUserService都是自定义的接口,可以自己替换。在这里就不贴出接口定义了。

package com.itheima.factory;


import com.itheima.dao.interfaces.InterfaceUserDao;
import com.itheima.dao.impl.UserDaoImpl;
import com.itheima.service.interfaces.InterfaceUserService;
import com.itheima.service.impl.UserServiceImpl;


/**
 * @copyright 2003-2024
 * @author    qiao wei
 * @date      2024-12-24
 * @version   1.0
 * @brief     静态工厂方法返回Bean。该模式的特点是,可以在返回Bean之前对Bean按需配置,随后再返回。或者Bean不是由
 *            构造方法创建。
 * @history   name
 *            date
 *            brief
 */
public class MyBeanFactory01 {
    
    /* *
     * @author  qiao wei
     * @brief   无参静态方法。
     * @param   
     * @return  
     * @throws  
     * @history name
     *          date
     *          brief
     */
    public static InterfaceUserDao getUserDao010() {
        /**
         * 对返回的Bean做操作或配置。。。
         * 通过非构造方法创建。
         */
        
        return new UserDaoImpl();
    }
    
    /* *
     * @author  qiao wei
     * @brief   基础类型参数方法。
     * @param   
     * @return  
     * @throws  
     * @history name
     *          date
     *          brief
     */
    public static InterfaceUserService getUserService01(int paramValue) {
        return new UserServiceImpl();
    }
    
    /* *
     * @author  qiao wei
     * @brief   多参数,且含有引用类型参数方法。
     * @param   
     * @return  
     * @throws  
     * @history name
     *          date
     *          brief
     */
    public static InterfaceUserService getUserService012(
        int paramValue,
        InterfaceUserDao paramUserDao) {
        
        return new UserServiceImpl();
    }
}

 配置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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    
    
    <!--  引入外部资源。  -->
    <import resource="../dao/userDaoImpl.xml"/>
    
    
    <!--
        使用工厂的静态方法获取Bean实例。
        1:Bean“myFactory001”的静态方法为无参方法。
        2:Bean“myFactory002”的静态方法为有参方法,参数分别为基本类型和引用类型。
        2.2:Bean“userDaoRef”为被调用的引用类型类。
     -->
    
    <!--
        通过工厂的静态方法获取Bean对象(InterfaceUserDao实例)。
        1:定义id。
        2:class字段指定的类。
        3:factory-method字段指定的getUserDao010方法,该方法的返回值为Bean对象。
     -->
    <bean id="myFactory001"
          class="com.itheima.factory.MyBeanFactory01"
          factory-method="getUserDao010">
    </bean>
    
    <!--
        通过工厂的有参静态方法获取Bean对象(InterfaceUserService实例)。
        1:Bean“myFactory002”的静态方法“getUserService012”是有参方法,参数分别为基本类型int和引用类型UserDaoImpl类。
     -->
    <bean id="myFactory002"
          class="com.itheima.factory.MyBeanFactory01"
          factory-method="getUserService012">
        
        <constructor-arg name="paramValue"
                         value="100">
        </constructor-arg>
        
        <!-- 从userDaoImpl.xml文件import类userDaoImpl001。 -->
        <constructor-arg name="paramUserDao"
                         ref="userDaoImpl001">
        </constructor-arg>
    </bean>
</beans>

静态工厂方法注意事项:

  • 使用属性id定义bean。
  • 使用属性class指定类。
  • 使用属性factory-method指定静态方法。

通过id读取的bean为factory-methond的返回值。

方法myFactory001和myFactory002分别对应无参方法和有参方法。