【问题标题】:Spring 4 @Transactional and @AspectSpring 4 @Transactional 和 @Aspect
【发布时间】:2015-04-23 13:36:42
【问题描述】:

我有一个拦截器类,它监视对象的创建。我希望在实际创建服务方法之前调用此拦截器以注入当前用户/日期。

/**
 * Author: plalonde
 * When: 2015-03-04
 */
@Aspect
public class IdentityInterceptor {
    /**
     * Injection de l'usagé actuellement en session dans l'entité.
     *
     * @param entity L'entité dans laquelle les informations de l'usagé doivent être injectée.
     */
    @Before("execution(* com.t3e.persistence.service.CrudService.create(..)) && args(entity)")
    public void giveOwnership(final TraceableDto entity) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        entity.setCreationUserId(authentication.getName());
        entity.setCreationDate(new Date());
        entity.setLastModificationUserId(authentication.getName());
        entity.setLastModificationDate(new Date());
    }
}


/**
 * Author: plalonde
 * When: 2015-04-22
 */
@Service
@Transactional(readOnly = true)
public class LocalWatsonLicenseService implements WatsonLicenseService {
    @Autowired
    private WatsonLicenseRepository repository;

    /**
     * Création d'une licence.
     *
     * @param watsonLicenseDetails Le détails de la licence à créer.
     * @return L'instance de la licence mise à jour.
     */
    @Transactional
    @Override
    public WatsonLicenseDetails create(final WatsonLicenseDetails watsonLicenseDetails) {
        return repository.create(watsonLicenseDetails);
    }

}

/**
 * Author: plalonde
 * When: 2015-04-22
 */
public interface WatsonLicenseService extends CrudService<WatsonLicenseDetails> {
}

我的拦截器永远不会被调用,我想知道是不是因为服务的方法是@Transctional?

看起来 Aspect 配置良好,因为我有其他服务方法,一切都按预期工作。

我尝试查找文档,但我只收到有关使用 Aspect 处理交易的帖子,但这不是我的情况。

我的包装方法应该返回更新的值还是在切入点中传递并处理它?

[编辑]

这就是拦截器的声明方式

/**
 * Author: plalonde
 * When: 2015-04-22
 */
@Configuration
@EnableTransactionManagement
class DatabaseConfig {
    @Value("${dataSource.driverClassName}")
    private String driver;
    @Value("${dataSource.url}")
    private String url;
    @Value("${dataSource.username}")
    private String username;
    @Value("${dataSource.password}")
    private String password;

    @Bean
    public DataSource configureDataSource() {
        DriverManagerDataSource dmds = new DriverManagerDataSource(url, username, password);
        dmds.setDriverClassName(driver);

        return dmds;
    }

    @Bean
    public NamedParameterJdbcTemplate configureTemplate(final DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Bean
    public PlatformTransactionManager transactionManager(final DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public IdentityInterceptor createInterceptor() {
        return new IdentityInterceptor();
    }
}

【问题讨论】:

  • WatsonLicenseDetails 是如何定义的?
  • 而你对方面没有做任何事情。您需要告诉 Spring 您要为此启用 aspectj,将 @EnableAspectJAutoProxy 添加到您的配置中。
  • 就是这样,忘记注释了

标签: spring transactions aspectj


【解决方案1】:

正如 M.Deinum 评论中提到的,我忘记了配置类中的 @EnableAspectJAutoProxy,这解决了问题。

在允许时将其标记为正确答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2023-03-10
    • 2014-06-04
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多