1

我一直在尝试在 servlet 中自动装配一个 bean,但没有这样做。在我的情况下,stackoverflow 中的现有答案似乎不起作用。

这是我的代码:

web.xml

<web-app>
   <display-name>Archetype Created Web Application</display-name>

   <context-param>
       <param-name>contextClass</param-name>
       <param-value>
           org.springframework.web.context.support.AnnotationConfigWebApplicationContext
       </param-value>
   </context-param>

   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>org.springsource.cloudfoundry.mvc.services.config.ServicesConfiguration</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/beans.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>

   <servlet>
       <servlet-name>service</servlet-name>
       <servlet-class>com.devesh.learn.mvc.controllers.RestClient</servlet-class>
   </servlet>

   <servlet-mapping>
       <servlet-name>service</servlet-name>
       <url-pattern>/hello</url-pattern>
   </servlet-mapping>

       <welcome-file-list>/hello</welcome-file-list>
</web-app>

一个我想自动装配的简单 pojo Webclient

public class WebClient {

    private String id;

    public WebClient() {
        super();
        System.out.println("webclient");
    }

    public void process() {
        System.out.println("Webclient process completed.");
    }

}

Servlet RestClient.java

public class RestClient extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = -752661275356306956L;
    private String id;

    @Autowired
    private WebClient webClient;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    private WebApplicationContext springContext;

      @Override
        public void init(final ServletConfig config) throws ServletException {
            super.init(config);
            springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
            final AutowireCapableBeanFactory beanFactory = springContext.getAutowireCapableBeanFactory();
            beanFactory.autowireBean(this);
        }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        System.out.println("Request reached doGet");        
        webClient.process();
    }

}

还有 beans.xml 文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <bean id="webClient" class="com.devesh.situation.WebClient" autowire-candidate="true">
    </bean>

    <bean id="restClient" class="com.devesh.learn.mvc.controllers.RestClient"></bean>

    <context:annotation-config></context:annotation-config>

</beans>

问题是 Servlet 类 RestClient 中的 webclient 字段为空。我正在为我的项目使用 spring 版本 4.2.2.RELEASE 依赖项(org.springframework:spring-webmvc)。

这是错误日志

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.devesh.learn.mvc.controllers.RestClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.devesh.situation.WebClient com.devesh.learn.mvc.controllers.RestClient.webClient; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.devesh.situation.WebClient] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBean(AbstractAutowireCapableBeanFactory.java:302)
    at com.devesh.learn.mvc.controllers.RestClient.init(RestClient.java:45)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1213)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.devesh.situation.WebClient com.devesh.learn.mvc.controllers.RestClient.webClient; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.devesh.situation.WebClient] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 15 more

任何帮助都感激不尽。

4

0 回答 0