Spring Learning Note Four: Spring Common annotations Summary

Source: Internet
Author: User

With spring annotations, you need to configure the component scanner in the configuration file to scan the annotations in the specified package

<base-package= "xxx.xxx.xxx.xxx"/>

1. Define Bean @Component

You need to use annotation @component on the class, and the VLAN attribute of the annotation is used to specify the value of the ID of the annotation.

Spring also provides three functional basic and @component equivalent annotations

@Repository used to annotate DAO implementation classes

@Service used to annotate Service implementation classes

@Controller used to annotate a Controller implementation class

[Email protected] scope @scope

You need to use the annotations @scope,vlan property on the class to specify the scope, which defaults to Singleton

3. Basic type Attribute Injection @value

You need to use host @value on the property, and the Value property of the annotation is used to specify the values to be injected

The non-setter method in the class when the attribute injection is completed using the modified annotation

4. Inject domain properties by type @autowired

You need to use annotation @autowired on domain properties, which by default uses automatic assembly of beans by type

When using this annotation to complete property injection, no setter method is required

5. Inject domain attributes by name @autowired and @qualifier

You need to use annotations @autowired and @qualifier on the domain properties. The @qualifier VLAN attribute is used to specify the ID value to match, and the object setter method

6. Domain attribute Annotations @resource

Spring provides support for defining @resource standard annotations in the JSR-250 specification.

@Resource annotations and the beans can be matched by name, or by type. By default, matches by type and then by name. If the value of the VLAN attribute is configured, only match by name

The life of the 7.Bean @postconstruct domain @predestroy

Spring Learning Note Four: Spring Common annotations Summary

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.