0

Can anyone tell me please what's is wrong with this seemingly simple Spring web app scenario. I need it to work like this so I can build a far more complex scenario. I just want to define beans in an xml file and have them Autowired in a rest controller.

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 (in 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 (in 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;
}
}

For the above example I get this error

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)}
Kurt Du Bois
  • 7,550
  • 4
  • 25
  • 33
orion_kid
  • 405
  • 1
  • 7
  • 20
  • Your `contextConfigLocation` attribute is useless. That is read by the `ContextLoaderListener` however you failed to add that in your web.xml. Hence nothing will be loaded. Next to that if it would be loaded all your beans would be duplicated due to the `` being executed twice (once by the `ContextLoaderListener` and once by the `DispatcherServlet`, currently probably not a problem, however when you start using AOP (add transactions for instance) you will run into trouble). – M. Deinum Sep 29 '14 at 09:26
  • possible duplicate of [Understanding Spring @Autowired usage](http://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage) – Xstian Sep 29 '14 at 10:57

3 Answers3

1

<context:annotation-config /> 

add this tag into your simplebean.xml file

To enable @Autowired, you have to register ‘AutowiredAnnotationBeanPostProcessor‘, and you can do it in two ways :

  1. Include Add Spring context and

<context:annotation-config /> 

in bean configuration file.

  1. Include AutowiredAnnotationBeanPostProcessor Include ‘AutowiredAnnotationBeanPostProcessor’ directly in bean configuration file.

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

And go through this Link Also.. Role/Purpose of ContextLoaderListener in Spring?

Community
  • 1
  • 1
Sunil Khokhar
  • 370
  • 1
  • 5
0

May be your simplebean.xml is not available to the container.

Try to put that file in WEB-INF folder.

And you have not written the listener in your web.xml file. Hence your contextConfigLocation is not available for the DispatcherServlet.

<!-- Configurations for the root application context (parent context) -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
  • I guess for revision 1 of the answer, and http://stackoverflow.com/questions/9213897/how-do-i-update-the-value-of-an-autowired-string-bean-in-spring – qingbo Sep 29 '14 at 09:47
  • Yes, I got downvote because I had previously a different answer. No I saw his configuration and change my answer. – Shoaib Chikate Sep 29 '14 at 09:48
0

You are missing

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

in web.xml, so the beans defined in simplebean.xml are never actually created.

varun
  • 684
  • 1
  • 11
  • 30