我正在为我的应用程序使用Spring + Hibernate + GWT,它运行正常。我现在想用AOP捕获异常,因为我使用事务并且一个简单的try / catch块无法完成这项工作。这是我在x.y.z.server.dao包中的代码:
public abstract class AbstractHibernateJpaDAO<K, E> {
protected Class<E> entityClass;
@SuppressWarnings("unchecked")
public AbstractHibernateJpaDAO() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass()
.getGenericSuperclass();
entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[1];
}
public void persist(E entity) {
getEntityManager().persist(entity);
}
...
private abstract EntityManager getEntityManager();
}
我的DAO继承了这个类:
@Repository("billOfLadingDAO")
public class BillOfLadingDAO extends
AbstractHibernateJpaDAO<String, BillOfLading> {
@PersistenceContext(unitName = "MyPUnit")
EntityManager entityManager;
@Override
public EntityManager getEntityManager() {
return entityManager;
}
我的服务在包x.y.z.server.dao.daoserviceimpl中实现如下
@Service("billOfLadingService")
public class BillOfLadingServiceImpl extends RemoteServiceServlet implements
BillOfLadingService {
private static final long serialVersionUID = -1282872068303829279L;
private static final Log LOG = LogFactory
.getLog(BillOfLadingServiceImpl.class);
@Autowired
private BillOfLadingDAO billOfLadingDAO;
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = MesExceptions.class)
public void save(BillOfLadingDTO billDTO) throws MesExceptions,Exception {
try {
if(billDTO.getUpdate()){
billOfLadingDAO.merge(bill);
}else {
billOfLadingDAO.persist(bill);
}
} catch (DataAccessException e) {
LOG.error(e);
throw new MesExceptions();
}catch(Exception eb){
throw new MesExceptions();
}
的applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<tx:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="x.y.z" />
<aop:aspectj-autoproxy />
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="persistenceUnitName" value="MyPUnit" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostPro cessor" />
</beans>
我写了这样的建议,以便捕获dataaccessexception
@Component
@Aspect
public class AfterThrowingIntercept {
@AfterThrowing(pointcut = "within(x.y.z.server.dao..*)", throwing = "ex")
public void errorInterceptor(DataAccessException ex) throws Throwable {
throw new MesExceptions();
}
}
我像这样调用save methode billOfLadingService
billOfLadingService.save(bill, new AsyncCallback() {
...
});
以下是完整的堆栈跟踪:
2015-09-09 14:41:00,433 [qtp157780255-45] DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Duplicate entry 'KJIOJIOOJ' for key 'PRIMARY' [n/a]
[INFO]com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'KJIOJIOOJ' for key 'PRIMARY'
[INFO] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[INFO] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[INFO] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[INFO] at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[INFO] at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
[INFO] at com.mysql.jdbc.Util.getInstance(Util.java:386)
[INFO] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
[INFO] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
[INFO] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
[INFO] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
[INFO] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
[INFO] at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
[INFO] at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
[INFO] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
[INFO] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
[INFO] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
[INFO] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[INFO] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[INFO] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO] at java.lang.reflect.Method.invoke(Method.java:606)
[INFO] at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
[INFO] at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
[INFO] at com.sun.proxy.$Proxy121.executeUpdate(Unknown Source)
[INFO] at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
[INFO] at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2859)
[INFO] at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPers ister.java:3300)
[INFO] at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
[INFO] at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
[INFO] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
[INFO] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
[INFO] at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
[INFO] at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
[INFO] at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
[INFO] at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
[INFO] at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
[INFO] at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
[INFO] at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)
[INFO] at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
[INFO] at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
[INFO] at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
[INFO] at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:521)
[INFO] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
[INFO] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
[INFO] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
[INFO] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
[INFO] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
[INFO] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
[INFO] at com.sun.proxy.$Proxy91.save(Unknown Source)
[INFO] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[INFO] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[INFO] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO] at java.lang.reflect.Method.invoke(Method.java:606)
[INFO] at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:587)
[INFO] at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:571)
[INFO] at org.spring4gwt.server.SpringGwtRemoteServiceServlet.processCall(SpringGwtRemoteServiceServlet.java:37)
[INFO] at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:373)
[INFO] at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
[INFO] at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
[INFO] at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
[INFO] at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:686)
[INFO] at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
[INFO] at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
[INFO] at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
[INFO] at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
[INFO] at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
[INFO] at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)
[INFO] at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
[INFO] at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
[INFO] at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
[INFO] at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
[INFO] at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:68)
[INFO] at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
[INFO] at org.eclipse.jetty.server.Server.handle(Server.java:370)
[INFO] at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
[INFO] at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:960)
[INFO] at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1021)
[INFO] at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
[INFO] at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
[INFO] at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
[INFO] at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:668)
[INFO] at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
[INFO] at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
[INFO] at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
[INFO] at java.lang.Thread.run(Thread.java:745)
但是当我像重复项目一样抛出异常时,我的建议不起作用。谁能帮我?
注意:我想抛出自己的异常,因为spring异常不可序列化并且无法通过gwt rpc调用传输。