Annotation based AOP advice in spring
1.define annotation
public @interface ApplyAuthorisationAdvice {
}
Set <aop:aspectj-autoproxy />
in spring config file if not done before
2.
import org.aopalliance.aop.Advice;
import org.aspectj.lang.annotation.Aspect;
@Aspect
@Component
public class AuthorisationAspect implements Advice {
private final static Logger logger = Logger.getLogger(AuthorisationAspect.class);
@Autowired
MyService myService;
@SuppressWarnings("unchecked")
@AfterReturning(pointcut = "@annotation(com.example.ApplyAuthorisationAdvice)", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
if (result != null && (result instanceof List)) {
// filter stuff
}
}
}
3.Apply annotation on service which needs to be intercepted
@Component("myService")
public class MyServiceImpl implements MyService {
@ApplyAuthorisationAdvice
public List<MySummary> search(MyContext searchContext) {
}
}