Announcement Announcement Module
Collapse
No announcement yet.
Need help with DI Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with DI

    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:
    Code:
    @ManagedBean
    @SessionScoped
    public class UserBean implements Serializable {
    	@Inject
    	private UserService userService;
    
    ...
    getUsers()
    getUserById()
    deleteUser()
    ...
    UserService is a easy interface:
    Code:
    public interface UserService {
    	public List<User> getUsers();
    	public User getUserByUsername(String username);
    ...
    The mock implementation is this here:
    Code:
    @Component
    public class UserServiceMockImpl implements UserService {
    private final List<User> dummyUsers = new ArrayList<User>();
    ...
    I added this into the web.xml:
    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>
    And created a application-context.xml which included this:
    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>
    The server.log told me that:
    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.
    I really tryed a lot. The way via XML, different Annotations, ...........
    What made I wrong?

  • #2
    Hi,

    Try adding <context:annotation-config/> to your application context.

    Also in your UserBean class, add a getter and setter method for userService.

    Hope that does the trick for you.

    Comment


    • #3
      Thank you very very much for your answer. It unfortunately not help.

      I found the reason for the problem.

      The userServiceMockImpl was loaded. You see this in the server.log. Spring works correctly. The UserBean is instanciated by JSF. So Spring did not notice that the Bean will created and cannot inject the service. I fixed the problem with a Factory (Singleton) which includes this lines:
      Code:
      private UserServiceFactory() {
      		ctx = new ClassPathXmlApplicationContext("application-context.xml");
      	}
      
      	public static UserServiceFactory getInstance() {
      		return instance;
      	}
      
      	public UserService getUserService() {
      		return (UserService) ctx.getBean("userService");
      	}
      The UserServiceMockImpl must be declared as a Bean in the applicationContext.xml.

      Thanks to everyone who read this thread and tryed to help.

      Comment

      Working...
      X