2014-09-29 67 views
0

任何人都可以告訴我這個看似簡單的Spring Web應用場景有什麼問題。我需要它這樣工作,所以我可以構建一個更復雜的場景。 我只想在xml文件中定義bean,並讓它們在休息控制器中自動裝配。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 /主/資源/ 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)} 
+0

您的'contextConfigLocation'屬性是無用的。這是由'ContextLoaderListener'讀取的,但是你沒有在你的web.xml中添加它。因此沒有什麼會被加載。接下來,如果它將被加載,所有的bean將被複制,這是由於被執行了兩次(一次由'ContextLoaderListener'執行,一次由'DispatcherServlet'執行,目前可能不是問題,但是當你開始使用AOP(例如添加交易)時,你會遇到麻煩)。 – 2014-09-29 09:26:38

+0

[Understanding Spring @Autowired usage]可能的重複(http://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage) – Xstian 2014-09-29 10:57:16

回答

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> 
+0

這似乎是一個合法的答案,爲什麼downvoted? – varun 2014-09-29 09:42:56

+0

我猜想修訂1的答案,和http://stackoverflow.com/questions/9213897/how-do-i-update-the-value-of-an-autowired-string-bean-in-spring – qingbo 2014-09-29 09:47:43

+0

是,我因爲以前有不同的答案而退縮了。不,我看到他的配置並更改我的答案。 – 2014-09-29 09:48:52

1

<context:annotation-config />

添加此標記到您的simplebean.xml文件

要啓用@Autowired,你必須註冊 'AutowiredAnnotationBeanPostProcessor',你可以用兩種方式做到這一點:

  1. 包括 添加Spring上下文和

<context:annotation-config />

在bean配置文件。

  1. Include AutowiredAnnotationBeanPostProcessor 直接在bean配置文件中包含'AutowiredAnnotationBeanPostProcessor'。

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

而且通過這個鏈接還.. Role/Purpose of ContextLoaderListener in Spring?

0

你缺少

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

,所以都沒有真正建立在simplebean.xml定義的豆子。