Java Reference
In-Depth Information
The
@Component
annotation is the basic stereotype for denoting components of general purposes.
Actually, there are other specific stereotypes denoting components in different layers. First, the
@Repository
stereotype denotes a DAO component in the persistence layer.
package com.apress.springenterpriserecipes.sequence;
import org.springframework.stereotype.Repository;
@Repository
public class SequenceDaoImpl implements SequenceDao {
...
}
Then the
@Service
stereotype denotes a service component in the service layer.
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SequenceService {
@Autowired
private SequenceDao sequenceDao;
...
}
There's another component stereotype,
@Controller
, which denotes a controller component in the
presentation layer.
Filtering Components to Scan
By default, Spring will detect all classes annotated with
@Component
,
@Repository
,
@Service
,
@Controller
,
or your custom annotation type that is itself annotated with
@Component
. You can customize the scan by
applying one or more include/exclude filters.
Spring supports four types of filter expressions. The
annotation
and
assignable
types are for you
to specify an annotation type and a class/interface for filtering. The
regex
and
aspectj
types allow you to
specify a regular expression and an AspectJ pointcut expression for matching the classes. You can also
disable the default filters with the
use-default-filters
attribute.
For example, the following component scan includes all classes whose name contains the word
Dao
or
Service
, and excludes the classes with the
@Controller
annotation:
<beans ...>
<context:component-scan base-package=
➥
"com.apress.springenterpriserecipes.sequence">
<context:include-filter type="
regex"
expression="com\.apress\.springenterpriserecipes\.sequence\..*Dao.*" />
<context:include-filter type="
regex"
expression="com\.apress\.springenterpriserecipes\.sequence\..
➥
*Service.*" />
Search WWH ::
Custom Search