我可以在 java 文件和 xml 中同时使用配置 SI 和注释吗?
Can I use both configuring SI with annotation in java file and xml?
去年,spring集成发布了4.0版本,让我们不用在XML文件中配置,使用注解进行配置。但我想使用现有的 XML 配置来使用此功能。
所以我使用 spring 引导和集成注释编写了代码
@Configuration
@ComponentScan(basePackages ={"com.strongjoe.blue"},excludeFilters=@ComponentScan.Filter(type=FilterType.REGEX, pattern={"com.strongjoe.blue.agent.Bootstrap*"}))
@IntegrationComponentScan
@ImportResource( {"${context.agent.path}context-bean-*.xml", // Context Configuration
"${project.agent.path}context-properties.xml" } ) // Project Based Chain configuration
public class AgentStarter implements CommandLineRunner{
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Lazy
@Bean
@ServiceActivator(inputChannel="blue-hub-start-channel", outputChannel="blue-hub-route-channel")
public <T> BlueMessage<T> startJob(BlueMessage<T> msg) {
logger.debug("BluE Hub Agent started :{} [phrase:{}]", System.currentTimeMillis() , "prototype-version");
return msg;
}
@Lazy
@Bean
@ServiceActivator(inputChannel="blue-hub-end-channel")
public <T> BlueMessage<T> endJob(BlueMessage<T> msg) {
logger.debug("BluE Hub Agent ended :{} [phrase:{}]", System.currentTimeMillis() , "prototype-version");
return msg;
}
@Bean
@Transformer(inputChannel="blue-normalized-channeel", outputChannel="blue-output-channel")
public org.springframework.integration.transformer.Transformer JsonToMap( ) {
return new JsonToObjectTransformer( List.class );
}
@MessageEndpoint
public static class Echo {
@ServiceActivator(inputChannel="blue-output-channel")
public void stringEcho(Message message) {
}
}
@Autowired
Gateway gateway;
public static void main(String[] args) {
SpringApplication app = new SpringApplication(AgentStarter.class);
app.setWebEnvironment(false);
app.run(args).close();
}
@Override
public void run(String... args) throws Exception {
System.err.println("blue-hub-agent started..");
System.out.println(gateway.sendReceive("gitlab"));
}
我在xml.
中写下了我使用的每个频道的定义
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ws="http://www.springframework.org/schema/integration/ws"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.0.xsd
http://www.springframework.org/schema/integration/ws http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd
http://www.springframework.org/schema/integration/xml http://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-4.0.xsd">
<int:channel id="blue-normalized-channel" />
<int:channel id="blue-header-channeel" />
<int:channel id="blue-request-channel" />
<int:channel id="blue-output-channel" />
<int:channel id="blue-gitlab-request-prepare-channel" />
<int:channel id="blue-hub-start-command-channel" />
<int:channel id="blue-hub-start-channel"/>
<int:channel id="blue-hub-end-channel" />
但是我出错了。
Caused by: org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application:8090.blue-hub-start-channel'.
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:81)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:255)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:223)
原因是,我想,
XML 文件中的 spring bean 和带有注释的 spring bean 具有不同的上下文。所以我认为即使 blue-hub-start-channel 被名为 "startJob" 的服务激活器订阅,它也会出错。
我该如何解决这个问题?
在@Bean 上注释 @ServiceActivator
不适用于 POJO 消息传递。参见 the documentation。
当您以这种方式注释 @Bean
时,您必须提供适当的对象(在本例中为 MessageHandler
)。
对于 POJO 样式的注释方法,注释必须在 @Bean
方法中的方法上(就像您在这个方法上...
@MessageEndpoint
public static class Echo {
@ServiceActivator(inputChannel="blue-output-channel")
public void stringEcho(Message message) {
}
}
... 然后为 Echo 声明一个 @Bean
。
您可以将所有 @ServiceActivator
方法(和 @Transformer
方法)放在同一个 @MessageEndpoint
中。
去年,spring集成发布了4.0版本,让我们不用在XML文件中配置,使用注解进行配置。但我想使用现有的 XML 配置来使用此功能。
所以我使用 spring 引导和集成注释编写了代码
@Configuration
@ComponentScan(basePackages ={"com.strongjoe.blue"},excludeFilters=@ComponentScan.Filter(type=FilterType.REGEX, pattern={"com.strongjoe.blue.agent.Bootstrap*"}))
@IntegrationComponentScan
@ImportResource( {"${context.agent.path}context-bean-*.xml", // Context Configuration
"${project.agent.path}context-properties.xml" } ) // Project Based Chain configuration
public class AgentStarter implements CommandLineRunner{
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Lazy
@Bean
@ServiceActivator(inputChannel="blue-hub-start-channel", outputChannel="blue-hub-route-channel")
public <T> BlueMessage<T> startJob(BlueMessage<T> msg) {
logger.debug("BluE Hub Agent started :{} [phrase:{}]", System.currentTimeMillis() , "prototype-version");
return msg;
}
@Lazy
@Bean
@ServiceActivator(inputChannel="blue-hub-end-channel")
public <T> BlueMessage<T> endJob(BlueMessage<T> msg) {
logger.debug("BluE Hub Agent ended :{} [phrase:{}]", System.currentTimeMillis() , "prototype-version");
return msg;
}
@Bean
@Transformer(inputChannel="blue-normalized-channeel", outputChannel="blue-output-channel")
public org.springframework.integration.transformer.Transformer JsonToMap( ) {
return new JsonToObjectTransformer( List.class );
}
@MessageEndpoint
public static class Echo {
@ServiceActivator(inputChannel="blue-output-channel")
public void stringEcho(Message message) {
}
}
@Autowired
Gateway gateway;
public static void main(String[] args) {
SpringApplication app = new SpringApplication(AgentStarter.class);
app.setWebEnvironment(false);
app.run(args).close();
}
@Override
public void run(String... args) throws Exception {
System.err.println("blue-hub-agent started..");
System.out.println(gateway.sendReceive("gitlab"));
}
我在xml.
中写下了我使用的每个频道的定义<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ws="http://www.springframework.org/schema/integration/ws"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.0.xsd
http://www.springframework.org/schema/integration/ws http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd
http://www.springframework.org/schema/integration/xml http://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-4.0.xsd">
<int:channel id="blue-normalized-channel" />
<int:channel id="blue-header-channeel" />
<int:channel id="blue-request-channel" />
<int:channel id="blue-output-channel" />
<int:channel id="blue-gitlab-request-prepare-channel" />
<int:channel id="blue-hub-start-command-channel" />
<int:channel id="blue-hub-start-channel"/>
<int:channel id="blue-hub-end-channel" />
但是我出错了。
Caused by: org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application:8090.blue-hub-start-channel'.
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:81)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:255)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:223)
原因是,我想, XML 文件中的 spring bean 和带有注释的 spring bean 具有不同的上下文。所以我认为即使 blue-hub-start-channel 被名为 "startJob" 的服务激活器订阅,它也会出错。
我该如何解决这个问题?
在@Bean 上注释 @ServiceActivator
不适用于 POJO 消息传递。参见 the documentation。
当您以这种方式注释 @Bean
时,您必须提供适当的对象(在本例中为 MessageHandler
)。
对于 POJO 样式的注释方法,注释必须在 @Bean
方法中的方法上(就像您在这个方法上...
@MessageEndpoint
public static class Echo {
@ServiceActivator(inputChannel="blue-output-channel")
public void stringEcho(Message message) {
}
}
... 然后为 Echo 声明一个 @Bean
。
您可以将所有 @ServiceActivator
方法(和 @Transformer
方法)放在同一个 @MessageEndpoint
中。