Question

I want to create programmatically the following XML config

<int:channel id="sample">
</int:channel>

What i can do is the following

PublishSubscribeChannel channel = new PublishSubscribeChannel();

But there is no method to assign the id.

Was it helpful?

Solution

Actually <int:channel> produces DirectChannel bean.

And if you want to do it programatically and have entire Messaging Infrastructure you should configure it as bean anyway:

@Configuration
public class MyConfiguration {

   @Bean
   public MessageChannel sample() {
       return new DirectChannel();
   }

}

The id attribute is a key feature of Spring IOC container and, of course, it isn't responcibility of concrete class.

Seems to me you should to take a look into the new stuff of Spring Integration 4.0.

OTHER TIPS

The "id" is specific to a Spring application context and helps identify each instance that is being defined inside the application context.

What you have in there translates, more "verbosely" to this Spring config:

<bean id="sample" class="org.springframework.integration.channel.DirectChannel" />

The "id" identifies, like a name, a class DirectChannel instance within the context of a Spring ApplicationContext. The default channel used for a "int:channel" definition is the DirectChannel class, not PublishSubscribeChannel (DirectChannel).

But, apart from the channel itself, Spring Integration creates some other beans behind the scene.

Another way of wiring/injecting DirectChannel Bean: For Spring 3.2.3.RELEASE version

Code in Controller/Config Loader file:

@Configuration
public class ConfigControllerLoader {
  @Autowired
  @Qualifier("direct-channel-bean")
  DirectChannel drtChannel;
}

Context file bean definition:

<int:channel id="direct-channel-bean"></int:channel>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top