<? 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-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd>
<Context: component-scan base-package = "*"/>
</Beans>
<Context: component-Scan/> specifies the class package to be scanned. All classes in the class package and Its Recursive Sub-package are processed.
<Context: component-Scan/> also allows you to define filters to include or exclude certain classes in the base package. Spring supports the following four types of filtering methods:
Filter Type |
Description |
Note |
If com. baobaotao. someannotation is an annotation class, we can filter out the classes that use this annotation. |
Class Name |
Filter by fully qualified class names. For example, you can specify com. baobaotao. Boss for scanning, and exclude com. baobaotao. Car. |
Regular Expression |
Use regular expressions to define filtered classes, as shown below: COM/. baobaotao/. Default .* |
Aspectj expression |
Use aspectj expressions to define filtered classes, as shown below: COM. baobaotao .. * Service + |
The following is a simple example:
<context:component-scan base-package="com.baobaotao"> <context:include-filter type="regex" expression="com/.baobaotao/.service/..*"/> <context:exclude-filter type="aspectj" expression="com.baobaotao.util..*"/> </context:component-scan> |
It is worth noting that the <context: component-Scan/> configuration item not only enables scanning of class packages to implement the annotation-driven bean definition function, the annotation driver automatic injection function is also enabled (that is, it is also implicitly registered internallyAutowiredAnnotationBeanPostProcessor
AndCommonAnnotationBeanPostProcessor
), So when <context: component-Scan/> is used, you can remove <context: annotation-config/>.
By default@Component
The defined beans are all singleton. If you want to use beans of other scopes, you can use@Scope
Note to achieve the goal, as shown in the following code:
@Scope("prototype") @Component("boss") public class Boss { … } |
In this wayboss
Every time the bean is returned, it is a new instance.
In addition to providing@Component
In addition to annotations, several annotations with special semantics are also defined. They are:@Repository
,@Service
And@Controller
. In the current spring version, the three annotations and@Component
Is equivalent, but from the annotation class name, it is easy to see the three annotations respectively
Corresponds to the persistence layer, business layer, and control layer (web layer. Although the three annotations and@Component
Compared with nothing new,
Spring will add special features for them in later versions. Therefore, if a web application uses a classic layer-3 hierarchy,
It is best to use@Repository
,@Service
And@Controller
Comment out classes in the hierarchy,
Use@Component
Annotate the neutral classes.
With these IOC annotations, can we completely remove the original xml configuration method? The answer is no. There are several reasons:
- The annotation configuration is not necessarily superior to the xml configuration in the previous days. If the bean dependency is fixed (such as which Dao classes are used by the Service), the configuration information will not be adjusted during deployment, so the annotation configuration is better than the xml configuration; if the dependency is adjusted during deployment, the xml configuration is obviously better than the annotation configuration because the annotation is an adjustment to the Java source code, you must rewrite the source code and re-compile the code to make adjustments.
- If the bean is not a self-compiled class (for example
JdbcTemplate
,SessionFactoryBean
.
- Annotation configuration is usually similar, while xml configuration can be more flexible. For example
@Transaction
Transaction annotation. The transaction configuration using the AOP/TX namespace is more flexible and simple.
Therefore, in the implementation of applications, we often need to use both annotation configuration and xml configuration. For Class-level configurations that do not change, annotations configuration should be given priority; xml configuration should be preferred for third-party classes and configuration that is prone to adjustment. Spring will combine the metadata of these two configuration methods before bean creation and bean injection are implemented.
Other materials:
Http://zfsn.javaeye.com/blog/506693
Http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/