KostasC
KostasC

Reputation: 1106

Spring Integration Could not autowire field

I have read the TCP-Server-Client and try to run my example but I am getting the following error:

2017-03-27 15:13:08 ERROR ContextLoader:331 - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorldController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.example.myapp.integration.SimpleGateway com.example.myapp.controller.HelloWorldContr oller.gw; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.myapp.integration.SimpleGateway] found  for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
                    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
                    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
                    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
               at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.example.myapp.integration.SimpleGateway com.example.myapp.controller.Hell oWorldController.gw; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.myapp.integration.SimpleGat eway] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.ann otation.Autowired(required=true)}
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
                    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
                    ... 26 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.myapp.integration.SimpleGateway] found for dependency: ex pected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=tru e)}
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
                    ... 28 more

My Controller:

@Controller
public class HelloWorldController {

    @Autowired
    SimpleGateway gw;

    @RequestMapping(value = { "/", "/home", "/frontend" }, method = RequestMethod.GET)
    public String homePage(Locale locale, ModelMap model) {

        gw.send("test");

        return "frontend";
    }

}

My xml configuration:

<?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-ip="http://www.springframework.org/schema/integration/ip"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">

    <context:property-placeholder />

    <!-- Client side -->
    <int:gateway id="gw"
        service-interface="com.example.myapp.integration.SimpleGateway"
        default-request-channel="input"/>

    <!-- Create a connection for the client gateway that uses the same Stx-Etx deserializer to turn the stream
    into the appropriate content (it looks for the Stx byte, extracts anything between it and the Etx byte). We
    don't specify the serializer (although we could) because the unit test explicitly shows how the content
    to be sent is wrapped by the Stx and Etx bytes. -->
    <int-ip:tcp-connection-factory id="client"
        type="client"
        host="localhost"
        port="50001"
        single-use="true"
        so-timeout="10000"
        deserializer="connectionSerializeDeserialize"/>

    <int:channel id="input" />

    <int-ip:tcp-outbound-gateway id="outGateway"
        request-channel="input"
        reply-channel="clientBytes2StringChannel"
        connection-factory="client"
        request-timeout="10000"
        reply-timeout="10000"/>

    <int:channel id="clientBytes2StringChannel"/>

    <int:object-to-string-transformer id="clientBytes2String"
        input-channel="clientBytes2StringChannel" />

    <!-- Server side -->
    <!-- When creating the socket factory on the server side, we specify both the serializer and deserializer
    which deals with both accepting a stream formatted with the Stx-Etx bytes as well as sending a stream
    formatted with the Stx-Etx bytes. -->
    <int-ip:tcp-connection-factory id="serverConnectionFactory"
        type="server"
        port="65535"
        serializer="connectionSerializeDeserialize"
        deserializer="connectionSerializeDeserialize"/>


    <bean id="connectionSerializeDeserialize" class="org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer"/>


    <int-ip:tcp-inbound-gateway id="gatewayCrLf"
        connection-factory="serverConnectionFactory"
        request-channel="incomingServerChannel"
        error-channel="errorChannel"/>

    <!-- We leave a message listener off of this channel on purpose because we hook
    one up before the test actually runs (see the unit test associated with this
    context file) -->
    <int:channel id="incomingServerChannel" />

</beans>

I have not figured out, if is needed any other additional configuration file.

Edit 1: My configuration file:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.myapp")
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter {
   ...
}

Upvotes: 1

Views: 901

Answers (2)

Gary Russell
Gary Russell

Reputation: 174554

You need to import the XML configuration.

Add @ImportResource("foo.xml") to your @Configuration class.

Or, add the Spring Integration configuration using @Beans instead of XML.

Upvotes: 1

Durlabh Sharma
Durlabh Sharma

Reputation: 407

It seems like you haven't specified the component scan location. Try adding the below tag to XML.

<context:component-scan base-package="<package-to-scan-under-spring-servlet>" />

Upvotes: 0

Related Questions