You are on page 1of 15

SPRING TUTORIAL

I. Gii thiu v Spring: Spring l mt application framework m ngun m, c gii thiu vo nm 2002. Rod Johnson a ra tng ny t kinh nghim lm vic vi kin trc J2EE. ng ta vit cun sch vi tiu : J2EE Develoment without using EJB gii thiu khi nim trnh cha hng nh (lightweight container). Vi l lun: EJB th c gi tr ca n, nhng khng phi lc no cng cn thit v ph hp cho tt c cc ng dng. Nh Hibernate chng li CMP v nguyn l c bn ca k thut ORM, Spring chng li EJB v s phc tp v khng ph hp cho cc unit test. Thay v EJB, Spring s dng Java bean, vi mt vi s thay i thu c tt c cc thun li m mi trng EJB a ra. Do Spring l mt s la chn khc so vi EJB. Spring khng phi l mt k thut persistence nhng n cho php tch hp vi cc k thut khc. EJB th tp trung vo k thut persistence v by gi n cng tc vi Hibernate, mt ORM tt nht ngy nay. N ang d nh cng tc vi mt k thut ORM tt khc l JDO (cung cp cho Object Database). Trong Spring, chng ta c th s dng Java Bean rt trch cc th m lc trc ch c th vi EJB. Mc tiu chnh ca Spring l n gin vic pht trin J2EE v testing. EJB c xut hin vo 1988 v l mt chun, c t trong th gii Enterprise. Mc d c mt s k thut Java gn y, nhng khng c k thut no ng k so vi EJB v mc tng cc c tnh ca n. Theo Rod Johnson th EJB khng phc tp nhng n c gng cung cp cch gii quyt cho cc vn phc tp. EJB c pht trin chnh cho cc giao dch, i tng c phn pht trn my khc. Nhng mt s enterprise project khng c mc phc tp v vn s dng EJB v thm ch cc ng dng n gin tr nn phc tp. Trong trng hp ny th Spring l mt s chn la. T lc Spring h tr cc dch v mc enterprise, th n tuyn b l mt s la chn khc i vi EJB. Thun li ca EJB: a) Transaction Management b) Declarative Transaction support c) Persistence ( CMP & BMP) d) Declarative Security e) Distributed Computing (Container managed RPC) Spring khng c gng lm mi th nhng n h tr nhng k thut tt nht cho mi yu cu.Thay v CMP & BMP, n h tr mt vi k thut persistence khc nh JDO, Hiberbate v

OJB. Cc ORM tool ny th c nhiu kh nng ci t hn CMP. n gin coding JDBC, c mt tool l iBatis v Spring cng h tr n. Spring s dng Acegi, mt security framework m ngun m v cung cp khai bo security thng qua cu hnh Spring hoc class metadata, trong khi EJB khai bo security c cu hnh thng qua m t demployment. Spring cung cp proxying cho RMI (k thut remoting c bit nh Burlap) JAX-RPC & web-service, trong khi EJB cung cp li gi phng thc xa c qun l bi container. Spring c th cung cp khai bo transaction nh EJB. Hn na, Spring cn cung cp khai bo th tc rollback cho cc phng thc v exception. Do , trong khi EJB th cng v c gng lm mi th, mt vi cng vic tt nhng mt s th khng. Spring ch s dng Java Bean v thng qua mt s k thut c bit cung cp nhiu chc nng nh EJB, bng cch tch hp vi mt s k thut open source khc. Do , n cung cp mt vi thun li hn EJB nh: a) Testing d dng hn - khng cn khi ng EJB container test. b) Spring da vo quy c ca chun Java Bean, nn programmer d dng lm vic vi n. c) N s dng AOP (Aspect-Oriented Programming), m hnh hu ch b sung vo OOP truyn thng v bo ton tnh nguyn vn ca OOP. d) N th uyn chuyn. Mc ch ca Spring l tr thnh mt application framework. Cc framework ph bin khc nh Struts, Tapestry, JSF,... l cc framework tt cho tng web nhng khi chng ta s dng cc framework ny, chng ta phi cung cp thm framework khc gii quyt tng enterprise m tch hp tt vi cc framework ny. Spring lm gim bt vn ny bng cch cung cp mt framework ton din bao gm: Core bean container, MVC framework, AOP integration framework, JDBC integration framework, EJB integration framework. N cng cung cp module tch hp vi O/R tool nh Hibernate v JDO. Do Spring framework c th c xem nh mt kin trc cha 7 module. Chc nng ca mi thnh phn nh sau: 1. Core Container: Core container cung cp chc nng c bn ca Spring. Thnh phn chnh ca n l Bean Factory, mt ci t ca Factory pattern. BeanFactory p dng IoC pattern c t s ph thuc t code ca ng dng. 2. Spring Context/Application Context:

Spring context l mt file cu hnh cung cp thng tin ng cnh ca Spring. Spring context cung cp cc service nh JNDI access, EJB integration, e-mail, internalization, validation, v scheduling functionality. 3. Spring AOP (Aspect-Oriented): Spring AOP module tch hp chc nng lp trnh hng kha cnh vo Spring framework thng qua cu hnh ca n. Spring AOP module cung cp cc dch v qun l giao dch cho cc i tng trong bt k ng dng no s dng Spring. Vi Spring AOP chng ta c th tch hp declarative transaction management vo trong ng dng m khng cn da vo EJB component. Spring AOP module cng a lp trnh metadata vo trong Spring. S dng ci ny chng ta c th thm annotation vo source code hng dn Spring ni v lm th no lin h vi aspect. 4. Spring DAO: Tng JDBC v DAO a ra mt cy phn cp exception qun l kt ni n database, iu khin exception v thng bo li c nm bi vendor ca database. Tng exception n gin iu khin li v gim khi lng code m chng ta cn vit nh m v ng kt ni. Module ny cng cung cp cc dch v qun l giao dch cho cc i tng trong ng dng Spring. 5. Spring ORM: Spring c th tch hp vi mt vi ORM framework cung cp Object Relation tool bao gm: JDO, Hibernate, OJB v iBatis SQL Maps. 6. Spring Web module: Nm trn application context module, cung cp context cho cc ng dng web. Spring cng h tr tch hp vi Struts, JSF v Webwork. Web module cng lm gim bt cc cng vic iu khin nhiu request v gn cc tham s ca request vo cc i tng domain. 7. Spring MVC Framework: MVC Framework th ci t y c tnh ca MVC pattern xy dng cc ng dng Web. MVC framework th cu hnh thng qua giao din v cha c mt s k thut view bao gm: JSP, Velocity, Tiles v generation of PDF v Excel file. V d: C mt s k thut tuyt vi cho tng web nh: Spring MVC framework, Struts, JSF, WebWork, JSP, Tapestry, FreeMarker,...Developer s b lng tng i chiu nhng im mnh v xu ca tt c chng. Mi khi h chn mt k thut v bt u ci t, th sau nu h

mun thay i mt k thut khc th rt kh. Nhng Spring a ra cc module cho tt c cc k thut trn, v rt n gin thay i file cu hnh. Vi phng php ny, n c kh nng cho c team th v test cc tt c cc hnh thc trn v xem nh hng cng tc trc khi quyt nh chn la. JSP l mt view template mc nh. "InternalResouceViewResolver" c th c s dng cho mc ch ny. II. Cc khi nim chnh ca Spring: Cc khi nim chnh ca Spring l: IoC v Aspect Orient Programming. Spring da vo Depenecy injection ca IoC. Chng ta khng trc tip kt ni cc component v service li vi nhau trong code m thng qua file cu hnh. Mt container th chu trch nhim mc ni n. Khi nim ny th tng t vi Declarative Management. 1. Dependency Injection: Depedency injection l mt pattern chu trch nhim to i tng v cc lin kt gia cc i tng b loi b ra khi cc i tng v chuyn n cho factory qun l. Cc i tng ca ng dng khng chu trch nhim tm kim cc resource m chng ph thuc. V v th nn s ph thuc c o ngc vo trong cc i tng. Theo phng php ny, cc i tng c th c rt trch thng qua Setter Injection (cc thuc tnh ca Java-Beans) hoc Constructor Injection (tham s ca constructor). Mi phng thc c thun li v bt li ring. Bnh thng trong Java bean, chng ta s dng phng thc setter v getter thit lp v ly gi tr ca thuc tnh nh sau: public class namebean { String name; public void setName(String a) name = a; } public String getName() { return name; } } Chng ta s to mt th hin ca bean "namebean" (gi l bean1) v thit lp thuc tnh: bean1.setName("Tom"). Cn trong setter injection, chng ta thit lp thuc tnh "name" trong subelement <property> ca th <bean> trong file cu hnh Spring nh sau: <bean id="bean1" class="namebean">

<property name="name" > <value>Tom</value> </property> </bean> Subelement <value> thit lp thuc tnh "name" bng cch gi phng thc set nh sau: setName("Tom"); => tin trnh ny c gi l setter injection. V d v constructor injection, chng ta s dng constructor vi mt tham s: public class namebean { String name; public namebean(String a) { name = a; } } Chng ta s thit lp thuc tnh "name" trong khi to th hin ca bean "namebean" nh sau: namebean bean1 = new namebean("Tom"); y chng ta s dng element <constructor-arg> thit lp thuc tnh s dng constructor injection nh sau: <bean id="bean1" class="namebean"> <constructor-arg> <value>My Bean Value</value> </constructor-arg> </bean> thit lp cc thuc tnh tham chiu n bean khc - <ref>, subelement <property> c s dng nh sau: <bean id="bean1" class="bean1impl"> <property name="game"> <ref bean="bean2"/> </property> </bean> <bean id="bean2" class="bean2impl" /> 2. Aspect-oriented Programming: K thut lp trnh a ra cch phn chia cc yu cu ca h thng. H thng c phn ra thnh mt vi component m trch mt chc nng xc nh. Bn cnh cc chc nng c bn th cc dch v (yu cu h thng) nh logging, transaction management, security...phi c trong chng trnh. Cc dch v h thng ny c gi l 'cross-cutting concern' - ngha l chng ct cho qua nhiu component trong h thng. AOP c kh nng module ha cc service

ny v sau cung cp n cc component v chng ta c th tp trung vo yu cu chnh (core concern). Trong Spring, lt ct (aspect) c cm vo trong cc i tng ca XML file nh cc JavaBean. Tin trnh ny gi l s an xen (weaving). Container l khi nim chnh ca Spring. qun l vng i v cu hnh cc i tng ca ng dng. Chng ta c th cu hnh mi bean nn c to nh th no hoc lm th no to mt instance ca bean hoc lm th no kt hp chng li. Spring khng s dng cc EJB container hnh nng. Spring c 2 container phn bit: Bean Factory c nh ngha bi org.springframework. beans.factory.BeanFactory l mt container n gin nht, cung cp h tr dependency injection. Application context c nh ngha bi org.springframework.context.ApplicationContext, cung cp cc service ca application framework. 2.1. Bean Factory: Bean factory th ci t factory design pattern v chc nng ca n l to v phn pht cc bean. Bean factory bit v nhiu i tng trong ng dng, n c th to mi kt hp cho cc i tng khi chng c khi to. iu ny s gim bt gnh nng cho bean v client. C mt vi ci t ca BeanFactory. Mt ci hu ch nht l org.springframework.beans.factory.xml.XmlBeanFactory. N s load cc bean da vo nh ngha trong XML file. to mt th hin ca XmlBeanFactory, hy chuyn vo constuctor ca n mt InputStream. Resource ny s cung cp XML cho factory. BeanFactory factory = new XmlBeanFactory(new FileInputStream("myBean.xml")); => dng ny bo bean factory c nh ngha bean t XML file. nh ngha bean bao gm m t bean v cc thuc tnh ca n. Nhng bean factory vn cha khi to n. rt trch mt bean t BeanFactory, hy gi phng thc getBean(). Khi phng thc getBean() c gi, factory s khi to bean v bt u thit lp cc thuc tnh ca n s dng depedency injection. myBena bean1 = (myBean)factory.getBean("myBean"); 2.2. Application Context: Trong khi Bean Factory c s dng cho cc ng dng n gin, th Application Context l mt container nng cao ca Spring. Ging nh BeanFactory, n c th c s dng load cc nh ngha bean, gn cc bean vi nhau v phn pht cc bean theo yu cu. N cng cung cp: 1) gii quyt text message, bao gm h tr internationlization. 2) cch chung load file resource. 3) cc s kin bean c ng k nh cc trnh lng nghe. C 3 cch s dng ci t Application Context: 1) ClassPathXmlApplicationContext: N load nh ngha context t XML file c t trong classpath, xem cc nh ngha context nh cc ti nguyn ca classpath. Application context th c load t classpath ca ng dng nh sau:

ApplicationContext

context = new ClassPathXmlApplicationContext("bean.xml");

2) FileSystemXmlApplicationContext: N load nh ngha context t XML file trong t h thng file. Application Context c load t h thng file nh sau: ApplicationContext context = new FileSystemXmlApplicationContext("bean.xml"); 3) XmlWebApplicationContext: N s load cc nh ngha context t XML file trong ng dng web. Spring th nh v c kch c v chi ph. Ton b framework c th c phn pht trong JAR file ch hn 1.7MB. Chi ph c yu cu bi Spring th khng ng k. V Spring khng rng buc, v d: cc i tng trong ng dng Spring khng ph thuc vo cc class ca Spring. III. Release info: Spring framework c download trang http://www.springframework.org. Trn c 2 file zip: mt file c dependencies, mt file th khng. Spring framework vi dependencies th ln hn v cha tt c th vin ph thuc. Ni dung ca phn phi "-with-dependencies" (~60 MB): * "dist" cha cc file jar ca bng phn phi v mt file nn cha tt c source code. * "docs" cha ti liu c nh dnh PDF, HTML v javadocs. * "lib" cha tt c th vin cn xy dng framework hoc chy cc v d. * "src" cha cc file Java source ca framework. * "mock" cha cc file Java source cho mock v test. * "test" cha file java cho b test ca Spring. * "tiger/src" cha cc file JDK-1.5-specific Java source cho framework. * "tiger/test" cha cc file JDK-1.5-specific Java source cho b test ca Spring. * "aspectj/src" cha tt c file AspectJ-specific source cho framework * "aspectj/test" cha tt c file AspectJ-specific source cho cho b test ca Spring. * "samples" cha cc ng dng demo v chng trnh gii thiu. IV. Spring Application: Cc bc to ng dng s dng Spring: 1. Mt interface nh ngha cc chc nng. 2. Mt implementation cha cc thuc tnh, cc phng thc settter v getter. 3. Mt file XML gi l file cu hnh Spring. 4. Chng trnh client s dng chc nng trn. 1. public interface Hello { public String sayHello(String s);

} 2. public class HelloImpl implements Hello{ private String greeting; /** * Class constructor * @param s */ public HelloImpl(){ } public HelloImpl(String s){ greeting = s; } /** * function */ public String sayHello(String s) { return greeting + s; } /** * @return Returns the greeting. */ public String getGreeting() { return greeting; } /** * @param greeting The greeting to set. */ public void setGreeting(String greeting) { this.greeting = greeting; } } 3.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="hello" class="HelloImpl"> <property name="greeting"> <value>Good Morning!</value> </property> </bean> </beans>

4.

import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class HelloClient { /** * @param args */ public static void main(String[] args) { try { ClassPathResource resource = new ClassPathResource("hello.xml"); BeanFactory factory = new XmlBeanFactory(resource); HelloImpl bean = (HelloImpl) factory.getBean("hello"); String hello = bean.sayHello("Trinh"); System.out.println(hello); } catch (Exception e) { e.printStackTrace(); } } } Chy ng dng s kt xut nh sau:
2007/01/12 13:43:16 org.springframework.core.CollectionFactory <clinit> : JDK 1.4+ collections available 2007/01/12 13:43:17 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions : Loading XML bean definitions from class path resource [hello.xml] Good Morning!Trinh

y, HelloImpl ci t giao tip Hello. Mc d khng cn thit phi du ci t pha sau mt giao tip. HelloImpl c mt thuc tnh greeting. Thuc tnh ny c th c thit lp theo 2 cch: bng phng thc setter hoc bng constructor. File hello.xml khai bo mt th hin ca HelloImpl trong container ca Spring v cu hnh gi tr ca thuc tnh greeting l "Good Morning!". Root ca file hello.xml l <beans> element - l root element ca bt k file cu hnh no ca Spring. Element <bean> c s dng bo Spring container v lp v n nn c cu hnh nh th no. y, thuc tnh id l tn ca interface v thuc tnh class xc nh tn lp y ca bean. Trong <element> bean, element <property> c s dng thit lp thuc tnh, trong trng hp ny l thuc tnh greeting. S dng <property>, chng ta ni vi Spring container gi phng thc setGreeting(). Gi tr ca greeting c nh ngha trong element <value>. Container s khi to HelloImpl da vo nh ngha XML nh sau:

HelloImpl hello = new HelloImpl (); hello.setGreeting("Good Morning!..."); Tng t, thuc tnh greeting c th c thit lp thng qua constructor c 1 tham s ca HelloImpl nh sau: <bean id="hello" class=" HelloImpl"> <constructor-arg> <value>Good Morning!...</value> </constructor-arg> </bean> By gi th container khi to HelloImpl da vo nh ngha XML nh sau: HelloImpl hello = new HelloImpl("Good Morning..."); chng trnh client, lp BeanFactory c s dng l Spring container. Sau phng thc getBean() c gi ly tham chiu n bean "hello". Vi tham chiu ny th phng thc sayHello() c gi. Bc k tip chng ta s xem lm th no chy chng trnh ny nh mt servlet. u tin copy spring.jar n th mc common\lib ca Tomcat v khi ng tomcat server. Sau khi thit lp classpath, to file HelloServlet.java nh sau: HelloServlet.java import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class HelloServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("userName"); response.setContentType("text/html"); ClassPathResource resource = new ClassPathResource("hello.xml"); BeanFactory factory = new XmlBeanFactory(resource);

Hello bean = (Hello) factory.getBean("hello"); PrintWriter out = response.getWriter(); out.write(bean.sayHello(name)); } } index.html

<form method="post" action="http://localhost:8080/springservlet/hello"> <input type="text" name="userName"/> <input type="submit" value="sayHello"/> </form> </body> </html>

<html> <body>

web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>springservlet</display-name> <servlet> <servlet-name>hello</servlet-name> <servlet-class>HelloServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>

By gi

chng ta s vo tin trnh chnh ca bt k ng dng enterprise no: data persistence. Spring c framework data access ring ca n. Spring chia phn c nh v ty bin ca tin trnh data access thnh hai lp phn bit: Template v Callback. Template qun l phn c nh ca framework nh data connection, qun l ti nguyn, iu khin giao dch...Trong khi Callback nh ngha cc th c xc nh trong ng dng nh to statement, mc ni tham s...Lp Template ca Spring l JdbcTemplate. Mt data source phi c cung cp bn trong JdbcTemplate. Mt v d v kt ni CSDL s dng JdbcTemplate nh sau. y chng ta s dng CSDL MySQL. Sau khi to bng user(name text, place text) trong CSDL test, chng ta to chng trnh nh sau: 1. To interface DataConnection cha hm ly v mt dataSource cung cp cho JdbcTemplate:

import javax.sql.DataSource; public interface DataConnection { public DataSource getDataSource(); } 2. To ci t ca interface cha thuc tnh dataSource v cc phng thc setter, getter: import javax.sql.DataSource; public class DataConnectionImpl implements DataConnection{ private DataSource dataSource; public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } } 3. To DataConServlet thc hin nghip v l insert d liu.: import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.jdbc.core.JdbcTemplate; public class DataConServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name");

String place = request.getParameter("place"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); ClassPathResource resource = new ClassPathResource("applicationContext.xml"); BeanFactory factory = new XmlBeanFactory(resource); DataConnection bean = (DataConnection) factory .getBean("dataConnection"); DataSource ds = bean.getDataSource(); try { JdbcTemplate template = new JdbcTemplate(ds); String sql = "insert into user values ('" + name + "','" + place + "')"; template.execute(sql); out.write("A record is added!"); } catch (Exception e) { e.printStackTrace(); } } } 4.
<html> <body> <form method="post" action="http://localhost:8080/springservlet/data"> <table> <tr> <th>Name:</th> <td><input type="text" name="name"/></td> </tr> <tr> <th>Place:</th> <td><input type="text" name="place"/></td> </tr> <tr> <td><input type="submit" value="Add"/></td> </tr> </table> </form> </body> </html>

To client s dng servlet trn:

5. Cu hnh bean dataSource:


<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="hello" class="HelloImpl"> <property name="greeting"> <value>Good Morning!</value>

</property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <bean id="dataConnection" class="DataConnectionImpl"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> </beans>

6. Cu hnh servlet:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name> springservlet</display-name> <servlet> <servlet-name>data</servlet-name> <servlet-class>DataConServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>data</servlet-name> <url-pattern>/data</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>

mapping cc resource ca Hibernate, cn to mt th hin ca SessionFactory, LocalSessionFactoryBean th c s dng cho mc ch ny v cc thuc tnh: hibernateProperties, mappingResources v mappingDirectoryLocation phi c thit lp. Ging nh framework DAO ca Spring, chng ta c HibernateTemplate to i tng SessionFactory. truy cp vo d liu vi HibernateTemplate, phng thc execute(HibernateCallback) phi c s dng. Tng t nh SessionFactory ca hibernate, JDO c PersistenceManagerFactory. N c cu hnh bi LocalPersistenceManagerFactoryBean. JDOTemplate cng to mt i tng ca PersistenceManagerFactory. truy cp vo d liu vi JDOTemplate, phng thc execute(JDOCallback) phi c s dng.

You might also like