Launching the application context with Java configuration

The following program shows how to launch a Java context; we use the main method to launch the application context using AnnotationConfigApplicationContext:

    public class LaunchJavaContext { 
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
AnnotationConfigApplicationContext(
SpringContext.class);
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 Java configuration. So, we use AnnotationConfigApplicationContext:

    ApplicationContext context = new 
AnnotationConfigApplicationContext(
SpringContext.class);

Once the context is launched, we will need to get the business service bean. We use the getBean method that passes the type of the bean (BusinessService.class) as an argument:

    BusinessService service = context.getBean(BusinessService.class );

We are all set to launch the application context by running the LaunchJavaContext program.

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

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