@Configuration, Removing the xml configuration


Beans can be created in three ways
1) using xml configuration
2) using @Component
3) using @Bean

When using @Component, component should be scan in either xml or in configuration java class.

To remove xml file and create  bean from java class:

@Configuration is used in one java class
@ComponentScan is used to scan packages, when @Component is used to create bean other wise
not required this.

To declare the context with java class:
ApplicationContext context = new AnnotationConfigApplicationContext(javaConfig.class);

@Bean({"name1", "name2"})
public Student getStudent() {
return new Student();
}

Bean can be given any name. The default name is getStudent for this bean.

Comments

Popular posts from this blog