我有一个带有自动对象的弹簧控制器,Spring说它无法找到,尽管我在日志文件中看到了它在根应用程序上下文中创建的bean /对象。它发生在应用程序部署期间(在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;
...
}
war的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的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的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.
答案 0 :(得分:0)
我有类似的问题。我们通过保持CASE相同来解决它。在您的示例中,您使用了@Qualifier("testSpringService")
和你的服务public class TestSpringServiceImpl implements TestSpringService
尝试使用testSpringService中的SMALL将其更改为public class TestSpringServiceImpl implements testSpringService
。
如果这不解决您的问题,那么尝试注释父类(请记住,注释不是从父类继承到子类,在您的情况下,您注释了子类而不是其父类),如:
package com.maha.testspring.services;
@Service("testSpringService")
public interface TestSpringService
{
public void testIt();
}