Spring annotation 02

Source: Internet
Author: User

Original: http://www.cnblogs.com/digdeep/p/4525567.html

Spring's bean container-related annotations:1) @Autowired is the most used annotation, in fact it is autowire=Bytype is based on the type of automatic injection dependency (annotation-based dependency injection), which can be used on domain, method, and constructor functions.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: @Controller @requestmapping ("/USER") Public classHellocontroller {@Autowired @Qualifier ("UserService")    PrivateUserService UserService;3@Resource belong to the JSR250 standard for attribute domain amounts and methods. is also a byname type of dependency injection. Usage: @Resource (name= "Xxbean"). @Resource default class name with no parameters The first letter is lowercase. Equivalent to: @Autowired @Qualifier ("Xxbean")4) JSR-330 Standard javax.inject.*notes in (@Inject, @Named, @Qualifier, @Provider, @Scope, @Singleton). @Inject is equivalent to @autowired, @Named is equivalent to @Qualifier, and the other @ Named also has @Component function on the class.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 used to produce beans, These annotations are annotations on the class, annotating the class into a spring bean factory in one of the [email protected], @Service, @Repository is basically a more granular @component 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)4In spring, the processing of annotations in spring is basically done by implementing interface Beanpostprocessor: Public InterfaceBeanpostprocessor {Object Postprocessbeforeinitialization (object bean, String beanname)throwsbeansexception; Object Postprocessafterinitialization (Object bean, String beanname)throwsbeansexception;} The related processing classes are: Autowiredannotationbeanpostprocessor, Commonannotationbeanpostprocessor, Persistenceannotationbeanpostprocessor, requiredannotationbeanpostprocessor These processing classes, can be<context:annotation-config/>Configure the implicit configuration into the spring container. These are the processing of dependency injections, as well as the processing 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<context:annotation-config/> can be omitted. In addition context:component-Scan also scans for @aspect-style AOP annotations, but it needs to be included in the configuration file<aop:aspectj-autoproxy/> to cooperate.

Spring annotation 02

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.