第二步:修改业务服务- 现在业务服务实现了一个新方法setDao(),Spring容器使用该方法传递DAO实现类的引用。该过程称为“设置方法注入(setter injection)”,通过第三步中的配置文件告知Spring容器该过程。注意,不再需要使用DAOFactory,因为Spring BeanFactory提供了这项功能:
public class EmployeeBusinessServiceImpl implements IEmployeeBusinessService {
IEmployeeDAO empDAO;
public List getEmployeesWithinSalaryRange(Map salaryMap){
List empList = empDAO.findBySalaryRange(salaryMap); return empList; } public void setDao(IEmployeeDAO empDAO){ this.empDAO = empDAO; } }
|
请注意P2I的灵活性;即使极大地改动DAO实现,业务服务实现也只需少量更改。这是由于业务服务现在由Spring容器进行管理。
第三步:配置Bean Factory- Spring bean factory需要一个配置文件进行初始化并启动Spring框架。这个配置文件包含所有业务服务和带Spring bean容器的DAO实现类。除此之外,它还包含用于初始化数据源和JdbcDaoSupport的信息:
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <!-- Configure Datasource --> <bean id="FIREBIRD_DATASOURCE" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiEnvironment"> <props> <prop key="java.naming.factory.initial"> weblogic.jndi.WLInitialContextFactory </prop> <prop key="java.naming.provider.url"> t3://localhost:7001 </prop> </props> </property> <property name="jndiName"> <value> jdbc/DBPool </value> </property> </bean>
<!-- Configure DAO --> <bean id="EMP_DAO" class="com.bea.dev2dev.dao.EmployeeDAOImpl"> <property name="dataSource"> <ref bean="FIREBIRD_DATASOURCE"></ref> </property> </bean>
<!-- Configure Business Service --> <bean id="EMP_BUSINESS" class="com.bea.dev2dev.sampleapp.business.EmployeeBusinessServiceImpl"> <property name="dao"> <ref bean="EMP_DAO"></ref> </property> </bean> </beans>
|
这个Spring bean容器通过调用JdbcDaoSupport提供的setDataSource()方法,设置包含DAO实现的数据源对象。
第四步:测试- 最后是编写JUnit测试类。依照Spring的方式,需要在容器外部进行测试。然而,从第三步中的配置文件可以清楚地看到,我们一直在使用WebLogic Server连接池。
|
package com.bea.dev2dev.business;
import java.util.*; import junit.framework.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext;
public class EmployeeBusinessServiceImplTest extends TestCase { private IEmployeeBusinessService empBusiness; private Map salaryMap; List expResult;
protected void setUp() throws Exception { initSpringFramework(); initSalaryMap(); initExpectedResult(); } private void initExpectedResult() { expResult = new ArrayList(); Map tempMap = new HashMap(); tempMap.put("EMP_NO",new Integer(1)); tempMap.put("EMP_NAME","John"); tempMap.put("SALARY",new Double(46.11)); expResult.add(tempMap); } private void initSalaryMap() { salaryMap = new HashMap(); salaryMap.put("MIN_SALARY","1"); salaryMap.put("MAX_SALARY","50"); } private void initSpringFramework() { ApplicationContext ac = new FileSystemXmlApplicationContext ("C:/SpringConfig/Spring-Config.xml"); empBusiness = (IEmployeeBusinessService)ac.getBean("EMP_BUSINESS"); } protected void tearDown() throws Exception { }
/** * Test of getEmployeesWithinSalaryRange method, * of class * com.bea.dev2dev.business.EmployeeBusinessServiceImpl. */ public void testGetEmployeesWithinSalaryRange() { List result = empBusiness.getEmployeesWithinSalaryRange (salaryMap); assertEquals(expResult, result); } }
|
更多内容请看PCdog.com--Spring开发技术篇 Spring框架技术篇 电脑入门教程专题