Home > AI > Backend > SpringBoot >

AnnotationConfigApplicationContext

Take specific @Configuration to get @Bean

Example

@SpringBootApplication
public class Demo12Application {


	public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(GoodConfig.class);
        User u = context.getBean(User.class);
        System.out.println(u.getEmail());


		SpringApplication.run(Demo12Application.class, args);
	}

}



@Configuration
class GoodConfig {

    @Bean
    User user() {
        User u = new User();
        u.setEmail("test@gmail.com");
        return u;
    }
}

It can take classes annotated with @Configuration@Component

Leave a Reply