来自xml定义的Spring Autowired bean

时间:2014-09-29 09:13:09

标签: spring autowired

有谁能告诉我这个看似简单的Spring网络应用场景有什么问题。我需要它像这样工作,所以我可以构建一个更复杂的场景。 我只想在xml文件中定义bean,并在休息控制器中将它们自动装配。

的web.xml:

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

<servlet>
    <servlet-name>mycompany</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mycompany</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:mypackage/simplebean.xml</param-value>
</context-param>

</web-app>

myCompany的-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
   xmlns:mvc="http://www.springframework.org/schema/mvc"    
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:util="http://www.springframework.org/schema/util"    
   xsi:schemaLocation="http://www.springframework.org/schema/beans    
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
   http://www.springframework.org/schema/mvc    
   http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
   http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util-3.2.xsd">  

<context:component-scan base-package="com.mycompany" />     

</beans>

simplebean.xml(在src / main / resources / mypackage中)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/util
                    http://www.springframework.org/schema/util/spring-util-3.2.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd">

<context:component-scan base-package="com.mycompany" /> 

<bean id="myString" class="java.lang.String">
    <constructor-arg value="A_String"/>
</bean>

</beans>

TestRestController(在com.mycompany中)

package com.mycompany;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("services/test")
public class TestRestController {

@Autowired
private String myString;

@RequestMapping(value = "/testService", method = RequestMethod.GET)
@ResponseBody
public Integer testSendMessage(HttpServletRequest request) throws Exception {

    System.out.println("myString: " + myString);
    return 0;
}
}

对于上面的示例,我收到此错误

org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.mycompany.TestRestController.myString; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] 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)}

3 个答案:

答案 0 :(得分:1)

&#13;
&#13;
<context:annotation-config /> 
&#13;
&#13;
&#13;

将此标记添加到simplebean.xml文件

要启用@Autowired,您必须注册'AutowiredAnnotationBeanPostProcessor',您可以通过两种方式执行此操作:

  1. 包含 添加Spring上下文和
  2. &#13;
    &#13;
    <context:annotation-config /> 
    &#13;
    &#13;
    &#13;

    在bean配置文件中。

    1. 包含AutowiredAnnotationBeanPostProcessor 直接在bean配置文件中包含'AutowiredAnnotationBeanPostProcessor'。
    2. &#13;
      &#13;
      <bean 
      class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
      &#13;
      &#13;
      &#13;

      并通过此链接也.. 的 Role/Purpose of ContextLoaderListener in Spring?

答案 1 :(得分:0)

可能是您的simplebean.xml不可用于容器。

尝试将该文件放在WEB-INF文件夹中。

您还没有在web.xml文件中编写监听器。因此,您的contextConfigLocation不适用于DispatcherServlet。

<!-- Configurations for the root application context (parent context) -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

答案 2 :(得分:0)

你错过了

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

在web.xml中,因此从未实际创建在simplebean.xml中定义的bean。