如何在 spring 集成网关上使用 AOP?
How to use AOP on spring integration gateways?
我想通过 AOP 拦截所有 spring 集成网关。
可以吗?如果不是,什么可能是处理进入网关的日志输入对象的最佳方式?
@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...");
}
}
}
是的,你可以做到。看看标准 Spring AOP Framework。由于所有这些 @Gateway
最后都是 bean,您可以通过它们的 bean 名称和特定方法为它们添加任何 Advice
,如果是的话。例如,我们经常建议在网关的方法上使用 @Transactional
。这正是一个示例 "how to use AOP on integration gateway".
我想通过 AOP 拦截所有 spring 集成网关。
可以吗?如果不是,什么可能是处理进入网关的日志输入对象的最佳方式?
@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...");
}
}
}
是的,你可以做到。看看标准 Spring AOP Framework。由于所有这些 @Gateway
最后都是 bean,您可以通过它们的 bean 名称和特定方法为它们添加任何 Advice
,如果是的话。例如,我们经常建议在网关的方法上使用 @Transactional
。这正是一个示例 "how to use AOP on integration gateway".