弹簧。异常翻译如何运作? [英] Spring. How exception translation works?

查看:124
本文介绍了弹簧。异常翻译如何运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

@Repository
public class UserDAOImpl implements UserDAO {
    public void addUser(User user) {
        throw new HibernateException("unchecked exception");
    }
}

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Override
    public void addUser(User user) {
        try {
            userDAO.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

web.xml :

<context:component-scan base-package="org.example.dao,
                                      org.example.services"/>
<mvc:annotation-driven />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

是否足够翻译异常?我应该实现自定义翻译吗?

Is it enough to translate exception? Should I to implement custom translator?

获取示例代码将是很好的。

It would be nice to get example code.

推荐答案

,Spring框架和其他与Spring相关的项目(如Spring Data NoSQL),定义了从 org.springframework
.dao.DataAccessException
。此层次结构处理由JDBC,Hibernate,其他ORM实现和JPA等引发的所有异常。

Basically, Spring Framework and other Spring related Projects (such as Spring Data NoSQL), define a thorough hierarchy of persistence exception inheriting from org.springframework .dao.DataAccessException. This hierarchy takes care of all the exception that are thrown by JDBC, Hibernate, other ORM implemntations, and JPA, etc.

在您的Web应用程序中,您需要做两件事实现异常翻译。

In your webapplication, You need to do two things to achieve exception translation.

首先,配置 org.springframework.dao.support
.PersistenceExceptionTranslator
您的配置文件中的实现。您可以通过在您的根配置类中定义一个LocalContainerEntityManagerFactoryBean bean,如下所示:

First, configure org.springframework.dao.support .PersistenceExceptionTranslator implementation(s) in your configuration files. You can do this by defining a bean of LocalContainerEntityManagerFactoryBean in your root config class like this:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
Map<String, Object> properties = new Hashtable<>();
properties.put("javax.persistence.schema-generation.database.action",
"none");
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect"); //you will chose a dialect that you are using for your project. 
LocalContainerEntityManagerFactoryBean factory =
new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(adapter);
factory.setDataSource(this.springJpaDataSource());
factory.setPackagesToScan("packagenames"); //the packages that contains you @Repositories annotations
factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
factory.setValidationMode(ValidationMode.NONE);
factory.setJpaPropertyMap(properties);
return factory;
}

此类 LocalContainerEntityManagerFactoryBean 实现 PersistenceExceptionTranslator 。所以你已经照顾了第一步。

This class LocalContainerEntityManagerFactoryBean implements PersistenceExceptionTranslator. So you have taken care of first step.

第二步是用 @Repository 标记你的存储库/实体注解。这告诉Spring,已注释的bean有资格使用
配置的$ code> PersistenceExceptionTranslators 进行异常翻译。如果存储库方法抛出任何持久性
异常,则 PersistenceExceptionTranslators 酌情翻译这些异常。

The second step is to mark your repositories/entities with @Repository annotation. This tells Spring that the annotated bean is eligible for exception translation using the configured PersistenceExceptionTranslators. If the repository methods throw any persistence exceptions, the PersistenceExceptionTranslators translate those exceptions as appropriate.

这篇关于弹簧。异常翻译如何运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆