12月1日struts、spring、hibernate等框架的整合培訓日記 (轉)
http://blog.csdn.net/zhangxiaoxiang/archive/2006/12/12/1439362.aspx
facade模式:就是中關村攢機者的角色,他負責與多個零件供銷商交互并將這些零件組裝起來交給最終客戶。
struts與spring集成的兩種方式:
第一種是在struts的Action的execute方法中調用spring的applicationContext去獲得Manager對象,這里的Action自身沒有用到spring的思想,Action無法作為Spring中的JavaBean進行配置,相應代碼如下:
????? ServletContext application = this.getServlet().getServletContext();
????? WebApplicationContext wapp = (WebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(application);
???? //WebApplicationContext wapp = (WebApplicationContext)application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
???? StudentManager studentManager = (StudentManager)wapp.getBean("studentManager");
? 另外,spring為集成struts而提供了一個ContextLoaderPlugIn插件,其作用在于:創建一個ApplicationContext對象,并將這個對象存儲在ServletContext中,以后在ActionSupport中就可以調用getWebApplicationContext來獲得這個ApplicationContext對象了,這并沒有讓我感覺有多大的好處,示例代碼如下:
??? Enumeration e = application.getAttributeNames();
??? while(e.hasMoreElements())
??? {
??????? String attributeName = (String)e.nextElement();
??????? String objClassName = application.getAttribute(attributeName).getClass().getName();
??????? try
??????? {
?????????? response.getWriter().println(attributeName + ":" + objClassName + "<br>");
??????? }catch(Exception ex){}
??? }
??? StudentManager studentManager = this.getWebApplicationContext().getBean("studentManager"));
第二種是將struts的Action作為spring的一個JavaBean進行配置,在Action里面只需要定義一個Manager變量和相應的setter/getter方法,就可以通過spring為其注入一個Manager對象。這種方式太復雜,很牽強,個人覺得沒有什么實際意義,建議大家不必花費時間去學習。
spring與hibernate整合的原理:
class MyController extend simpleformcontroller
{
?public MyController()
?{
??setCommandClass(Studnet.class);
?}
}
首先回憶使用hibernate的步驟:
1.創建和配置Configuration對象,配置Congiuration對象可以通過hibernate.properties或hiberate.cfg.xml文件,也可以完全采用如下的編程方式:
????? configuration = new Configuration()
????? configuration.addClass(Student.class)
???????????? //.addFile("Student.hbm.xml")
???????????? //.addResource("/Student.hbm.xml")
???????????? .setProperty("show_sql","true");
2.由configuration創建出SessionFactory對象:????????????
??? SessionFactory sf = configuration.buildSessionFactory();
3.通過SessionFactory獲得session對象,然后進行CRUD操作:
??? Session session = sf.openSession();
??? session.save()\delete\update\load\
??? session.close();
使用spring集成hibernate的第一步就是借助spring的配置創建出SessionFactory對象
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
?<property name="mappingResource?>
??<list>
???<value>xx.hbm.xml</value>
???<value>yyy.hbm.xml</value>
??<list>
?</Property>
?<property name="hibernateProperties">
??<props>
???<prop key="show_sql">true</prop>
??<props>
?</property>
?<property name="datasource"><ref locale=""/></property>
</bean>
在LocalSessionFactoryBean內部完全采用編程方式來配置configuration對象,而不再通過hibernate.properties或hiberate.cfg.xml配置文件,帶著大家查看了LocalSessionFactoryBean的源代碼。
有了SessionFactory,我們接著就可以通過spring將這個SessionFactory注入到DAO類中,配置如下:
<bean id="studentDAO" class="cn.itcast.StudentDAO">
?<property name="sessionFactory">
??<ref bean="sessionFactory" />
?</property>
</bean>
相應的調用程序代碼示意如下:
class StudentDAO
{
?sessionFactroy;
?void setSessionFactory(sessionFactory)
?{
??this.sessionFactory = sessionFactory;
?}
?insert(User user)
?{
??sessionFactory.openSession();//累
??session.save(user);
??session.close();//累
?}
}
我們還有自己openSession和getSession,這是不是很累呢?為此,spring又提供了一個配置類hibernateTemplate,它可以幫助我們去openSession和closeSession,這個配置類能夠openSession,顯然它一定要有SessionFactory的引用。
<bean id="hibernateTemplate" class="HibernateTemplate">
?<property name="sessionFactory">
??<ref bean="sessionFactory" />
?</property>
</bean>
遵循spring的IOC思想,我們接著還要將HibernateTemplate注入到DAO類中,配置如下:
<bean id="studentDAO" class="cn.itcast.StudentDAO">
?<property name="hibernateTemplate">
??<ref bean="hibernateTemplate" />
?</property>
</bean>
class StudentDAO
{
?hibernateTemplate;
?void sethibernateTemplate(hibernateTemplate)
?{
??this.hibernateTemplate = hibernateTemplate;
?}
?insert(User user)
?{
??hibernateTemplate.save(user);//我們不再openSession和closeSession,不是很爽嗎?
?}
}
另外,spring還提供了一個HibernateSuport類,它可以通過注入的SessionFactory返回一個HibernateTemplate,我們的DAO類繼承這個HibernateSupport,也可以獲得HibernateTemplate對象進行CRUD操作,配置及示意代碼如下:
<bean id="studentDAO" class="cn.itcast.StudentDAO">
?<property name="sessionFactory">
??<ref bean="sessionFactory" />
?</property>
</bean>
class StudentDAO extends HibernateSuport
{
?/*hibernateTemplate;
?void gethibernateTemplate()
?{
??return hibernateTemplate;
?}
?sessionFactroy;
?void setSessionFactory(sessionFactory)
?{
??this.sessionFactory = sessionFactory;
??hibernateTemplate = new HibernateTemplate(sessionFactory);
?}*/
?
?
?insert(User user)
?{
??getHibernateTemplate().save(user);
?}
}
?
最后由王澤佑提問引出spring mvc的注冊綁定的詳細講解:
在spring幫助文檔中搜索editor,可以看到有關注冊綁定的信息,CustomDateEditor是沒有被自動注冊的,需要用戶在initBinder方法內部調用,示例代碼如下:
protected void initBinder(
??????? HttpServletRequest request,
??????? ServletRequestDataBinder binder)
??????? throws ServletException {
??????? binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"),true));
??? }
一種替換的方式及registerCustomEditor底層的實現內幕:
調用BaseCommandController.setPropertyEditorRegistrars()方法,顯然這可以作為controller的屬性進行配置。
class MyPropertyEditorRegistrar
{
?void registerCustomEditors(PropertyEditorRegistry registry)
?{
??reguistry.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
?}
}
關于Date數據校驗的問題:通過在struts幫助文擋的Validator幫助頁面中搜索validate,是可以搜索到DateValidator的配置幫助的。
如果注冊了DateEditor,數據成功轉換成Date類型的字段,那么隨后的的Validator框架就沒必要對這個Date字段進行校驗了,因為Validator是對已裝配到Bean中的字符串類型的字段進行校驗,而數據都已經成功裝配進了Date字段中,哪還有校驗的必要。框架把數據裝配到formbean里面,Validator從Formbean里面拿數據進行校驗。
<bean id="myController class="cn.itcast.MyController">
?<property key="commandClass">
??<value>cn.itcast.Student</value>
?</prperty>
</bean>
上面的配置信息相當于下面的一段代碼,這其中就涉及到了屬性綁定的問題,要把字符串"cn.itcast.Student"轉換成Class對象。
Class clazz = Class.forName("cn.itcast.Student");
setCommandClass(clazz);
小記:通過spring配置文件中的<import>元素的resource屬性可以導入更多的配置文件,實現信息分散配置的目的。?