Bean annotation-based configuration

Here is an annotation to define a DAO of Bean:

import org.springframework.stereotype.Component;

@Component("bookDao")
public class BookDao {
}

It is equivalent to:

<bean id="bookDao" class="com.dao.bookDao" />

@Component Spring container can be identified, automatically convert the pojo container-managed Bean.
In addition @Component, Spring defines three functional annotation and @Component same, respectively Controller DAO, Service and Web carried annotation:

The @Repository : DAO implementation class is used to annotate
@Service : Service implementation class is used to annotate
@Controller : Controller used to annotate the implementation class

Although these features and the same @Component, but may identify the particular bean, conveniently recognize what effect the bean.

Scan annotation defines the Bean

Spring provides a context namespace, it provides a way to define application notes to scan Bean class package, the following code:

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

Spring will scan all the classes in this package, Bean and obtain information from the definition of annotations.

If you want to scan a particular class, not the whole package, you can use the resource-pattern property to filter out specific classes:

<context:component-scan base-package="com.library" 
resource-pattern="books/*.class" />

Here the set of resource-pattern books / *. Class, Spring com.library.books only scans the sub-classes in the package.

But you may need more demands, such as scanning only achieve a particular class and other classes.
This can be context: component-scan and the filter implemented subclasses:

<context:component-scan base-package="com.smart">
        <context:include-filter type="regex" expression="com\.smart\.anno.*" />
        <context:exclude-filter type="aspectj" expression="com.smart..*Controller+" />
</context:component-scan>

context: include-filter represented by the filter class to include.
context: exclude-filter indicates the target class to be excluded.

There may be a number of context: exclude-filter and context: exclude-filter. The two filter elements to support multiple types of filter expressions.
Supported types are as follows:

1, type = annotation, such as
org.springframework.stereotype.Component;
org.springframework.stereotype.Controller;
org.springframework.stereotype.Repository;
org.springframework.stereotype.Service;
will target a certain class is marked notes were filter.

2, type = assignable, as com.library.XxxService.
It means all inherited or extended XxxService class, whether the target class will inherit a particular class or expanded filtering

3, type = those classes aspectj, filter and scan the specified AspectJ expression matched.

4, type = regex, scanning the class name of the filter specified by the regular expression matching that class.

5, type = custom, the class must implement org.springframework.core.type.TypeFilter interface.

In all of these types of filters, in addition to the type of custom filtering aspectj the strongest expression.

context: component-scan as well as a use-default-filters property , the default is true, meaning four kinds of notes mentioned above are the default will be scanned. If you only need to scan @Controller of Bean, the property must be set to false.

Therefore, context: component-scan according to the scan lists need not exclude-filter, and then through the scan lists requires include-filter, and then whether to scan a particular annotation type according to the value of the use-default-filters.

These are the bean comments configuration and scanning annotation defines the Bean.

Guess you like

Origin blog.csdn.net/RebelHero/article/details/79681589