How to initialize Application Context in Spring Framework 4

SergeZ picture SergeZ · Apr 5, 2014 · Viewed 23.1k times · Source

I have a project based on Spring Framework 4 and its subproject - Spring Data Solr.

All examples I had a chance to see explain how to organize your project - from base entity ( pojo's ) classes till spring specific classes such as repositories and service. When it came to test functionality all examples show a test with private field ( spring bean ) which usually becomes initialized with the help of annotation

@ContextConfiguration(classes = some-spring-data-main-class.class, loader = SpringApplicationContextLoader.class)

Then it is possible to call for it's bean's methods in @Test methods.

But, when it comes to init bean in a project - how to make it with Spring 4, which is completely XMLless ( I mean, I do not have an applicationContext xml file ).

P.S. in Spring 3 we usually wrote smth like:

ApplicationContext context = new ClasspathApplicationContext("applicationContext.xml") 

Is it reasonable to expect smth similar to this of Spring 4 introduces absolutely new concepts of application initialization? What should we write now to init app's first bean?

Answer

SergeZ picture SergeZ · Apr 5, 2014

I got it !

In Spring 4 now we have to write:

ApplicationContext context = new AnnotationConfigApplicationContext(<out-main-config-class>.class);

and then call for beans and its methods.