我有一个带有事务注释的类:
@Service
@EnableTransactionManagement(order=2000)
public class UserManagementServiceImpl implements UserManagementService {
@Transactional
public User addUser(User user) throws AlreadyExistException {
// ...
}
}
我有一个Aspect类:
@Component
@Aspect
public class PublisherAspect {
@Around("execution(* com.toi.expresso.service.UserManagementService.addUser(..))")
public User publishAddUserEvent(ProceedingJoinPoint jp) {
// ...
}
}
我的@Around
注释在addUser
保存数据库中的数据之前执行。请建议我在数据库中保存数据后如何执行@Around
。
答案 0 :(得分:0)
您方面的有趣部分是...
,即建议机构。假设您的addUser
方法确实返回User
而不是void
(我期望的)或其他任何方法,您可以将切入点缩小到实际拥有User
的方法返回类型,以避免返回其他类型的方法的问题。
@Component
@Aspect
public class PublisherAspect {
@Around("execution(User com.toi.expresso.service.UserManagementService.addUser(..))")
public User publishAddUserEvent(ProceedingJoinPoint jp) {
// Do something before method execution
User user = jp.proceed();
// Do something after method execution
return user;
}
}
如果要匹配所有返回类型,则使用更通用的解决方案:
@Component
@Aspect
public class PublisherAspect {
@Around("execution(* com.toi.expresso.service.UserManagementService.addUser(..))")
public Object publishAddUserEvent(ProceedingJoinPoint jp) {
// Do something before method execution
Object result = jp.proceed();
// Do something after method execution
return result;
}
}
答案 1 :(得分:0)
是'adduser'
正在返回User对象。但是,如果我正在检查同一用户的数据库,则返回空集后,在持有用户对象之后。
@Component
@Aspect
public class PublisherAspect {
@Around("execution(User com.toi.expresso.service.UserManagementService.addUser(..))")
public User publishAddUserEvent(ProceedingJoinPoint jp)
{
// Do something before method execution
User user = jp.proceed();
//checking in database(Sending message to other application but that application didn't find user in same db)
return user;
}
}
}