简体   繁体   English

Spring Web App-Autowire-找不到要注入的bean

[英]Spring web app - autowire - can not find bean to inject

I have a spring controller with an autowired object that Spring says it can't find, despite my seeing - in the log file - the bean/object for it being created in the root application context. 我有一个带有自动连线对象的spring控制器,尽管我在日志文件中看到在根应用程序上下文中为其创建的bean /对象,但Spring却找不到它。 It occurs during the deployment of the application (in Tomcat). 它发生在应用程序部署期间(在Tomcat中)。

I tried adding @Qualifier to the @Autowired field but it didn't resolve the problem. 我尝试将@Qualifier添加到@Autowired字段,但无法解决问题。

Controller: 控制器:

package com.maha.testspring.endpoints.webrest.controllers;
import com.maha.testspring.services.TestSpringService;

@Controller
@RequestMapping("/testspring")
public class TestSpringController
{
    @Autowired
    @Qualifier("testSpringService")
    private TestSpringService testSpringService;
    ...
}

war's testspring-endpoints-webrest-1.0-SNAPSHOT/ WEB-INF/web.xml 战争的testspring-endpoints-webrest-1.0-SNAPSHOT / WEB-INF / web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

war's testspring-endpoints-webrest-1.0-SNAPSHOT/ WEB-INF/spring/servlet-context.xml : 战争的testspring-endpoints-webrest-1.0-SNAPSHOT / WEB-INF / spring / servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans 
        ...
        <mvc:annotation-driven />
        <context:annotation-config />
        <context:spring-configured />
        <context:component-scan base-package="com.maha.testspring.endpoints.webrest.controllers" />

</beans:beans>

war's testspring-endpoints-webrest-1.0-SNAPSHOT/ WEB-INF/spring/root-context.xml : 战争的testspring-endpoints-webrest-1.0-SNAPSHOT / WEB-INF / spring / root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans ...
    <bean class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
            <list>
               <value>/applicationContext.testspring.services.xml</value>
            </list>
        </constructor-arg>
    </bean>
</beans>

Service implementation class: (in jar, testspring-services-impl-1.0-SNAPSHOT.jar - in the war's testspring-endpoints-webrest-1.0-SNAPSHOT\\WEB-INF\\lib folder) 服务实现类:(在jar中,testspring-services-impl-1.0-SNAPSHOT.jar-在战争的testspring-endpoints-webrest-1.0-SNAPSHOT \\ WEB-INF \\ lib文件夹中)

package com.maha.testspring.services;
import org.springframework.stereotype.Service;

@Service("testSpringService")
public class TestSpringServiceImpl implements TestSpringService {
    public void testIt() { System.out.println("..."); }
}

Service interface: (in jar, testspring-services-interfaces-1.0-SNAPSHOT.jar - in the war's testspring-endpoints-webrest-1.0-SNAPSHOT\\WEB-INF\\lib folder) 服务接口:(在jar中,testspring-services-interfaces-1.0-SNAPSHOT.jar-在战争的testspring-endpoints-webrest-1.0-SNAPSHOT \\ WEB-INF \\ lib文件夹中)

package com.maha.testspring.services;
public interface TestSpringService
{
    public void testIt();
}

applicationContext.testspring.services.xml (in jar, testspring-services-impl-1.0-SNAPSHOT.jar - in the war's testspring-endpoints-webrest-1.0-SNAPSHOT\\WEB-INF\\lib folder) applicationContext.testspring.services.xml (在jar中,testspring-services-impl-1.0-SNAPSHOT.jar-在战争的testspring-endpoints-webrest-1.0-SNAPSHOT \\ WEB-INF \\ lib文件夹中)

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
    <context:spring-configured/>
    <context:annotation-config />
    <context:component-scan base-package="com.maha.testspring.services"/>
</beans>

Logging - shows TestSpringServiceImpl was processed for @Service (as a candidate component when injecting dependency later on) 记录-显示TestSpringServiceImpl已为@Service处理(稍后注入依赖项时作为候选组件)

annotation.ClassPathBeanDefinitionScanner     - Identified candidate component class: URL  [jar:file:/C:/osd/Tomcat%208.0/webapps/testspringwebrest/WEB-INF/lib/testspring-services-impl-1.0-SNAPSHOT.jar!/com/maha/testspring/services/TestSpringServiceImpl.class]
support.ClassPathXmlApplicationContext     -  Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@41e89deb: org.springframework.beans.factory.support.DefaultListableBeanFactory@7487b2bc

Logging shows creation of bean instance for this class : 日志记录显示了该类的bean实例的创建

support.DefaultListableBeanFactory     - Creating shared instance of singleton bean 'testSpringService'
support.DefaultListableBeanFactory     - Creating instance of bean 'testSpringService'
support.DefaultListableBeanFactory     - Eagerly caching bean 'testSpringService' to allow for resolving potential circular references
support.DefaultListableBeanFactory     - Finished creating instance of bean 'testSpringService'

Error when trying to create the controller - can't find the bean to inject : 尝试创建控制器时发生错误- 找不到要注入的bean

support.DefaultListableBeanFactory     - Creating shared instance of singleton bean 'testSpringController'
support.DefaultListableBeanFactory     - Creating instance of bean 'testSpringController'
annotation.InjectionMetadata     - Registered injected element on 
class  [com.maha.testspring.endpoints.webrest.controllers.TestSpringController]:     AutowiredFieldElement for 
private   com.maha.testspring.services.TestSpringService com.maha.testspring.endpoints.webrest.controllers.TestSpringController.testSpringService
annotation.InjectionMetadata     - Processing injected element of bean 'testSpringController': 
AutowiredFieldElement for private 
com.maha.testspring.services.TestSpringService
com.maha.testspring.endpoints.webrest.controllers.TestSpringController.testSpringService

support.XmlWebApplicationContext     - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testSpringController': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private   com.maha.testspring.services.TestSpringService com.maha.testspring.endpoints.webrest.controllers.TestSpringController.testSpringService; 
nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type
[com.maha.testspring.services.TestSpringService] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency.

I had a similar problem . 我有一个类似的问题。 We solved it by keeping the CASE same . 我们通过保持CASE不变来解决它。 In your example you used @Qualifier("testSpringService") and your yout service public class TestSpringServiceImpl implements TestSpringService 在您的示例中,您使用了@Qualifier("testSpringService")并且您的服务public class TestSpringServiceImpl implements TestSpringService

TRY changing it to public class TestSpringServiceImpl implements testSpringService with a SMALL t in testSpringService. 尝试将其更改为public class TestSpringServiceImpl implements testSpringService ,以在testSpringService中public class TestSpringServiceImpl implements testSpringService SMALL t实现testSpringService。

If that dont solve your problem then try annotating the parent class (Remember that annotations are not inherited from parent class to the child and in your case you have annotated the child and not its parent ) like : 如果这样不能解决您的问题,请尝试为父类添加注释(请注意,注释不是从父类继承到孩子的,在您的情况下,您已经给孩子而不是其父添加了注释),例如:

package com.maha.testspring.services;
@Service("testSpringService")
public interface TestSpringService
{
    public void testIt();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM