gpt4 book ai didi

java - Spring/JPA/JSF 的异常处理策略

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:34:37 24 4
gpt4 key购买 nike

我们在我们的应用程序中使用了 JSF、Spring 和 JPA。我们正努力简化我们项目的异常处理策略。

我们的应用架构如下:

UI(JSF) --> 托管 Bean --> 服务 --> DAO

我们正在为 DAO 层使用 Exception Translation bean 后处理器。这是在 Spring Application Context 文件中配置的。

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

其中 Spring 将所有数据库异常包装到 'org.springframework.dao.DataAccessException'。我们没有在 DAO 层中进行任何其他异常处理。

我们处理异常的策略如下:

表示层:

Class PresentationManangedBean{

try{
serviceMethod();
}catch(BusinessException be){
// Mapping exception messages to show on UI
}
catch(Exception e){
// Mapping exception messages to show on UI
}

}

服务层

@Component("service")
Class Service{

@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = BusinessException.class)
public serviceMethod(){

try{

daoMethod();

}catch(DataAccessException cdae){
throws new BusinessException(); // Our Business/Custom exception
}
catch(Exception e){
throws new BusinessException(); // Our Business/Custom exception
}
}

}

DAO 层

@Repository("dao")
Class DAO{

public daoMethod(){
// No exception is handled
// If any DataAccessException or RuntimeException is occurred this
// is thrown to ServiceLayer
}

}

问题:我们只想确认上述方法是否符合最佳实践。如果没有,请向我们建议处理异常的最佳方法(使用事务管理)?

最佳答案

我觉得这种方法不错。我们在项目中使用了相同类型的方法。

维奈

关于java - Spring/JPA/JSF 的异常处理策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14729227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com