Skip to main content

Posts

Showing posts with the label Spring Java configuration example

Spring Core - Java configuration example

In this post, we will create a spring context and will register bean via Java configuration file. You can see my previous post on how to create a simple spring core project. What is @Configuration annotation? @Configuration annotation indicates that there is one or more bean methods and spring containers can process to generate bean definitions at runtime. Also, @Bean annotation is used at method level to signifies that this will be registered as bean in spring context. Let's create a quick configuration class. @Configuration public class WelcomeGbConfig { @Bean GreetingService greetingService() { return new GreetingService(); } } Now, we will create spring context as follows. // using try with resources so that this context closes automatically try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( WelcomeGbConfig.class);) { GreetingService greetingService = context.getBean(GreetingService.class); greetingService.gre