春のアノテーションベースのAOPアドバイス
1.注釈を定義する
public @interface ApplyAuthorisationAdvice {
}
<aop:aspectj-autoproxy />
前に行われていない場合は、春の構成ファイルに設定します
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.傍受する必要があるサービスにアノテーションを適用する
@Component("myService")
public class MyServiceImpl implements MyService {
@ApplyAuthorisationAdvice
public List<MySummary> search(MyContext searchContext) {
}
}