【问题标题】:Spring Autowires beans from xml definition来自 xml 定义的 Spring Autowires bean
【发布时间】:2014-11-23 15:15:32
【问题描述】:

谁能告诉我这个看似简单的 Spring Web 应用场景有什么问题。我需要它像这样工作,这样我就可以构建一个更复杂的场景。 我只想在一个 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)}

【问题讨论】:

  • 你的contextConfigLocation 属性没用。这是由ContextLoaderListener 读取的,但是您未能将其添加到您的 web.xml 中。因此不会加载任何内容。除此之外,如果它会被加载,你所有的 bean 都会被复制,因为 &lt;component-scan /&gt; 被执行了两次(一次是 ContextLoaderListener,一次是 DispatcherServlet,目前可能不是问题,但是当你开始使用 AOP 时(例如添加事务)你会遇到麻烦)。

标签: spring autowired


【解决方案1】:

可能是您的 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>

【讨论】:

  • 我猜是答案的第 1 版,stackoverflow.com/questions/9213897/…
  • 是的,我投了反对票,因为我以前有不同的答案。不,我看到了他的配置并改变了我的答案。
【解决方案2】:

&lt;context:annotation-config /&gt; 

将此标签添加到您的 simplebean.xml 文件中

要启用@Autowired,你必须注册‘AutowiredAnnotationBeanPostProcessor’,你可以通过两种方式来实现:

  1. 包括 添加 Spring 上下文和

&lt;context:annotation-config /&gt; 

在 bean 配置文件中。

  1. 包括 AutowiredAnnotationBeanPostProcessor 直接在 bean 配置文件中包含“AutowiredAnnotationBeanPostProcessor”。

<bean 
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

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

【讨论】:

    【解决方案3】:

    你不见了

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-09
      • 2011-08-28
      • 2022-11-22
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多