我想通过AOP拦截所有弹簧集成网关。
有可能吗?如果不是什么可能是最好的方式来做日志输入对象来到网关?
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class AdviceExample {
@Autowired
private TestGateway testGateway;
@Test
public void testIt() {
System.out.println(this.testGateway.testIt("foo"));
}
@MessagingGateway
public interface TestGateway {
@Gateway(requestChannel = "testChannel")
@CustomAnnotation
String testIt(String payload);
}
@Configuration
@EnableIntegration
@IntegrationComponentScan
@EnableMessageHistory
@EnableAspectJAutoProxy
public static class ContextConfiguration {
LoggingHandler logger = new LoggingHandler(LoggingHandler.Level.INFO.name());
@Bean
public IntegrationFlow testFlow() {
return IntegrationFlows.from("testChannel")
.transform("payload.toUpperCase()")
.channel("testChannel")
.transform("payload.concat(' Manoj')")
.channel("testChannel")
.handle(logger)
.get();
}
@Bean
public GatewayAdvice gtwyAdvice(){
return new GatewayAdvice();
}
}
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
@Inherited
public @interface CustomAnnotation{
}
@Aspect
public static class GatewayAdvice {
@Before("execution(* advice.AdviceExample.TestGateway.testIt(*))")
public void beforeAdvice() {
System.out.println("Before advice called...");
}
@Before("@annotation(advice.AdviceExample.CustomAnnotation)")
public void beforeAnnotationAdvice() {
System.out.println("Before annotation advice called...");
}
}
}
答案 0 :(得分:3)
是的,你可以这样做。看看标准Spring AOP Framework。由于所有这些@Gateway
最终都是bean,你可以通过它们的bean名称和特定方法为它们添加任何Advice
,如果是这样的话。例如,我们经常建议在网关的方法上使用@Transactional
。这正是一个样本"如何在集成网关上使用AOP"。