我用的IDE是Eclipse2.1.2 下面是要引入的JAR包 <?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="var" path="JRE_LIB" sourcepath="JRE_SRC"/> <classpathentry kind="lib" path="D:/classes/spring.jar"/> <classpathentry kind="lib" path="D:/classes/junit-3.8.1.jar"/> <classpathentry kind="lib" path="D:/classes/commons-logging-1.0.3.jar"/> <classpathentry kind="lib" path="D:/classes/spring-dao.jar"/> <classpathentry kind="lib" path="D:/classes/mysql-connector-java-3.0.9-stable-bin.jar"/> <classpathentry kind="lib" path="D:/classes/commons-dbcp-1.1.jar"/> <classpathentry kind="lib" path="D:/classes/commons-pool-1.1.jar"/> <classpathentry kind="lib" path="D:/classes/commons-collections.jar"/> <classpathentry kind="lib" path="D:/classes/commons-beanutils.jar"/> <classpathentry kind="lib" path="D:/classes/commons-lang-2.0.jar"/> <classpathentry kind="lib" path="D:/classes/hibernate2.jar"/> <classpathentry kind="lib" path="D:/classes/jta.jar"/> <classpathentry kind="lib" path="D:/classes/dom4j-1.4.jar"/> <classpathentry kind="lib" path="D:/classes/ehcache.jar"/> <classpathentry kind="lib" path="D:/classes/odmg.jar"/> <classpathentry kind="lib" path="D:/classes/cglib-2.0-rc2.jar"/> <classpathentry kind="output" path="bin"/> </classpath> 今天還是先看看Spring的配置文件吧,Hibernate和Spring融合,大部分功能由配置文件搞定了. Hibernate-Context.xml文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <description>Spring Quick Start</description> <!-- datasource --> <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value></value> </property> </bean> <!-- hibernate support --> <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean" > <property name="dataSource"> <ref local="datasource"/> </property> <property name="mappingResources"> <list> <value>com/bcxy/dao/hibernate/Test.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> net.sf.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> </bean> <!-- hibernate dao --> <bean id="hibernatedao" class="com.bcxy.spring.dao.TestDAO" > <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> Spring管理了Hibernate的Session,確實(shí)方便了很多. 寫個(gè)接口先 ITtestDAO.java文件 package com.bcxy.spring.dao; import com.bcxy.dao.hibernate.Test; public interface ITestDAO { void insertTest(Test test); } TestDAO.java文件 package com.bcxy.spring.dao; import org.springframework.orm.hibernate.support.HibernateDaoSupport; import com.bcxy.dao.hibernate.Test; public class TestDAO extends HibernateDaoSupport implements ITestDAO { public void insertTest(Test test) { // getHibernateTemplate().save(test); } } 需要繼承HibernateDaoSupport類 這么少的幾行代碼,能用嗎?來用JUnit測(cè)試一下吧. /* * Created on 2004-10-9 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package test; import java.io.FileInputStream; import java.io.FileNotFoundException; import junit.framework.TestCase; import org.springframework.beans.BeansException; import org.springframework.beans.factory.xml.XmlBeanFactory; import com.bcxy.dao.hibernate.Test; import com.bcxy.spring.dao.ITestDAO; public class TestDAOTest extends TestCase { ....... public void testInsertTest() throws BeansException, FileNotFoundException { XmlBeanFactory beans = new XmlBeanFactory(new FileInputStream("Hibernate-Context.xml")); ITestDAO tests = (ITestDAO)beans.getBean("hibernatedao"); Test test = new Test(); test.setId(8); test.setUn("testtest"); test.setPw("testtest"); tests.insertTest(test); } ...... } 真的可以用啊,太厲害了--Spring!!! 這段代碼似乎并沒有什么特殊,但有一個(gè)細(xì)微之處: ITestDAO tests = (ITestDAO)beans.getBean("hibernatedao"); 這里并沒有直接用UserDAO對(duì)獲得的Bean實(shí)例進(jìn)行強(qiáng)制轉(zhuǎn)型。這與上面 JdbcTemplate的測(cè)試代碼不同,。并非完全出自設(shè)計(jì)上的考慮,,這里情況有些特殊,,我們可 以嘗試一下用TestDAO類對(duì)bean實(shí)例進(jìn)行強(qiáng)制轉(zhuǎn)型,,不過將得到一個(gè) ClassCastException,,程序異常中止,。 為什么會(huì)出現(xiàn)這樣的問題,?是不是只有在使用Hibernate才會(huì)出現(xiàn)這樣的問題?事實(shí) 并非如此,如果對(duì)上面基于JdbcTempate的TestDAO進(jìn)行改造,,使之實(shí)現(xiàn)ITestDAO接口,, 同樣的問題也將會(huì)出現(xiàn)。ITestDAO接口本身非常簡(jiǎn)單,,顯然也不是導(dǎo)致異常的原因所在,。 原因在于Spring的AOP實(shí)現(xiàn)機(jī)制,前面曾經(jīng)提及,,Spring中的事務(wù)管理實(shí)際上是基于 動(dòng)態(tài)AOP機(jī)制實(shí)現(xiàn),,為了實(shí)現(xiàn)動(dòng)態(tài)AOP,Spring在默認(rèn)情況下會(huì)使用Java Dynamic Proxy,,但是,,Dynamic Proxy要求其代理的對(duì)象必須實(shí)現(xiàn)一個(gè)接口,該接口定義了準(zhǔn)備 進(jìn)行代理的方法,。而對(duì)于沒有實(shí)現(xiàn)任何接口的Java Class,,需要采用其他方式,Spring通 過CGLib10實(shí)現(xiàn)這一功能,。 當(dāng)TestDAO沒有實(shí)現(xiàn)任何接口時(shí)(如JdbcTemplate示例中),。Spring通過CGLib對(duì) TestDAO進(jìn)行代理,此時(shí)getBean返回的是一個(gè)繼承自TestDAO類的子類實(shí)例,,可以通 過TestDAO對(duì)其強(qiáng)制轉(zhuǎn)型,。而當(dāng)TestDAO實(shí)現(xiàn)了ITestDAO接口之后,Spring將通過Java Dynamic Proxy機(jī)制實(shí)現(xiàn)代理功能,,此時(shí)返回的Bean,,是通過 java.lang.reflect.Proxy.newProxyInstance方法創(chuàng)建的ITestDAO接口的一個(gè)代理實(shí) 現(xiàn),這個(gè)實(shí)例實(shí)現(xiàn)了ITestDAO接口,,但與TestDAO類已經(jīng)沒有繼承關(guān)系,,因此無法通過 TestDAO強(qiáng)制轉(zhuǎn)型,。 參考: 夏昕--<Spring開發(fā)指南> 有一個(gè)問題,就是什么時(shí)間,在什么地點(diǎn),如何生成可取得Bean對(duì)象的實(shí)例? 下面有一個(gè)例子: /* * Created on Mar 12, 2004 * * (c) 2004, Mark Eagle, [email protected] * relased under terms of the GNU public license * http://www./licenses/licenses.html#TOCGPL */ package com.meagle.action; import javax.servlet.ServletContext; import org.apache.struts.action.Action; import org.apache.struts.action.ActionServlet; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.meagle.service.IOrderService; /** * @author meagle * * */ public abstract class BaseAction extends Action { private IOrderService orderService; public void setServlet(ActionServlet actionServlet) { super.setServlet(actionServlet); ServletContext servletContext = actionServlet.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); this.orderService = (IOrderService) wac.getBean("orderService"); } protected IOrderService getOrderService() { return orderService; } } 請(qǐng)大家提示一下,還有什么方式呢? |
|