Antoine Antoine - 1 year ago 71
Java Question

Spring doesn't instanciate bean at start

I'm stuck with a new problem. My application doesn't create my Services at startup and I end up with a nullPointerException when i try to use them in my Maven Tests after Inject them with @Autowired

I don't understand where it come from. I have done some research but i still don't understand why it doesn't work.

Here is the class where my Autowired "administrationActionService" is null :

public class AdministrationActionTests extends EntityTests {

AdministrationActionService administrationActionService;

public void equalsTests() {



The class :

public class AdministrationActionService extends ServiceEntity{

@Autowired private AdministrationActionDao administrationActionDao;


And my ApplicationContext.xml. I've placed it in src/ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""

<context:component-scan base-package=", com.bloombooking.dao"/>


I really don't know what I could have done wrong, if someone could help me ? Thanks

Answer Source

to make it work you have make the following changes
1. Add @RunWith(SpringJUnit4ClassRunner.class) to your class.
2. Add @ContextConfiguration("path_to_you_spring_beans.xml")

So your class becomes

AdminisrationActionTests {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download