首页 文章

我可以在java文件和xml中同时使用SI和注释吗?

提问于
浏览 1761
1

去年,Spring集成发布了4.0版本,我们可以使用注释进行配置而无需在XML文件中进行配置 . 但我想使用现有的XML配置来使用此功能 .

所以我使用spring boot和integration注释编写了代码

@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”的服务激活器订阅,也会出错 .

我怎么解决这个问题?

1 回答

  • 2

    在@Bean上注释 @ServiceActivator 不适用于POJO Messaging . 见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 s)放在同一个 @MessageEndpoint 中 .

相关问题