Automatically Discover Beans with Auto Component Scanning
You can define your spring beans manually or let spring automatically discover stereotyped classes and register corresponding bean definitions with the application context. Spring scans for special marker annotations, which allows spring to auto-detect components through class-path scanning. The following annotations are eligible for auto component scanning:
- @Component this indicates that an annotated class is a component.
- @Controller this indicates that an annotated class is a controller.
- @Service this indicates that an annotated class is a service.
- @Repository this indicates that an annotated class is a repository.
Enable Component for Component Scanning
To make our component eligible for component scanning, we simply annotate our class/POJO with one of the marker interfaces described above. When the class is annotated with a marker interface and the spring component scanning is configured to scan the right packages, spring automatically registers the corresponding bean definitions with the application context.
package com.memorynotfound.spring.core.component;
import org.springframework.stereotype.Component;
@Component
public class ComponentResolver {
public void resolve(){
System.out.println("resolving component");
}
}
Configure XML Component Scanning
The <context:component-scann/> element recursively checks for candidates inside the package provided by the base-package attribute. You can specify a comma/semicolon separated list given the packages spring needs to scan for components. When <context:component-scann/> is used, this implicitly enables the functionality of the <context:component-scan/>.
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.memorynotfound"/>
</beans>
Configure Java Config Component Scanning
The equivalent of the XML element is the @ComponentScan
annotation. When registered, spring will automatically detect beans recursively in the package specified by the basePackages attribute. You can specify a comma/semicolon separated list given the packages spring needs to scan for components.
package com.memorynotfound.spring.core.component;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.memorynotfound")
public class AppConfig {
}
Testing The Configuration
We can obtain a reference of the bean using the getBean()
method of the ApplicationContext
. Afterwards we simply invoke a method of the bean to show that all the wiring and component scanning is done automatically.
package com.memorynotfound.spring.core.component;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Run {
public static void main(String... args) {
ApplicationContext xmlContext = new ClassPathXmlApplicationContext("app-config.xml");
ComponentResolver componentResolver = xmlContext.getBean(ComponentResolver.class);
componentResolver.resolve();
}
}
Output
INFO: Loading XML bean definitions from class path resource [app-config.xml]
resolving component