top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

What are different ways to configure a class as spring bean?

0 votes
8 views
posted Nov 12 by Frank Lee
Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

0 votes

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" />
answer 5 days ago by Jon Deck

Your answer

Preview

Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...