2012-04-30 42 views
2

我試圖僅使用XML語法來配置spring,並將原生資源異常轉換爲DataAccessException。根據文檔,我總是需要將@Repository放在存儲庫bean上,並聲明PersistenceExceptionTranslationPostProcessor bean。我認爲這裏應該是定義某種使用AOP的過濾器的方式,它將執行異常轉換,但在文檔中找不到類似的內容。這個想法是引入約定,例如「對於所有以Dao結尾的應用來說,應用本地例外翻譯」。有任何想法嗎?@Repository的XML替代方案,用於將DataAccessException的本機異常轉換爲

+0

將'@Repository'註釋應用到dao類而不是服務層bean。對服務層bean使用'@Service' –

+0

我希望使用任何註釋來實現相同的WITHOUT,只能使用XML配置。註釋工作正常,我只想了解另一種方法,或者確保沒有這種方法。在這種情況下,Springs團隊聲稱XML支持的方式與anntations不正確相同。 – alebu

回答

0

到@Repository annotaion的Spring 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" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 
... 
     <bean id="translationInterceptor" class="org.springframework.dao.support.PersistenceExceptionTranslationInterceptor" /> 

     <aop:config> 
      <aop:advisor advice-ref="translationInterceptor" pointcut="within(org.example.spring.dao..*Dao)" /> 
     </aop:config> 
... 
    </beans> 

切入點 「內(org.example.spring.dao .. *道)」 適用攔截器在位於包有機類的方法。 example.spring.dao及其子包以「Dao」後綴結尾