What are different ways to configure a class as Spring Bean?

What are different ways to configure a class as Spring Bean?

There are three different ways to configure Spring Bean.
1 XML Configuration: This is the most popular configuration and we can use bean element in context file to configure a Spring Bean.
For example:
<bean name="myBean" class="com.test.spring.beans.MyBean"></bean>

2 Java Based Configuration: If you are using only annotations, you can configure a Spring bean using @Bean annotation. This annotation is used with @Configuration classes to configure a spring bean. Sample configuration is:
@Configuration
@ComponentScan(value="com.test.spring.main")
public class MyConfiguration {
@Bean
public MyService getService(){
return new MyService();
}}
3 Annotation Based Configuration: We can also use @Component, @Service, @Repository and @Controller annotations with classes to configure them to be as spring bean. For these, we would need to provide base package location to scan for these classes.
For example:
<context:component-scan base-package="com.test.spring" />

Read More →
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!