2

For some reason, in my config package, I can not autowire fields while in my controller package, there does not seem to be any problem.

For instance:

servlet-context.xml

<context:component-scan base-package="service, controller, config" />

root-context.xml (tried adding service here as well)

<context:component-scan base-package="security" />

This does not work: Setup class inside the config package. I receive a null pointer error.

@Component
public class Setup { //inside the config package
    @Autowired
    private UserService userService; //null pointer error
    /*..other stuff */
}

This does work: A controller inside the controller package:

@Controller
@RequestMapping(value="/contact")
public class ContactController { //inside the controller package
    @Autowired
    private UserService userService; //this works
    /*..other stuff..*/
}

Why does it work for the controller package but not config package?

Moody
  • 851
  • 2
  • 9
  • 23

2 Answers2

2

This happens because probably the @Service class UserService is not visible in any of the two packages "config" or "controller" I assume you would also need a component scan for the packages containing the services:

<context:component-scan base-package="service" />

Edit:

Usually you should remember that if a Bean/Component is not within the scanned context it will always be null on injection (autowiring).

EDIT 2:

So as you've probably seen Spring has two type of contexts, the ApplicationContext and the ServletContext. If you scan a bean in the ServletContext it will be accessible only from Controllers (or as the name states from the servlets), but beans scanned by the ApplicationContext are also accessible from Services or other Components and any servlet can access beans scanned here without having to scan them in the servlet context.

In your case you should have the following setup:

In servlet-context.xml:

<context:component-scan base-package="controller" />

In applicationContext.xml or root-context.xml (I assume you've referenced it as so in the web.xml)

<context:component-scan base-package="config, security, services" />

The image bellow illustrates the context hierarchy from Spring:

enter image description here

Bogdan Emil Mariesan
  • 5,529
  • 2
  • 33
  • 57
  • Hi, unfortunately, this still does not work. I have a feeling this may be caused because I have 2 xml's: root-context.xml and servlet-context.xml. I updated my post. – Moody May 14 '15 at 14:46
  • Thank you for your answer Bogdan. Unfortunately this does not work as well. The userService is still null inside the config.Setup class. If there is anything I can provide for more clarity please let me know. – Moody May 14 '15 at 18:10
  • @Moody write me a private message on linkedin or my blog and I'll help you fix it and then we will post the proper solution here – Bogdan Emil Mariesan May 14 '15 at 19:00
1

Try using this configuration instead:

<context:component-scan base-package="config,controller" />
Akash Rajbanshi
  • 1,553
  • 11
  • 23
  • you can find more details on annotation based bean configuration here: http://stackoverflow.com/questions/7414794/difference-between-contextannotation-config-vs-contextcomponent-scan – Akash Rajbanshi May 14 '15 at 02:32
  • Thanks for your comment, unfortunately it does not work. I have also tried to follow the post you gave me (although they used seperate setter methods and put the autowired above them), it still did not work. – Moody May 14 '15 at 11:16