Autodetecting and Spring Components
•        In Spring 2.0, the @Repository annotation was introduced to signify a data access object (bean).
•        This annotation allows the bean to be registered as a “component” without as much configuration.
•        Consider the typical service to DAO component relationship as setup in a standard Spring
configuration file below.
<bean id="orderService" class="com.intertech.OrderServiceImpl">
<property name="orderDao">
<ref bean="orderDao"/>
</property>
</bean>

<bean id="orderDao" class="com.intertech.OrderDaoImpl">
</bean>
•        Now, the OrderDao bean can be simply marked as a DAO component using the @Repository
annotation.
@Repository
public class OrderDaoImpl implements OrderDao {
...
}
•        And the configuration file does not even have to carry the DAO component configuration.
•        In order to use the annotation, the Spring container must be informed to scan for and automatically
detect these types of components.  
•        Therefore, the configuration file must include a “component-scan” element as shown below.
<bean id="orderService" class="com.intertech.OrderServiceImpl">
</bean>

<context:component-scan base-package="com.intertech">
</context:component-scan>

•        The @Repository is just one of several component, and also known as “stereotype,” annotations now
available.
•        In Spring 2.5, several other stereotype annotations were added to mark components in each layer of the
application.
•        The @Service and @Controller annotations allow service and controller beans to be denoted in a similar
fashion.
•        There is also a @Component generic marker (it is actually the parent annotation to all the stereotype
annotations).
•        This annotation allows any bean to be marked as a component.
•        These stereotype annotations help to clearly identify the components serving in each role of the
application.
•        The Spring framework/container can then use these annotations to provide additional behavior to these
components (now and in the future).
•        For example, these components now make themselves better known for processing by tools or
associating them with aspects.
•        Autodected stereotype components are singletons by default.  However, their scope can be explicitly
set with the @Scope annotation.
@Scope(“prototype”)
@Repository
public class OrderDaoImpl implements OrderDao {
...
}
•        A name can explicitly be provided to each component.
@Repository(“orderDao1”)
public class OrderDaoImpl implements OrderDao {
...
}
•        In fact, each component is given a name by default.
•        Lower camel casing of the class name is used as the default name of the component.
•        Without the name provided above, the repository’s name would have been “orderDaoImpl” in this
example.
•        By default, all components marked with the @Repository, @Service, @Controller and @Component
annotations are detected in the scan.
•        However, the component-scan element in the configuration file offers several filters to include or
exclude classes in the scan.
•        In the example below, a regular expression include filter allows all classes that end in “Dao” to be
included in the scan for components.
•        Likewise, any component marked with a “@Service” annotation are excluded from the scan.
<context:component-scan base-package="com.intertech">
<context:include-filter type="regex" expression=".*Dao"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
•        From the Spring 2.5 documentation, below is a list of the Include and exclude filter types (and
associated expressions to use them).
Filter Type        Example Expression        Description
annotation        org.example.SomeAnnotation        An annotation to be present at the type level in target
components.
assignable        org.example.SomeClass        A class (or interface) that the target components are assignable to
(extend/implement).
aspectj        org.example..*Service+        An AspectJ type expression to be matched by the target components.
regex        org\.example\.Default.*        A regex expression to be matched by the target components' class
names.
custom        org.example.MyCustomTypeFilter        A custom implementation of the org.springframework.
core.type.TypeFilter interface.
JDK
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials