Spring – Auto Scanning Components
Normally you need to declare all the beans or components in the bean configuration file, so that the Spring Ioc container can detect and manage your beans or components. In Spring 2.5, it’s able to auto scan, detect and instantiate your beans or components with certain annotations from project class path. It can save you a lot of work in the beans configuration.
Here’s a simple Spring project, including a customer service, dao and print a String message. Let’s explore what’s the different between manual components declaration and auto components scanning.
Manual Components Declaration
CustomerDAO class
package com.mkyong.customer.dao; public class CustomerDAO { @Override public String toString() { return "Hello , This is CustomerDAO"; } }
Customer service class, call CustomerDAO
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 classic 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]
Auto Components Scanning
Here’s how auto components scanning work, see the different.
CustomerDAO class, add @Component to indicate this 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"; } }
Customer service class, call CustomerDAO. Add @Component to indicate this is an auto scan component, @Autowired to auto wire the customer DAO by type.
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 + "]"; } }
Enable the auto scanning feature in Spring (Spring-AutoScan.xml)
P.S The base-package is indicate where’s your components stored? Spring will scan this folder and find out the components you want to include 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 declared in Spring.
Custom 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");
For some cases, you may need a custom name for your component. You can declared a custom name ‘AAA’ for the ‘CustomerService’ class.
@Service("AAA") public class CustomerService ...
Retrieve it with custom 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? 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 see all @Repository,@Service or @Controller are belong to @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 but not a good practice, you should declare the @Repository,@Service or @Controller for a specified layer to make your code more easier to understand as following :
Customer DAO class
package com.mkyong.customer.dao; import org.springframework.stereotype.Repository; @Repository public class CustomerDAO { @Override public String toString() { return "Hello , This is CustomerDAO"; } }
Customer services class
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 + "]"; } }
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
[...] /> </beans> Note You may interest at this Spring auto scanning components [...]
[...] Auto Scanning Components How to automatic scan, detect and instantiate your beans or components from project class path. [...]
hmmmm nice great post….
[...] Components in Auto Scanning Written on March 25, 2010 at 5:25 am by mkyong In last tutorial, you learn about how to enable the auto scan components with @Component annotation. The alternative [...]