Spring4@Transactional和@Aspect
我有一个监视对象创建的拦截器类。我希望在服务的实际创建方法注入当前用户/日期之前调用这个拦截器Spring4@Transactional和@Aspect,spring,transactions,aspectj,Spring,Transactions,Aspectj,我有一个监视对象创建的拦截器类。我希望在服务的实际创建方法注入当前用户/日期之前调用这个拦截器 /** * Author: plalonde * When: 2015-03-04 */ @Aspect public class IdentityInterceptor { /** * Injection de l'usagé actuellement en session dans l'entité. * * @param entity L'entité
/**
* 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> {
}
正如M.Deinum评论中提到的,我忘记了配置类中的@enableSpectJautoproxy,这解决了问题
将在允许时将此标记为正确答案。WatsonLicenseDetails是如何定义的?您对该特性不做任何操作。您需要告诉Spring,您希望为此启用aspectj,并将@enableSpectJautoproxy添加到您的配置中
/**
* 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();
}
}