ssh整合新解

<------原来做法,当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那么其他两个就不能很好运行,相比耦合性较大

 

------------------刚学习,有不足之处热情希望有人帮忙指出,,希望对初学者有帮助

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章