Spring Auto scanning components
Normally you declare all the beans or components in XML bean configuration file, so that Spring container can detect and register your beans or components. Actually, Spring is able to auto scan, detect and instantiate your beans from pre-defined project package, no more tedious beans declaration in in XML file.
Following is a simple Spring project, including a customer service and dao layer. Let’s explore the different between declare components manually and auto components scanning in Spring.
1. Declares Components Manually
See a normal way to declare a bean in Spring.
Normal bean.
package com.mkyong.customer.dao;
public class CustomerDAO
{
@Override
public String toString() {
return "Hello , This is CustomerDAO";
}
}
DAO layer.
package com.mkyong.customer.services;
import com.mkyong.customer.dao.CustomerDAO;
public class CustomerService
{
CustomerDAO customerDAO;
public void setCustomerDAO(CustomerDAO customerDAO) {
this.customerDAO = customerDAO;
}
@Override
public String toString() {
return "CustomerService [customerDAO=" + customerDAO + "]";
}
}
Bean configuration file (Spring-Customer.xml), a normal bean configuration in Spring.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerService" class="com.mkyong.customer.services.CustomerService">
<property name="customerDAO" ref="customerDAO" />
</bean>
<bean id="customerDAO" class="com.mkyong.customer.dao.CustomerDAO" />
</beans>
Run it
package com.mkyong.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.services.CustomerService;
public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
CustomerService cust = (CustomerService)context.getBean("customerService");
System.out.println(cust);
}
}
output
CustomerService [customerDAO=Hello , This is CustomerDAO]
2. Auto Components Scanning
Now, enable Spring auto component scanning features.
Annotate with @Component to indicate this is class is an auto scan component.
package com.mkyong.customer.dao;
import org.springframework.stereotype.Component;
@Component
public class CustomerDAO
{
@Override
public String toString() {
return "Hello , This is CustomerDAO";
}
}
DAO layer, add @Component to indicate this is an auto scan component also.
package com.mkyong.customer.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.mkyong.customer.dao.CustomerDAO;
@Component
public class CustomerService
{
@Autowired
CustomerDAO customerDAO;
@Override
public String toString() {
return "CustomerService [customerDAO=" + customerDAO + "]";
}
}
Put this “context:component
” in bean configuration file, it means, enable auto scanning feature in Spring. The base-package is indicate where are your components stored, Spring will scan this folder and find out the bean (annotated with @Component) and register it in Spring container.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.mkyong.customer" />
</beans>
Run it
package com.mkyong.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.services.CustomerService;
public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"});
CustomerService cust = (CustomerService)context.getBean("customerService");
System.out.println(cust);
}
}
output
CustomerService [customerDAO=Hello , This is CustomerDAO]
This is how auto components scanning works in Spring.
Custom auto scan component name
By default, Spring will lower case the first character of the component – from ‘CustomerService’ to ‘customerService’. And you can retrieve this component with name ‘customerService’.
CustomerService cust = (CustomerService)context.getBean("customerService");
To create a custom name for component, you can put custom name like this :
@Service("AAA")
public class CustomerService
...
Now, you can retrieve it with this name ‘AAA’.
CustomerService cust = (CustomerService)context.getBean("AAA");
Auto Components Scan Annotation Types
In Spring 2.5, there are 4 types of auto components scan annotation types
- @Component – Indicates a auto scan component.
- @Repository – Indicates DAO component in the persistence layer.
- @Service – Indicates a Service component in the business layer.
- @Controller – Indicates a controller component in the presentation layer.
So, which one to use? It’s really doesn’t matter. Let see the source code of @Repository
,@Service
or @Controller
.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
String value() default "";
}
You will noticed that all @Repository
,@Service
or @Controller
are annotated with @Component
. So, can we use just @Component for all the components for auto scanning? Yes, you can, and Spring will auto scan all your components with @Component annotated.
It’s working fine, but not a good practice, for readability, you should always declare @Repository,@Service or @Controller for a specified layer to make your code more easier to read, as following :
DAO layer
package com.mkyong.customer.dao;
import org.springframework.stereotype.Repository;
@Repository
public class CustomerDAO
{
@Override
public String toString() {
return "Hello , This is CustomerDAO";
}
}
Service layer
package com.mkyong.customer.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mkyong.customer.dao.CustomerDAO;
@Service
public class CustomerService
{
@Autowired
CustomerDAO customerDAO;
@Override
public String toString() {
return "CustomerService [customerDAO=" + customerDAO + "]";
}
}
Why do we still use CustomerService cust = (CustomerService)context.getBean(“customerService”); ? and not use @Autowired for CustomerService
Spring can inject dependencies to the fields marked with @Autowired only when the enclosing class itself resides as a bean in its container. SpringContext is simply unaware of instances not created by it and hence obviously cannot autowire the fields within those. In the above examples, spring is unaware of the ‘App’ class. So, to make @Autowired work for CustomerService field within App class, mark App class with @Component thereby informing spring to create a bean of the same and then obtain this bean object of App class through application context as done for CustomerService. This bean object will have CustomerService value set by Spring.
Great explanation!! Your posts are really very useful and easy to understand. Thank you so much for your efforts.
Great tutorial, really clear and step by step.
I might add that if anyone is using JDK 1.8 and is getting the error “component-scan and its parser class are only available on JDK 1.5 and higher” after enabling the component-scan you might need to switch to JDK 1.7 as spring-context-2.5 and JDK 1.8 are apparently not compatible anymore.
Which stereo type annotation used in web mvc module
NIce post, easy to understand.
Can you not use Autowired to get the bean for CustomerService?
you cleared the confusion
Fantastic Explanation!
Could you please create EAR project and explain spring mvc+hibernate using only with annotation please?
Create one ear project for spring mvc. jsp, controller, appinitializer alone in web module and call the dao from the ejb module or jar module.
Hi MKyong ,take a snickers ,you are not your self when you are hungry
Superb & easy to understand post. Thanks a ton Mr MKyoung for your contributions on your portal that helps thousands of Java/JEE developers across the globe to learn the technical stuff easily.
How is autowiring is different from component scanning.
I’m getting an error about the Java version needing to be 1.5 or greater (even though it is). I’m thinking the use of Spring 2.5 is the cause of this. I know the post is old, but maybe worth pointing that out.
You may use 1.8 JDK and that is why. Try to update your Spring to 4.0
Great article
help me a lot man! thanks!
Thanks a lot.. Good one..
Its a great post Sir, can i have one post for what is @Configurable, when should we use it? and its purpose? please mail me at [email protected]. please sir.
HI Mkyong, I am getting below error org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘SAController’: but when I add bean id in xml it is working any solution will help.
This example miss the annotation-config in spring context file without which you cannot run @Autowire implemnetation classes.
component-scan implies the effects of the ‘annotation-config’ tag,
good article!!!
“By default, Spring will lower case the first character of the component – from ‘CustomerService’ to ‘customerService’. And you can retrieve this component with name ‘customerService’.”
Is there a way to have spring automatically name the component with the value that is returned with CustomerService.class.getName() so you can then retrieve the component with
CustomerService cust = (CustomerService) context.getBean(CustomerService.class.getName());
no,that is not right.CustomerService.class.getName() get only get CustomerService not customerService.you can make the first character lower case like this.CustomerService cusomter =(CustomerService)context.getBean
(Introspector.decapitalize(CustomerService.class.getSimpleName()));
Hi,
I have a doubt regarding @Component
In one of the classes I have seen like
@Component(“CORE.TestClass”)
@Scope(“singleton”)
public class TestClass implements ITestHelper{
After running the application it will throw an error like:
Caused By: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘CORE.CoherenceCacheHelper’ defined in URL [zip:D:/system12.1.2.0.40.66.68/DefaultDomain/servers/DefaultServer/tmp/_WL_user/ADF-TEST/21n7qx/war/WEB-INF/lib/TestClass.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [TestClass]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:883)
Why is this error occurring ?
Nigel.
Hi Mkyong,
This is the most well explained article i’ve read in a while. Many thanks for sharing your expertise!
Best Regards.
Hello Sir,
Could please help me by providing example/s or link of example/s which usage
@Configuration
@Import
@ImportResource
Annotation at a time–> as these are also supported by Auto Component Scan. I want to understand their relation.
with following Annotaion-
@Component, @Service, @Repository, @Controller, @Endpoint
, @Bean, @Lazy, @Scope, @Order, @Primary, @Profile, @DependsOn
Very nice article, my doubt got clear.
Nice article. clear and to the point.
Hi Mkyong,
I have a doubt. Usually a spring mvc application will have dao, service, controller and form (component) which will all be treated as beans. So in the bean xml how do i declare these
The tutorial is just mind blowing. Very thanks to you MKYong
Nice post … explained the concept in such an easy way.
hello,
i saw this example. Very clear.
The problem I have is that I got the error in the file xml on the line with <context:component-scan…. The error says that it cannot find a definition for "<context…" because it cannot find the xsd definition file.
I use eclipse and spring 2.5 and I got this error for all other projects I have.
Thank you,
Adrian
Cause you haven’t mentioned xsd’s perfectly. I’m using spring 3.x and I got the same error. Rectified that error after putting this code. So google it about xsd s or copy paste mine and change the spring version.
Add this attribute to the beans tag in your configuration xml. Change 2.5 to 3.0 if you’re using that version.
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd