<------原來做法,當tomcat5啓動時會發生監聽器錯誤的嚴重提示------(沒解決歡迎指導)>
---------------------------------------------------------------------------------------------------------------------
原來使用ssh集成,方法是struts->hibernate->spring.
把hibernate-cfg.xml和applicationContext.xml放在WEB-INF下面.
導入struts-config.xml後web.xml裏面就會把struts-config.xml作爲初始化參數。
當web容器啓動就會初始化struts-config.xml
在web.xml裏面配置spring的監聽器org.springframework.web.context.ContextLoaderListener。
把web上下文讀入到spring的applicationContext.xml裏進行管理.在applicationContext.xml裏面配置:
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value><!--file:WebRoot-->/WEB-INF/hibernate.cfg.xml</value>
</property>
</bean>
把hibernate納入到spring中進行管理。
這樣當web容器啓動時間,先啓動spring的監聽類就可以掃描到aplicationContext.xml並把spring放置到application範圍中,然後讀取struts-config.xml進行初始化。到需要獲取業務bean或者dao實例時(一般在struts的action中)就在struts中:
private AuthoeService as;
public AuthorService getAuthorService(){....}
ServletContext application=arg0.getServletContext();
WebApplicationContext wac= WebApplicationContextUtils.
getRequiredWebApplicationContext(application);
as(業務對象)=(AuthorService)wac.getBean("as");
通過web上下文獲取spring的上下文,並根據IOC容器的配置獲得對應的實體的實例.
(簡略介紹---水平有限僅供參考)
<---------------------------正確集成1---------------------------->
(近期又學到一種方法:)
spring-->hibernate-->struts,
1添加spring,將applicationContext.xml放在src下;
2添加hibernate,這時hibernate會放在applicationContext.xml並詢問:
A---create a new hibernate config file or specify an existing config file
B---create a new spring config file or specify an existing config file
選B 然後next
又詢問spring configuration file to be used by MyEclipse Hibernate tools:
A New spring configuration file
B Existing Spring configuration file
意思是spring的配置文件已存在是在新的applicationContext.xml配置數據源還是用原來的配置文件。選B
然後爲在sessionFactory爲hibernate的會話工廠命名一個標識,next。
然後選擇配置的數據庫併爲其bean命名(datasourse)
3在web.xml中集成spring:
------用初始化spring配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
-------自啓動的servelt啓動spring的監聽器。管理web上下文
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
4添加struts到項目中,在action中覆蓋setServlet(ActionSetvelt arg0)方法。方法內:
if(arg0!=null){
ServeltContext sc=arg0.getServletContext();
WebApplicationContexwac=WebApplicationContextUtils.getWebApplicationContext(sc);
(業務對象)=(業務類)wac.getBean("IOC注入id標識");
}
--------------------------------------------
該方法可以解決第一種方法集成的嚴重錯誤,
-------------------------------------------------------------------------------------------------------------------
總結:第一種方法雖然沒成功,但其低耦合性值得重視。
第二種方法是spring管理其他的,但是如果拿掉spring那麼其他兩個就不能很好運行,相比耦合性較大
------------------剛學習,有不足之處熱情希望有人幫忙指出,,希望對初學者有幫助