一、使用静态工厂实例化bean
1.1、OrderDao接口与实现类
// 接口
public interface OrderDao {
void save();
}
// 实现类
public class OrderDaoImpl implements OrderDao {
public void save() {
System.out.println("order dao save ...");
}
}
1.2、OrderFactory类
public class OrderDaoFactory {
public static OrderDao getOrderDao(){
System.out.println("factory setup");
return new OrderDaoImpl();
}
}
1.3、spring配置applicationContext.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">
<!--使用静态工厂实例化bean-->
<bean id="orderDao" class="com.itheima.factory.OrderDaoFactory" factory-method="getOrderDao"></bean>
</beans>
1.4、使用
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
OrderDao orderDao = (OrderDao)ctx.getBean("orderDao");
orderDao.save();
}
1.5、总结
- 在xml中配置class的工厂类,factory-method指定工厂实例方法
二、 使用实例工厂实例化Bean
2.1、UserDao接口和实现类
// 接口
public interface UserDao {
void save();
}
// 实现类
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("user dao save...");
}
}
2.2、UserDaoFactory类
public class UserDaoFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
2.3、配置applicationContext.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">
<!--方式三: 使用实例工厂实例化bean-->
<bean id="userFactory" class="com.itheima.factory.UserDaoFactory" />
<bean id="userDao" factory-bean="userFactory" factory-method="getUserDao"/>
</beans>
2.4、使用
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao)ctx.getBean("userDao");
userDao.save();
}
2.5、总结
- 设置userFactory的Bean
- 设置userDao的Bean,配置factory-bean为上一步userFactory,配置factory-method为工厂类中的getUserDao方法
三、 使用FactoryBean实例化
3.1、UserDaoFactoryBean类
public class UserDaoFactoryBean implements FactoryBean<UserDao> {
// 代替原始实例工厂中创建对象的方法
public UserDao getObject() throws Exception {
return new UserDaoImpl();
}
public Class<?> getObjectType() {
return UserDao.class;
}
}
3.2、applicationContext.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">
<!--方式四: 使用FactoryBean实例化-->
<bean id="userDao" class="com.itheima.factory.UserDaoFactoryBean" />
</beans>
3.3、使用
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao)ctx.getBean("userDao");
userDao.save();
}
3.4、总结
- UserDaoFactoryBean实现接口FactoryBean<UserDao>
- 实现getObject接口和getObjectType方法
文章来源地址https://www.toymoban.com/news/detail-612369.html
文章来源:https://www.toymoban.com/news/detail-612369.html
到了这里,关于spring学习笔记三的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!