Java Interview--spring Technical Essentials--spring annotations

Source: Internet
Author: User

20 Java-based spring annotation configuration (for some examples of annotations)

Java-based configuration allows you to do most of your spring configuration, rather than through an XML file, with the help of a small number of Java annotations.

As an example of @configuration annotations, it is used to mark a class as a bean's definition and be used by a SPRINGIOC container. Another example is the @bean annotation, which indicates that this method will return an object that is registered as a bean into the spring application context. (generally seldom used Java code to configure meta data)

Spring Annotation-based container configuration, and open configuration

In contrast to XML files, annotation-type configuration relies on the assembly of bytecode metadata rather than the declaration of angle brackets. Instead of using XML to describe the assembly relationship of a bean, a developer is configured in a direct component class by using annotations on the corresponding class, method, or property.

Annotation assembly is not turned on by default, in order to use annotation assembly, we must configure the <context:annotation-config/> element in the spring configuration file. (<context:annotation-config/> tab can turn on automatic scanning and also open annotation assembly )

Introduction to Spring annotations

    • @Required: This note indicates that the Bean attribute must be set at configuration time, either through an explicit attribute value defined by a bean or by automatic assembly, if the Bean property of the @required annotation is not set, The container will throw beaninitializationexception.
    • @Autowired: Annotations provide finer granularity of control, including where and how automated assembly is done. Its usage, like @required, modifies a setter method, a constructor, a property, or a PN method with any name and/or multiple parameters.
    • @Qualifier: When there are multiple beans of the same type but only one needs to be automatically assembled, use @qualifier annotations and @autowire annotations in conjunction to eliminate this confusion and specify the exact beans that need to be assembled.

the difference between @Autowired and @resource

    • Both @Autowired and @resource can be used to assemble beans, either written on a field or on a method.
    • The @Autowired belongs to spring, and the @Resource is the annotation of the JSR-250 standard, which belongs to the EE.
    • @Autowired is assembled by type by default, you must require that the dependent object must exist by default, and if you want to allow null values, you can set its Required property to False, for example: @Autowired (Required=false), If we want to use the name assembly can be used in conjunction with @qualifier annotations, as follows:

@Autowired ()

@Qualifier ("Basedao")

Private Basedao Basedao;

    • @Resource, the default installation name is assembled, the name can be specified by the Name property, and if the Name property is not specified, the default name lookup is used when the annotation is written on the field, and if the annotation is written on the setter method, the property name is assembled by default. When a bean matching the name cannot be found, it is assembled by type. However, it is important to note that if the Name property is specified once, it will only be assembled by name. For example:

@Resource (name= "Basedao")

Private Basedao Basedao;

Recommended: @Resource annotation on the field, so that the setter method is not written, and this annotation belongs to the Java EE, reducing the coupling with spring. This makes the code look more elegant.

Java Interview--spring Technical Essentials--spring annotations

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.