Introduction to Spring annotations

Source: Internet
Author: User

An annotation-based (annotation-based) configuration is provided, and we can use annotations to complete the injection dependency.

1. Configuring with Annotations

We need to modify the header information of the spring configuration file and modify some of the red annotations as follows:

<context:annotation-config/>

<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /spring-context-2.5.xsd "> <context:annotation-config/></beans>
2. Spring annotation Type

Spring requires the display of the Java class (Context:component-scan method) that specifies which paths are searched. The appropriate Java class is then registered as a spring Bean (a class that is appropriately labeled by annotation).

The annotations in spring can be divided into two main categories:

1) Spring bean Container-related annotations, or bean factory-related annotations;

2) SPRINGMVC related annotations.

Spring's bean container-related annotations are: @Required, @Autowired, @PostConstruct, @PreDestory, and the JSR-330 standard that Spring3.0 started to support javax.inject.* Notes in (@Inject, @Named, @Qualifier, @Provider, @Scope, @Singleton).

SPRINGMVC related notes are: @Controller, @RequestMapping, @RequestParam, @ResponseBody and so on.

Spring's bean container-related annotations:

1) @Autowired is the most used annotation, in fact, is Autowire=bytype is based on the type of automatic injection dependency (annotation-based dependency injection), can be used again domain, method, constructor, the completion of automatic assembly work. Use the @Autowired to eliminate the set, get method.

2) @Qualifier is autowire=byname, @Autowired annotation to judge multiple bean types, you need to use @Qualifier ("Xxbean") to specify the ID of the dependent bean:

3) @Resource belong to the JSR250 standard for attribute domain amounts and methods. is also a dependency injection of the byname type. How to use: @Resource (name= "Xxbean"). The @Resource default class name with no parameters is the lowercase first letter.

4) annotations in JSR-330 standard javax.inject.* (@Inject, @Named, @Qualifier, @Provider, @Scope, @Singleton). @Inject is equivalent to @autowired, @Named is equivalent to @Qualifier, and @Named used on classes with @Component functions.

5) @Component, @Controller, @Service, @Repository, these annotations are different from the annotations above, and the annotations above are injected into the dependent bean, and these annotations are all production beans, which are annotations on the class , annotate the class as a bean in the Spring bean factory. @Controller, @Service, @Repository is basically a @component with more refined semantics.

6) @PostConstruct and @PreDestroy are not for dependency injection, but for the bean's life cycle. Similar to Init-method (Initializeingbean) Destory-method (Disposablebean)

3. Processing of annotations in spring

The processing of annotations in spring is basically done by implementing interface Beanpostprocessor:

Public interface Beanpostprocessor {
Object Postprocessbeforeinitialization (Object bean, String beanname) throws beansexception;
Object Postprocessafterinitialization (Object bean, String beanname) throws beansexception;
}

The related processing classes are: Autowiredannotationbeanpostprocessor,commonannotationbeanpostprocessor, Persistenceannotationbeanpostprocessor, Requiredannotationbeanpostprocessor

These processing classes can be configured implicitly in the <context:annotation-config/> configuration into the spring container. These are the processing of dependency injections, as well as the handling of production bean annotations (@Component, @Controller, @Service, @Repository):

<context:component-scan base-package= "Net.aazj.service,net.aazj.aop"/>

These are done by specifying the scan's base package path and scanning them into the spring bean container. Note Context:component-scan will also default to Autowiredannotationbeanpostprocessor,commonannotationbeanpostprocessor configuration in. So the <context:annotation-config/> can be omitted. In addition, Context:component-scan can also scan @aspect-style AOP annotations, but you need to include <aop:aspectj-autoproxy/> in the configuration file for mates.

Introduction to Spring annotations

Related Article

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.