Description of Spring IoC annotation (excerpted online)

Source: Internet
Author: User
<? 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 internallyAutowiredAnnotationBeanPostProcessorAndCommonAnnotationBeanPostProcessor), So when <context: component-Scan/> is used, you can remove <context: annotation-config/>.

By default@ComponentThe defined beans are all singleton. If you want to use beans of other scopes, you can use@ScopeNote to achieve the goal, as shown in the following code:

@Scope("prototype")
@Component("boss")
public class Boss {

}

In this waybossEvery time the bean is returned, it is a new instance.

 

In addition to providing@ComponentIn addition to annotations, several annotations with special semantics are also defined. They are:@Repository,@ServiceAnd@Controller. In the current spring version, the three annotations and@ComponentIs 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@ComponentCompared 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,@ServiceAnd@ControllerComment out classes in the hierarchy,

Use@ComponentAnnotate 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 exampleJdbcTemplate,SessionFactoryBean.
  • Annotation configuration is usually similar, while xml configuration can be more flexible. For example@TransactionTransaction 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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.