最近正在学习ssh的最新版本。由于一直没有用ssh做过东西,尤其是中间的spring,项目中用到的struts和hibernate,所以对于在中间层加上spring该如何处理,还存在一些困惑,这是我做的一个三者结合的例子,其主要目的是充分利用spring的ioc和hibernate3的annoation
1.struts2和spring的结合,以及如何把action纳入spring的管理
首先:工程中应加入struts2-spring-plugin-2.0.8.jar
其次:在web.xml中加入如下
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
第三:在struts.xml和applicationContext.xml中的配置如下
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="com" extends="struts-default">
<action name="Login" class="Login" method="getAllUserInfo"><!--此处的class属性不能写成包名.类名的格式,否则会报空指针异常-->
<result name="input">Login.jsp</result><!-- 如果输入为空的话跳转到Login.jsp -->
<result >userindex.jsp</result><!--因为此处没有声明name属性,所以默认为成功时的跳转页, 如果成功的话跳转到userindex.jsp -->
</action>
</package>
</struts>
applicationContext.xml中(配置片段)
<bean id="Login" class="com.web.action.Login" p:iuserinfo-ref="UserInfoImpl" scope ="prototype"/>
注意”红色“字体部分,其中action中的class=“Login”要和<bean>中的id对应,action的属性mehtod如果没有制定的话,将默认调用struts中action中的execute方法,在struts2官方的faq中提到要把action纳入spring的管理还得添加<beans default-autowire="autodetect">(原因还不太清除),由于strus2为每一个请求实例化一个action所以bean的scope要设成protype,默认为singleton
ok,struts2和spring已经结合到一起,剩下的就是根据项目需要"求精了"
2,spring和hibernate的结合hibernate3 aonotation
目前的例子只是演示如何集成二者,所以只是将hibernte的SessionFactory交由spring管理,其他的如(事务管理没有考虑很细致,全部在dao层控制)
如下applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-lazy-init="true" default-autowire="autodetect"><!-- 默认是byName,可以改为byType,autodetect将先按照byName处理在按照byType -->
<!--datasource config -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/paulstudy" p:username="root"
p:password="1234" />
<!-- SessionFactory annotation config, instead of "hibernate.cfg.xml"-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.model.UserInfo</value>
</list>
</property>
<property name="annotatedPackages">
<list>
<value>com.model</value>
</list>
</property>
</bean>
<!-- SessionFactory annotation config, i end -->
<bean id="usermanager" class="com.dao.hibernate.UserManagerImpl">
<property name="dataSource">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="UserInfoImpl" class="com.business.service.UserInfoImpl"
p:iusermanager-ref="usermanager" />
<bean id="Login" class="com.web.action.Login"
p:iuserinfo-ref="UserInfoImpl" scope="prototype" />
<bean id="userinfo" class="com.UserInfo" p:userName="paul"
p:age="25" />
</beans>
通过以上实现了hibernate的零配置文件。如下domainobject
package com.model;
import javax.persistence.Column;//所在jar包是 persistence-api-1.0.jar
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Entity;
import org.hibernate.annotations.AccessType;
//import org.hibernate.annotations.Entity;
import org.hibernate.annotations.GenericGenerator;
@Entity//此处声明必须用avax.persistence.Entity;而不能用 org.hibernate.annotations.Entity
@Table(name = "userinfo")
@AccessType("property")
public class UserInfo {
private String userId = "";
private String userName = "";
private String userSex = "";
private int userAge = 0;
@Id
@Column(name = "userid")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Column(name = "username")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "usersex")
public String getUserSex() {
return userSex;
}
@Column(name = "userage")
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
}
ok。spring和hibernate的结合,hibernate的零配置已经搞定,很简单吧
学好。学精还需要时间啊!