2012-08-07 77 views
1

我只是不明白。使用@Serviced標記並通過@ComponentScan在應用程序上下文中註冊的bean是否通過@Transaction批註代理了事務支持?@Transaction @Service在Spring註釋配置

這工作得很好:

public class LocationManagerImpl implements LocationManager { 

     @Transactional 
     public void saveLocation(Location location) { 

     } 

    } 

//config class 

@Bean 
public LocationManager locationManager() { 
    return new LocationManagerImpl(); 
} 

,這並不:

@Service 
public class LocationManagerImpl implements LocationManager { 

    @Transactional 
    public void saveLocation(Location location) { 

    } 

} 
+2

ComponentScan註釋用於@Configuration類。你有上下文:你的應用程序上下文中的組件掃描? – jeff 2012-08-07 15:31:31

+0

我的配置是基於註釋的,我在標記爲\ @Configuration的類中應用\ @ComponentScan。服務已創建,但事務支持不起作用。 – 2012-08-07 18:07:11

+0

當您使用第二種方法並嘗試從上下文中獲取LocationManager時,是否有?它在那裏,但只是沒有代理交易?還是完全缺失? – jeff 2012-08-07 18:46:15

回答

3

的問題可能是您的@Transactional註解類是坐落在servlet上下文。如果您在servlet應用程序上下文配置中有<context:component-scan>,而Spring AOP攔截器在根應用程序上下文中配置,則可能發生這種情況。

解決方案是將@Service帶註釋的類移動到根Web應用程序上下文。

請參閱Spring @Transactional not working

Servlet和Web應用程序之間的區別根上下文: Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

相關問題