in Spring

Spring for dummies


Spring is awesome, it is easy to use and setup,but before we jump into big projects. Lets see few application contexts in action.

Possible choices are:

  1. ClassPathXmlApplicationContext, it allows us to load application context from file located in the class path.
  2. FileSystemXmlApplicationContext, it allows us to load application context from file using absolute path.
  3. AnnotationConfigApplicationContext,it allows us to load application context from class that it annotated with @Configuration.

For each example we are going to use same POJO named MajesticKnight that implements Knight interface.


XML configuration file

Configuration class equivalent to above xml configuration



In the next 3 examples I will show you how to create contexts , obtain knight bean from them and print the knights name.



output will be : Majestic Knight


Important note is that here we must add “file:” before the absolute path, here is the part of  spring documentation that relates to this note.

Plain paths will always be interpreted as relative to the current VM working directory, even if they start with a slash. (This is consistent with the semantics in a Servlet container.) Use an explicit “file:” prefix to enforce an absolute file path. 


Observe that KnightConfig class is used as parameter to AnnotationConfigApplicationContext.

Unit tests

We have seen how to create application context and how to use it, now we will see how to create a unit test and verify that all is correct and that name of the Knight instance is correct. First lets see how to use KnightConfig configuration class.

We can configure context configuration from xml as well.

As you have seen using spring is just awesome, easy and straightforward.


Write a Comment



  • Spring Bean Lifecycle –

    […] previous blog post I have shown you how to use ClassPathApplicationContext and run your examples. Lets repeat this […]