Different ways to provide spring configuration metadata.

There are three way to provide spring configuration metadata inside a Spring Container.

XML based configuration :-

  • We can specify configuration metadata data in an XML file.
  • This will be declare inside spring-config.xml file.
				
					<beans>
<bean id="test" class="com.Employee">
<property name="name" value="javatechnote"/>
</bean>
</beans>
				
			

Annotation-based configuration :-

  • This type of configuration metadata we are use Annotation.
  • Here we can configure the bean into component class by using annotation on the relevant class, method or field declaration.
  • This will be declare inside spring-config.xml file.
  • To use annotation we need to <context:annotation-config/> enable inside spring configuration file.
				
					<beans> 
<context:annotation-config/> 
<!-- bean definitions describe here --> 
</beans>
				
			

Java-based configuration :-

  • We are use annotations like @Configuration, @Import, @Bean in Java code to specify configuration metadata inside Spring Container.
  • @Import allows developers to import one or more @Configuration classes into another.
  • @Bean annotation plays the same role as the <bean/> element. we can use this method to register a bean definition within an ApplicationContext of the type specified as the method’s return value.
  • @Configuration classes allow define inter-bean dependencies by simply calling other @Bean methods inside the same class.
				
					@Configuration
public class EmployeeConfig {
	@Bean
	public Employee testEmployee() {
		return new Employee();
	}
}
				
			
				
					@Configuration
@Import({DatabaseConfig.class, ServiceConfig.class})
public class EmployeeAppConfig {
    // employee main configuration code here
}
				
			

In above program when EmployeeAppConfig is processed, Spring will also execute DatabaseConfig, ServiceConfig class.