【发布时间】: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