在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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/aophttp://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分別對應無參方法和有參方法。