繁体   English   中英

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

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

我有一个带有自动连线对象的spring控制器,尽管我在日志文件中看到在根应用程序上下文中为其创建的bean /对象,但Spring却找不到它。 它发生在应用程序部署期间(在Tomcat中)。

我尝试将@Qualifier添加到@Autowired字段,但无法解决问题。

控制器:

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;
    ...
}

战争的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>

战争的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>

战争的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>

服务实现类:(在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("..."); }
}

服务接口:(在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 (在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>

记录-显示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

日志记录显示了该类的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'

尝试创建控制器时发生错误- 找不到要注入的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.

我有一个类似的问题。 我们通过保持CASE不变来解决它。 在您的示例中,您使用了@Qualifier("testSpringService")并且您的服务public class TestSpringServiceImpl implements TestSpringService

尝试将其更改为public class TestSpringServiceImpl implements testSpringService ,以在testSpringService中public class TestSpringServiceImpl implements testSpringService SMALL t实现testSpringService。

如果这样不能解决您的问题,请尝试为父类添加注释(请注意,注释不是从父类继承到孩子的,在您的情况下,您已经给孩子而不是其父添加了注释),例如:

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