通过两天的换Jar包 删代码等多种测试方法
目前我配置使用的SSH版本为:struts2.3.4+spring3.1.2+hibernate4.1.6
一直报这个错 不知道什么原因啊 困扰了我两天了 换了好多jar包 都是这个错 不知道问题出在哪里啊 写的Junit Test代码可以执行 并且数据可以插入数据库 但是一启动Tomcat并部署他 就报下面的错误 真的是莫名其妙 今天无意中发现关了所有的项目 tomcat依然报错 接着我就换了MyEclipse自带的cat 一测试 居然神奇的好了 问题出在了Tomcat上 应该是版本兼容问题
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.convert.Property.<init>(Ljava/lang/Class;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;Ljava/lang/String;)V
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
下面记录下SSH整合的过程:struts2.3.4+spring3.1.2+hibernate4.1.6
1、引入SSH项目所需要的所有Jar包
可参考(http://txazo.iteye.com/blog/1669771)
2、引用的jar包结构如下:
3、改写Web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Web Request Listener -->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- struts -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Log4j ConfigurationFile Location -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- Spring Log4j Listener -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
</web-app>
4、编写spring与hibernate集成的beans.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.myspring.registration" />
<!-- DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://127.0.0.1:3306/spring" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.myspring.registration.model.User
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--
<aop:config>
<aop:pointcut expression="execution(public * com.myspring.service.impl.*.*(..))"
id="bService" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bService" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
-->
</beans>
5、struts整合到spring和hibernate中,主要是struts2-spring-plugin-2.3.4.1.jar文件的作用
配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="register" class="com.myspring.registration.action.UserAction">
<result name="success"> /registerSuccess.jsp </result>
<result name="fail"> /registerFail.jsp </result>
</action>
</package>
</struts>
6、在UserAction中通过注解注入即可将struts交给spring进行管理了
UserAction.java文件
package com.myspring.registration.action;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.myspring.registration.model.User;
import com.myspring.registration.service.UserManager;
import com.opensymphony.xwork2.ActionSupport;
@Scope("prototype")
@Component("register")
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String password2;
private UserManager um;
public UserManager getUm() {
return um;
}
@Resource(name="userManager")
public void setUm(UserManager um) {
this.um = um;
}
@Override
public String execute() throws Exception {
User u = new User();
u.setUsername(username);
u.setPassword(password);
if(um.exists(u)) {
return "fail";
}
um.add(u);
return "success";
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getPassword2() {
return password2;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
}
7、UserDaoImpl.java文件
package com.myspring.registration.dao.impl;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import com.myspring.registration.dao.UserDao;
import com.myspring.registration.model.User;
@Component("userDao")
public class UserDaoImpl implements UserDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(User u) {
Session s = sessionFactory.openSession();
s.beginTransaction();
s.save(u);
s.getTransaction().commit();
}
public boolean checkUserWithName(String username) {
Session s = sessionFactory.openSession();
s.beginTransaction();
long count = (Long)s.createQuery("select count(*) from User u where u.username = ?")
.setParameter(0, username)
.uniqueResult();
s.getTransaction().commit();
if(count > 0) {
return true;
}
return false;
}
}
8、UserManagerImpl.java文件
package com.myspring.registration.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.myspring.registration.dao.UserDao;
import com.myspring.registration.model.User;
import com.myspring.registration.service.UserManager;
@Component("userManager")
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
@Resource(name="userDao")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/* (non-Javadoc)
* @see com.myspring.registration.service.UserManagerImpl#exists(com.myspring.registration.model.User)
*/
public boolean exists(User u) throws Exception {
System.out.println("==="+userDao);
return userDao.checkUserWithName(u.getUsername());
}
/* (non-Javadoc)
* @see com.myspring.registration.service.UserManagerImpl#add(com.myspring.registration.model.User)
*/
public void add(User u) throws Exception {
userDao.save(u);
}
}
通过如上步骤就可以完整的整合SSH了,2天的苦苦调试终于有结果了。。。哈哈哈哈