vendredi 27 février 2015

Spring Autowire in Controller not working

I am unable to get my @Autowired annotation working in my Spring REST Controller. (The controller itself works when I remove its @Autowired dependency).


I am running this on Windows under Apache Tomcat v7 with a java runtime and compile-time compiler version 1.6.


The URL is:



http://localhost:8080/bill-web/rest/article/get/chapterName/sdfsa/searchQueryInput/sadfsfa


The error is:



org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'articleController':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field:
private org.ieee.bill.services.ArticleService org.ieee.bill.web.controller.ArticleController.articleService;

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.ieee.bill.services.ArticleService]
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)}


WEB-INF/web.xml:



<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://ift.tt/ra1lAU"
xmlns="http://ift.tt/nSRXKP"
xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/LU8AHS"
id="WebApp_ID" version="2.5">

<display-name>bill-web</display-name>

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>

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

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>


WEB-INF/rest-servlet.xml:



<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns:mvc="http://ift.tt/1bHqwjR"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:beans="http://ift.tt/GArMu6"
xmlns:context="http://ift.tt/GArMu7"
xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="
http://ift.tt/1bHqwjR
http://ift.tt/1fmimld
http://ift.tt/GArMu6
http://ift.tt/1jdM0fG
http://ift.tt/GArMu7
http://ift.tt/1jdLYo7">
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</beans:bean>
<beans:bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
</beans:bean>

<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jacksonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>

<mvc:annotation-driven />
<context:annotation-config />
<context:spring-configured />
<context:component-scan base-package="org.ieee.bill.web.controller" />
</beans:beans>


WEB-INF/spring/application-config.xml:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:aop="http://ift.tt/OpNdV1"
xmlns:context="http://ift.tt/GArMu7"
xmlns:tx="http://ift.tt/OGfeU2"
xmlns:batch="http://ift.tt/1fayA4Z"
xmlns:task="http://ift.tt/LFBt0P"
xsi:schemaLocation="
http://ift.tt/GArMu6
http://ift.tt/GAf8ZW
http://ift.tt/OpNdV1
http://ift.tt/OGfcLK
http://ift.tt/OGfeU2
http://ift.tt/OGffan
http://ift.tt/GArMu7
http://ift.tt/1bb5cbf
http://ift.tt/LFBt0P
http://ift.tt/1k4aabm
">

<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="org.ieee.bill.services"/>
</beans>


ArticleController (the REST controller):



package org.ieee.bill.web.controller;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Qualifier;
import org.apache.log4j.Logger;

import org.ieee.bill.domain.vo.Article;
import org.ieee.bill.services.ArticleService;

@Controller
@RequestMapping(value = "/article")
public class ArticleController {

static Logger log = Logger.getLogger(ArticleController.class.getName());

@Autowired
@Qualifier("articleService")
private ArticleService articleService;

@RequestMapping(
value = "/get/chapterName/{chapterName}/searchQueryInput/{searchQueryInput}",
method = RequestMethod.GET,
headers = "Accept=application/json")
public @ResponseBody Collection<Article> getArticles(
@PathVariable final String chapterName,
@PathVariable final String searchQueryInput,
final HttpServletRequest request,
final HttpServletResponse response
)
throws Exception
{
log.debug("Entered ArticleController.getArticles");
ArrayList<Article> articles = null;

articles = new ArrayList<Article>();
articles.add(new Article("Camping", "It is fun"));
articles.add(new Article("Exercising", "It is healthy"));

return articles;
}
}


ArticleService.java:



package org.ieee.bill.services;

import java.util.List;
import java.util.ArrayList;

import org.ieee.bill.domain.vo.Article;
import org.ieee.bill.services.ArticleService;
import org.springframework.stereotype.Service;

@Service("articleService")
public class ArticleServiceImpl implements ArticleService {

@Override
public List<Article> getArticles(String chapterName, String searchQuery)
{
ArrayList<Article> articles = null;

articles = new ArrayList<Article>();
articles.add(new Article("Camping", "It is fun"));
articles.add(new Article("Exercising", "It is healthy"));

return articles;
}
}


List of jar files packaged in my WEB-INF/lib:



antlr-3.1.1.jar
aopalliance-1.0.jar
aspectjrt-1.7.2.jar
bill-domain.jar
bill-services-impl.jar
bill-services-interfaces.jar
commons-beanutils.jar
commons-codec-1.4.jar
commons-collections-3.2.jar
commons-dbcp-1.4.jar
commons-digester.jar
commons-fileupload-1.3.1.jar
commons-httpclient-3.1.jar
commons-io-2.2.jar
commons-lang-2.3.jar
commons-logging-1.1.3.jar
commons-pool-1.6.jar
commons-primitives-1.0.jar
commons-validator.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.7.Final.jar
hibernate-entitymanager-4.3.7.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-validator-5.0.3.Final.jar
jackson-annotations-2.5.0.jar
jackson-core-2.5.0.jar
jackson-databind-2.5.0.jar
jackson-datatype-joda-2.5.0.jar
jackson-datatype-jsr310-2.5.0.jar
jackson-mapper-asl-1.9.13.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA - Copy.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
log4j-1.2.17.jar
ojdbc6.jar
slf4j-api-1.7.6.jar
slf4j-simple-1.6.1.jar
spring-aop-4.1.1.RELEASE.jar
spring-aspects-4.0.8.RELEASE.jar
spring-beans-4.1.1.RELEASE.jar
spring-context-4.0.8.RELEASE.jar
spring-core-4.1.1.RELEASE.jar
spring-data-commons-1.8.4.RELEASE.jar
spring-data-jpa-1.6.4.RELEASE.jar
spring-expression-4.1.1.RELEASE.jar
spring-jdbc-4.1.1.RELEASE.jar
spring-orm-4.1.1.RELEASE.jar
spring-security-acl-3.2.5.RELEASE.jar
spring-security-config-3.2.5.RELEASE.jar
spring-security-taglibs-3.2.5.RELEASE.jar
spring-security-web-3.2.5.RELEASE.jar
spring-tx-4.1.1.RELEASE.jar
spring-web-4.1.1.RELEASE.jar
spring-webmvc-4.1.1.RELEASE.jar

Aucun commentaire:

Enregistrer un commentaire