Launching an application context with the XML configuration

The following program shows you how to launch an application context using the XML configuration. We use the main method to launch the application context using ClassPathXmlApplicationContext:

    public class LaunchXmlContext { 
private static final User DUMMY_USER = new User("dummy");
public static Logger logger =
Logger.getLogger(LaunchJavaContext.class);
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext(
"BusinessApplicationContext.xml");
BusinessService service =
context.getBean(BusinessService.class);
logger.debug(service.calculateSum(DUMMY_USER));
}
}

The following lines of code create the application context. We want to create an application context based on the XML configuration. So, we use ClassPathXmlApplicationContext to create an application context, AnnotationConfigApplicationContext:

    ApplicationContext context = new 
ClassPathXmlApplicationContext (SpringContext.class);

Once the context is launched, we will need to get a reference to the business service bean. This is very similar to what we did with the Java configuration. We use the getBean method, passing the type of the bean (BusinessService.class) as an argument.

We can go ahead and run the LaunchXmlContext class. You will notice that we get output very similar to that we get when run the context with the Java configuration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset