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 + "
");
}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對象
true
在LocalSessionFactoryBean內部完全採用編程方式來配置configuration對象,而不再通過hibernate.properties或hiberate.cfg.xml配置文件,帶着大家查看了LocalSessionFactoryBean的源代碼。
有了SessionFactory,我們接着就可以通過spring將這個SessionFactory注入到DAO類中,配置如下:
相應的調用程序代碼示意如下:
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的引用。
遵循spring的IOC思想,我們接着還要將HibernateTemplate注入到DAO類中,配置如下:
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操作,配置及示意代碼如下:
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裏面拿數據進行校驗。
cn.itcast.Student
上面的配置信息相當於下面的一段代碼,這其中就涉及到了屬性綁定的問題,要把字符串"cn.itcast.Student"轉換成Class對象。
Class clazz = Class.forName("cn.itcast.Student");
setCommandClass(clazz);
小記:通過spring配置文件中的元素的resource屬性可以導入更多的配置文件,實現信息分散配置的目的。