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

  • Need help with DI


    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:
    public class UserBean implements Serializable {
    	private UserService userService;
    UserService is a easy interface:
    public interface UserService {
    	public List<User> getUsers();
    	public User getUserByUsername(String username);
    The mock implementation is this here:
    public class UserServiceMockImpl implements UserService {
    private final List<User> dummyUsers = new ArrayList<User>();
    I added this into the web.xml:
    And created a application-context.xml which included this:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    	<context:component-scan base-package="" />
    The server.log told me that:
    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.s[email protected]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.s[email protected]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

    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.


    • #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:
      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.