1. 调用类的无参构造函数,如果没有无参构造函数则不能成功创建
<?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="accountDao" class="xust.student.dao.Impl.AccountDaoImpl"></bean>
</beans>
2.使用Spring管理静态工厂
package xust.student.factory;
import xust.student.service.AccountService;
import xust.student.service.Impl.AccountServiceImpl;
public class StaticFactory {
public static AccountService createAccountService(){
return new AccountServiceImpl();
}
}
<?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="accountService" class="xust.student.factory.StaticFactory" factory-method="createAccountService"></bean>
</beans>
3.spring管理实例工厂
package xust.student.factory;
import xust.student.service.AccountService;
import xust.student.service.Impl.AccountServiceImpl;
public class Factory {
public AccountService createAccountService(){
return new AccountServiceImpl();
}
}
<?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="Factory" class="xust.student.factory.Factory"></bean>
<bean id="accountService" factory-bean="Factory" factory-method="createAccountService"></bean>
</beans>