- Mastering Spring 5.0
- Ranga Rao Karanam
- 146字
- 2021-07-02 22:12:04
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.