1 CostDao.java 持久化层接口
package dao;
public interface CostDao {
public void save();
public void delete();
}
2 两个实现类JdbcCostDao.java & HibernateCostDao.java
package dao;
public class JdbcCostDao implements CostDao {
public JdbcCostDao() {
System.out.println("创建了一个JdbcCostDao对象");
}
@Override
public void save() {
System.out.println("Jdbc save()");
}
@Override
public void delete() {
System.out.println("Jdbc delete()");
}
}
package dao;
public class HibernateCostDao implements CostDao {
public void myinit() {
System.out.println("hibernate myinit()");
}
public HibernateCostDao() {
System.out.println("创建了一个HibernateCostDao对象");
}
@Override
public void save() {
System.out.println("hibernate save()");
}
@Override
public void delete() {
System.out.println("hibernate delete()");
}
}
3 DeleteAction.java
package action;
import dao.CostDao;
public class DeleteAction {
private CostDao dao;
// 配置文件中name="dao" 被传到参数dao,而不是类的成员
public void setDao(CostDao dao) {
this.dao = dao;
}
public String execute() {
System.out.println("开始处理资费删除请求");
dao.delete();
return "success";
}
}
4 导包
5 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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<!--
prototype 原型(默认),Bean对象在getBean方法调用时创建
singleton 单例,Bean对象在容器实例化时创建
lazy-init Bean对象在getBean方法调用时创建
init-method 指定初始化方法,在对象创建之后自动调用。
destroy-method 在容器销毁时,释放单例的Bean对象,自动调用mydesctory方法。(该属性仅适用于singleton模式的Bean对象)
-->
<bean id="hibernateCostDao" scope="singleton" lazy-init="true" init-method="myinit" class="dao.HibernateCostDao"></bean>
<bean id="jdbcCostDao" class="dao.JdbcCostDao"></bean>
<bean id="deleteAction" scope="prototype" class="action.DeleteAction">
<!-- ref可以指向不同的持久层bean -->
<property name="dao" ref="hibernateCostDao"></property>
</bean>
</beans>
6 测试
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.CostDao;
public class TestBean {
@Test
public void testCreate() {
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
CostDao dao1 = (CostDao) ac.getBean("hibernateCostDao");
CostDao dao2 = (CostDao) ac.getBean("hibernateCostDao");
// 测试单例
System.out.println(dao1 == dao2);
}
}
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import action.DeleteAction;
public class TestAction {
@Test
public void testDelete() {
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
DeleteAction action = (DeleteAction) ac.getBean("deleteAction");
action.execute();
}
}