Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of ClassPathXmlApplicationContext in Standalone Java class

Tags:

java

spring

I am not exposed to Spring as yet. I saw the below code in one of the standalone java projects that I have in my system. Can you please help me understand the below code.I am unable to see spring.xml in the project - is it something that must be there and is missing?

    appContext = new ClassPathXmlApplicationContext(new String[] {
        "classpath*:/META-INF/spring.xml",
        "classpath*:myapplication-application-context.xml"
        });
like image 766
Punter Vicky Avatar asked Jan 05 '12 08:01

Punter Vicky


2 Answers

The classpath* syntax means that Spring will search the classpath for all resources called /META-INF/spring.xml and myapplication-application-context.xml, and will amalgamate them into the context. This includes looking through JAR files inside the project, so there may not be any visible within your main project files.

like image 94
skaffman Avatar answered Oct 18 '22 08:10

skaffman


The core functionality of Spring revolves around the ApplicationContext which is the "Central interface to provide configuration for an application. " This interface is implemented by the ClassPathXmlApplicationContext which helps you take the context definitins from your classpath .Hence you specify classpath* .

As @skaffman explains , your application get loaded from the context definitions in the above mentioned files . i.e, all the Spring beans are initialized and Dependency Injection is performed as required .

If you deal with web applications , Spring has got a corresponding web application context loaded by XmlWebApplicationContext

like image 22
Aravind A Avatar answered Oct 18 '22 10:10

Aravind A