1、空参构造方法
2、静态工厂方法
3、实例工厂
bean.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.xsd"> <!-- 依赖于空参构造函数,没有空参构造函数将不能创建 --> <!-- 配置service --> <bean id="AccountService" class="com.alphajuns.webstudy.service.impl.AccountServiceImpl"></bean> <!-- 配置dao --> <bean id="AccountDao" class="com.alphajuns.webstudy.dao.impl.AccountDaoImpl"></bean> <!-- 静态工厂方法创建对象,class指向工厂类,factory-method指向工厂类中的静态方法 --> <bean id="AccountService1" class="com.alphajuns.webstudy.util.StaticFactory" factory-method="createAccountService"></bean> <!-- 实例工厂方法创建对象 --> <bean id="InstanceFactory" class="com.alphajuns.webstudy.util.InstanceFactory"></bean> <!-- factory-bean指向实例工厂类,factory-method为实例工厂类的方法 --> <bean id="AccountService2" factory-bean="InstanceFactory" factory-method="createAccountService"></bean> </beans>
AccountServiceImpl.java
静态工厂
实例工厂
测试