Configuration metadata can be provided to Spring container in following ways:
- XML-Based configuration: In Spring Framework, the dependencies and the services needed by beans are specified in configuration files which are in XML format. These configuration files usually contain a lot of bean definitions and application-specific configuration options. They generally start with a bean tag. For example:
|
<bean id="studentbean" class="org.edureka.firstSpring.StudentBean">
<property name="name" value="Edureka"></property>
</bean> |
- Annotation-Based configuration: Instead of using XML to describe a bean wiring, you can configure the bean into the component class itself by using annotations on the relevant class, method, or field declaration. By default, annotation wiring is not turned on in the Spring container. So, you need to enable it in your Spring configuration file before using it. For example:
|
<beans>
<context:annotation-config/>
<!-- bean definitions go here -->
</beans> |
- Java-based configuration: The key features in Spring Framework’s new Java-configuration support are @Configuration annotated classes and @Bean annotated methods.
1. @Bean annotation plays the same role as the <bean/> element.
2.@Configuration classes allow define inter-bean dependencies by simply calling other @Bean methods in the same class.
For example:
|
@Configuration
public class StudentConfig
{
@Bean
public StudentBean myStudent()
{ return new StudentBean(); }
}
Hope this helps!
Enroll in Spring course online to learn more about it.
Thanks! |