美食怎么做的小视频网站,企业宣传网站在哪里做,快注销网站,山东网站定制设计公司在spring可以直接通过配置文件获取bean对象#xff0c;如果获取的bean对象还有若干设置#xff0c;需要自动完成#xff0c;可以通过工厂方法获取bean对象。
静态工厂类#xff0c;其中InterfaceUserDao和InterfaceUserService都是自定义的接口#xff0c;可以自己替换。…在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 version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://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实例。1Bean“myFactory001”的静态方法为无参方法。2Bean“myFactory002”的静态方法为有参方法参数分别为基本类型和引用类型。2.2Bean“userDaoRef”为被调用的引用类型类。--!--通过工厂的静态方法获取Bean对象InterfaceUserDao实例。1定义id。2class字段指定的类。3factory-method字段指定的getUserDao010方法该方法的返回值为Bean对象。--bean idmyFactory001classcom.itheima.factory.MyBeanFactory01factory-methodgetUserDao010/bean!--通过工厂的有参静态方法获取Bean对象InterfaceUserService实例。1Bean“myFactory002”的静态方法“getUserService012”是有参方法参数分别为基本类型int和引用类型UserDaoImpl类。--bean idmyFactory002classcom.itheima.factory.MyBeanFactory01factory-methodgetUserService012constructor-arg nameparamValuevalue100/constructor-arg!-- 从userDaoImpl.xml文件import类userDaoImpl001。 --constructor-arg nameparamUserDaorefuserDaoImpl001/constructor-arg/bean
/beans
静态工厂方法注意事项 使用属性id定义bean。使用属性class指定类。使用属性factory-method指定静态方法。 通过id读取的bean为factory-methond的返回值。 方法myFactory001和myFactory002分别对应无参方法和有参方法。