Can't #Autowire a bean into a controller - NoSuchBeanDefinitionException - java

I have a spring-mvc web app.
The following code has been simplified and sanitised to protect the guilty. The basic idea is that I have two separate context configuration files: one for MVC, the other for general config stuff.
When Spring goes to wire up the controller, I get:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.ConfigBean com.example.ConfigController.config; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.example.ConfigBean] 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)}
However, the following code from another servlet works just fine:
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
ConfigBean config = context.getBean(ConfigBean.class);
This suggests to me that the MVC component scanner can't see the config stuff for some reason. I've tried adding
<import resource="config.xml" />
to dispatcher-servlet.xml, but that made no difference. It feels wrong anyway, as I don't want two instances of the config beans. Even manually copying the ConfigBean declaration into dispatcher.xml doesn't fix my problem, which suggests to me I'm doing something really dumb. Any suggestions on what I might be missing?
My detailed configuration is as follows:
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
/WEB-INF/config.xml
</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
... Some other non-spring stuff ...
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<context:component-scan base-package="com.example" />
<mvc:annotation-driven />
</beans>
classpath:config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean class="com.example.ConfigBean">
<property name="foo" value="bar" />
</bean>
</beans>
ConfigController.java
package com.example;
import com.example.ConfigBean
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("/config")
public class ConfigController {
#Autowired
private ConfigBean config;
#RequestMapping(method=RequestMethod.GET)
public #ResponseBody ConfigBean getConfig() {
return config;
}
}

It might be because you have added /WEB-INF/dispatcher-servlet.xml to contextConfigLocation in context-param.
It is not required as the web-application context will ready the file based on the displater servlet name
From Spring Doc
Upon initialization of a DispatcherServlet, Spring MVC looks for a
file named [servlet-name]-servlet.xml in the WEB-INF directory of your
web application and creates the beans defined there, overriding the
definitions of any beans defined with the same name in the global
scope.

I think the autowiring is failing due to multiple instance of ConfigBean bean being present. The error message is slightly confusing. You get the same error if the bean is missing or if there are more than one instance.
The problem is with the component scan in the dispatcher-servlet.xml. Assuming that the ConfigBean class is also annotated with #Component or one of it's subtypes two instances will get created. Once for the bean definition in config.xml and once by component scanning.
To fix this you need to change the component-scan to ignore non-controller classes.
<context:component-scan user-default-filters='false'>
<context:include-filter annotation="org.springframework.stereotype.Controller"/>
</context:component-scan>
The lookup in the other servlet class is working because you are accessing the root application context directly.

This did turn out to be pretty dumb. The <context:annotation-config /> directive should have been in config.xml, not dispatcher-servlet.xml.
As I understand it, the annotation stuff is in the parent context, so that's where the directive belongs.

Related

Doesn't see a bean using Autowired annotation, nothing helps

I'm trying to autowire the Service in my Spring MVC app and it always fails! Tried everything from every question on the Internet but I couldn't find the answer. The problem is that Spring actually can't autowire field and says there is no such bean,
Tried everything, changing the code, configuration, etc. Maybe for now everything in my project is too messed up and I can't find the bug.
#Controller
public class ViewController {
#Autowired
private IUserService userService;
public void setUserService(IUserService userService) {
this.userService = userService;
}
}
#Component
#Service("userService")
public class UserServiceImpl implements IUserService {
private static final SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").
addAnnotatedClass(User.class).addAnnotatedClass(Restaurant.class).
buildSessionFactory();
public UserServiceImpl() {
}
}
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.springmvcdemo.Controller" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>spring-mvc-demo</display-name>
<absolute-ordering />
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com.springmvcdemo.Services" />
</beans>
Error creating bean with name 'viewController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.springmvcdemo.Services.IUserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
So the problem is that the SessionFactory was being constructed in a static field of the service.
The class could be found, but there was some error creating the session factory which is why the error message contains "Instantiation of bean failed".
Moving the session factory to be managed by Spring and then injecting it was the way to go.

getting 404 when consumiing spring controller service

I have created a spring application (Spring version 3.2.8). The application is working fine but the issue which I am facing is that when I try to consume the controller service through url I am getting 404 error
The url which I try to consume through browser url is given below, which I expect to return a test string
http://localhost/Spring3Sample/user/getPersonDetails
My code is as given below
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" />
</beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.controllers" />
</beans>
UserController.java
package com.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/user")
public class UserController {
private static final String APP_JSON = "application/json";
#RequestMapping(value = "/getPersonDetails", method = RequestMethod.GET, produces = APP_JSON)
public String getPersonDetails() {
return "test";
}
}
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>Spring3Sample</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
</web-app>
Can anyone please tell me some solution for this
UPDATE 1
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_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Spring3Sample</display-name>
:
:
:
You have mapped the DispatcherServlet with URL pattern as
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Whenever you want t request a controller method you must have to add this URL pattern to the request. So that Spring will find the correct mapping this.
So change your request URL to following:-
http://localhost:8080/Spring3Sample/user/getPersonDetails.do
As you expect to return a test string:
Add the
#ResponseBody
annotation to ur controller's method. So that Spring wouldn't look for the JSP page with name test, instead return test as String.
EDIT:
From the discussion in chat:
Also u are missing <mvc:annotation-driven /> in applicationContext.xml.
To remove *.do from the request URL:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
After observing your applicationContext.xml , I can see you are missing <mvc:annotation-driven/> Your Updated code will look like below
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.controllers" />
<mvc:annotation-driven/>
</beans>
Explanation
<annotation-driven /> means that you can define spring beans dependencies without actually having to specify a bunch of elements in xml or implement an interface or extend a base class.Means #Controller tells spring that the the class specified contains methods that will handle http requests without you having to implement the Controller interface or extend a subclass that implements controller.
<context:component-scan base-package="com.controllers" /> tells spring that it should search the class path for all the classes under com.controllers and look at each class to see if it has a #Controller, or #Repository, or #Service, or #Component and if it does then Spring will register the class with the bean factory as if you have defined in in the xml configuration files
As per user comment
do we need both applicationContext.xml and dispatcher-servlet.xml
Actually Spring lets you define multiple contexts in a parent-child hierarchy.
applicationContext.xml defines the beans for the root webapp context means the context associated with the webapp.
dispatcher-servlet.xml defines the beans for one servlet's application context. There can be many dispatcher-servlet.xml in a webapp i.e servlet1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2
note:: Beans in spring-servlet.xml can reference beans in
applicationContext.xml, but not vice versa.
You missed port number in your URL
Use this URL
http://localhost:8080/Spring3Sample/user/getPersonDetails
First in Spring MVC manual
The #Controller annotation acts as a stereotype for the annotated
class, indicating its role. The dispatcher scans such annotated
classes for mapped methods and detects #RequestMapping annotations
(see the next section).
So delete this
#RequestMapping("/user")
Because it makes no sense next
#RequestMapping("/user/getPersonDetails")
you should note I add "/user" to url.
Also you are not return any jSon. First if I were you I change second request mapping (on the method getUserDetails) with string I put in this answer.
And looks like you miss with config files...
Check this string in web.xml
<url-pattern>*.do</url-pattern>
it will dispatch only urls with ".do" at their end.
I added this to my config class that extends WebMvcConfigurer:
#Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/pages/");
bean.setSuffix(".jsp");
return bean;
}
You'll have to add this dependency for it to work:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

Spring attempting to #Autowire my dependencies twice

I have a Spring MVC application in which I'm trying to use #Autowired inside a class annotated with #Controller. In classes without the #Controller annotation, #Autowired works fine, as soon as I add the #Controller annotation, I get an enormous stacktrace at startup which mainly boils down to No qualifying bean of type [com.fasterxml.jackson.databind.ObjectMapper] found for dependency
I'm sensing that Spring is trying to autowire my dependency twice?
I'll get to the prove further down in my question ...
In my web.xml, I'm loading both my contextConfigLocation and DispatcherServlet:
....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext.xml</param-value>
</context-param>
....
<servlet>
<servlet-name>ahpw-api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/webmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ahpw-api</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
....
webmvc-config.xml contains just the bare basics to allow me to open up a Jackson JSON API:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="com.ahpw.api" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!-- Cookies Filter to set cookies on JSON AJAX responses -->
<mvc:interceptors>
<bean id="cookieInterceptor" class="com.ahpw.api.controller.COOKIEFilter"/>
</mvc:interceptors>
</beans>
In my applicationContext.xml I have the following:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<context:spring-configured/>
<context:component-scan base-package="com.ahpw.api" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Service" type="annotation"/>
</context:component-scan>
<!-- Setup Jackson instance -->
<bean id="jackson" class="com.fasterxml.jackson.databind.ObjectMapper" />
<!-- Setup RabbitMQ -->
<bean id="nativeConnectionFactory" class="com.rabbitmq.client.ConnectionFactory">
<property name="connectionTimeout" value="${rabbit.connection.timeout}"/>
<property name="requestedHeartbeat" value="${rabbit.heartbeat}"/>
</bean>
<rabbit:connection-factory
id="connectionFactory"
port="${rabbit.port}"
virtual-host="${rabbit.virtual}"
host="${rabbit.host}"
username="${rabbit.username}"
password="${rabbit.password}"
connection-factory="nativeConnectionFactory"/>
<rabbit:admin connection-factory="connectionFactory"/>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" reply-timeout="${rabbit.rpc.timeout}" />
</beans>
My class where the conflict is happening starts like this:
#Service
#Controller
#RequestMapping("/auth")
#JsonIgnoreProperties(ignoreUnknown = true)
public class AuthenticationController extends AbstractRPCPublisher<AuthenticationRequest, AuthenticationResponse> {
#Autowired
AmqpTemplate template;
#Autowired
ObjectMapper mapper;
If I remove #Controller, everything starts up fine, but obviously the request mapping of /auth stops working.
If I put #Controller back and duplicate the jackson bean and rabbitMQ goodies in webmvc-config.xml, it starts up without errors, but it means having two instances of each resource, a copy of the configs in both WEB-INF and META-INF, not desirable.
Is there a way to instantiate my controllers via webmvc-config.xml, but tell it to ignore #Autowired so that my applicationContext can take care of them and if so, will #Autowire function as normal?
You must define the ContextLoaderListener listener which will create the root context of your web application and load the contextConfigLocation define in the <context-param> tags:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Thus bean defined in the applicationContext.xml application context will be available to the webmvc-config.xml application context.
I don't know if it is a typo but the AuthenticationController class must not be annotated with #Service, #Controller is enough.

#Autowired doesn't work when using Jersey with Spring

I am trying to use Jersey with Spring. But for the life of me, I can't figure out why my Spring dependencies aren't being injected into my Rest Class.
Each time I try and invoke the autowired dependency I get NULL. Can anyone suggest why my dependency isn't being injected?
My Rest Class looks like this:
com.myapp.rest
#Component
#Scope("request")
#Path("/home")
public class ChartResource {
#Autowired
ChartService chartService;
#GET
#Path("/chart")
#Produces(APPLICATION_JSON)
public Bean getChart() {
return chartService.retrieveChart();
}
My web.xml file looks like this
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.codehaus.jackson.jaxrs;com.myapp.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.codehaus.jackson.jaxrs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
My applicationContext.xml is standard, and specifies the base package for component scanning:
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.myapp" />
</beans:beans>
Try adding
<context:annotation-config />
I'm not sure what you'll need to get Jersey's annotations to work, but #Autowired is a Spring annotation so you'll need to use the Spring version of <annotation-config> in order to get that to work properly.
Ok - silly question. My applicationContext wasn't being loaded and so it wasn't picking up the package I defined for component scanning!

Bean not in the JNDI

I created a small web service(test), I'm unable to get it to deploy to jboss server. It might be something with one of these files, this is my web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/testing/*</url-pattern>
</servlet-mapping>
</web-app>
here is servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Imports user-defined #Controller beans that process client requests -->
<beans:import resource="controllers.xml" />
</beans:beans>
And controllers.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- This required so that Spring can recognize our annotated beans -->
<context:annotation-config />
<!-- Scans within the base package of the application for #Components to configure as beans -->
<context:component-scan base-package="com.test.jd" />
</beans>
I can't figure out what seems to be the problem(I think the code is not the problem, I might be wrong). any ideas?
Sorry forgot to mention this(big one) :
Here is the exception -> http://pastebin.com/m1aQFmbj
Fixed it **
#Resource(mappedName="userService", name="userService") in addition to name added, mappedName.
Now I've got another one :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined: not found in JNDI environment
I've tried adding <bean id="userService" name="userService" class="com.test.jd.service.impl.UserServiceImpl"/>
to both controllers and root-context and get this same error.
any clues what to do next(I'm wondering why do I need JNDI)?
JBoss is trying to inject a resource (probably an EJB?) into a servlet or filter. The annotation has a name field with the value 'userService'. JBoss has been unable to find an appropriate resource to inject and is asking for you to specify where the resource exists in JNDI via the 'mappedName' attribute.
This can happen if the name on your EJB and the name of the resource injection don't match. For example the following won't work:
On the EJB: #Stateless(name="foo")
In the Servlet: #EJB(name="bar") private MyBean myBean
Both name values must be the same.
More information:
http://download.oracle.com/javaee/5/api/javax/annotation/Resource.html
http://java.sun.com/developer/technicalArticles/J2EE/injection/

Categories