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

时间:2015-06-24 02:41:05

标签: spring-boot spring-integration

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

所以我使用spring boot和integration annotation编写代码

@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 个答案:

答案 0 :(得分:2)

在@Bean上注释while (true) { Console.WriteLine("Enter Name:"); name = Console.ReadLine(); // Point A if(name =="A") break; Console.WriteLine("Enter Surname:"); surname = Console.ReadLine(); // Point B if(surname == "A") break; } 不适用于POJO Messaging。请参阅the documentation

当您以这种方式注释@ServiceActivator时,您必须提供适当的对象(在这种情况下为@Bean)。

对于POJO样式的注释方法,注释必须在MessageHandler方法的方法上(就像你在这个方法上一样......

@Bean

...然后为Echo声明@MessageEndpoint public static class Echo { @ServiceActivator(inputChannel="blue-output-channel") public void stringEcho(Message message) { } }

您可以将所有@Bean方法(和@ServiceActivator s)放在同一个@Transformer中。