Hello,
I try to use the dependency injection of spring but only get a NullPointerException. I had never worked before with spring, have nobody to ask here, tryed a lot for the last 5 hours and really need help.
I want to do is really simple. I have got a JSF page where I want to show a list of users. I made a service api (a project which now only has one interface in it) and a mock implementation which gives me list of dummy users. A database connectivity will be implemented later.
Here is the code:
UserBean in the webproject where the userservice will be injected:
UserService is a easy interface:Code:@ManagedBean @SessionScoped public class UserBean implements Serializable { @Inject private UserService userService; ... getUsers() getUserById() deleteUser() ...
The mock implementation is this here:Code:public interface UserService { public List<User> getUsers(); public User getUserByUsername(String username); ...
I added this into the web.xml:Code:@Component public class UserServiceMockImpl implements UserService { private final List<User> dummyUsers = new ArrayList<User>(); ...
And created a application-context.xml which included this:Code:<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/application-context.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>
The server.log told me that:Code:<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="path.to.service" /> </beans>
I really tryed a lot. The way via XML, different Annotations, ...........Code:INFO: PWC1412: WebModule[null] ServletContext.log():Closing Spring root WebApplicationContext INFO: Closing Root WebApplicationContext: startup date [Fri Jan 20 11:07:43 CET 2012]; root of context hierarchy INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e2f0803: defining beans [userServiceMockImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy INFO: PWC1412: WebModule[null] ServletContext.log():No Spring WebApplicationInitializer types detected on classpath INFO: Mojarra 2.1.3 (FCS b02) f�r Kontext '/MyApp' wird initialisiert. INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. INFO: Monitoring jndi:/server/MyApp/WEB-INF/faces-config.xml for modifications INFO: PWC1412: WebModule[null] ServletContext.log():Initializing Spring root WebApplicationContext INFO: Root WebApplicationContext: initialization started INFO: Refreshing Root WebApplicationContext: startup date [Fri Jan 20 12:00:16 CET 2012]; root of context hierarchy INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/application-context.xml] INFO: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@38205c30: defining beans [userServiceMockImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy INFO: Root WebApplicationContext: initialization completed in 734 ms INFO: WEB0671: Loading application [MyApp] at [/MyApp] INFO: MyApp was successfully deployed in 1.476 milliseconds.
What made I wrong?


Reply With Quote