How to Automatically Discover Spring Beans on the Classpath
by, 11-27-2011 at 10:38 PM (1619 Views)
Since it’s appearance in Java 5, annotations has helped to simplify the configuration of application through component scanning. This removes the need for XML bean tags in many cases. Classes annotated as component stereotypes including the following: @Service, @Controller, @Configuration, @Component, @Repository as well as the JSR-330 annotations, such as @Named are included in the auto-scan default filter. Once auto-detected, these components become Spring beans and are automatically placed at the root of the application context.
myComponentScan.xml – Enabling Component Scanning
Java Code:<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.acme.springexamples.config.componentscan"/> </beans>