2017-03-27 56 views
0

我已閱讀TCP-Server-Client並嘗試運行我的例子,但我收到以下錯誤:Spring集成無法自動裝配領域

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 

我的控制器:

@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"; 
    } 

} 

我的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-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> 

我還沒有弄清楚,如果需要其他任何額外的配置文件。

編輯1: 我的配置文件:

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

你是如何創建應用程序上下文的? –

+0

@GaryRussell,我沒有使用XML文件,只有Java註釋,請參閱「編輯1」(如果我理解你的問題是正確的)。 – KostasC

回答

1

您需要導入XML配置。

@ImportResource("foo.xml")添加到您的@Configuration類。

或者,使用@Bean而不是XML添加Spring集成配置。

+0

感謝您的幫助和快速回復。 – KostasC

0

好像你還沒有指定的組件掃描位置。嘗試將以下標記添加到XML。

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