SPRINGMVC Four annotations Component Controller Service Repository

Source: Internet
Author: User

How to use component\controller\service\repository four annotation classes: • @Component is a universal annotation • @Controller Callout Web Controller • @Service Callout SERVICEC Layer Services • @Respository marking the DAO layer's data access
• These annotations are class-level, with no parameters, or with an argument that represents the bean name and can be injected by name when injected.
• You can use the component when you don't know the annotation.
• For clarity of code logic, it is advisable to use specific annotations • In the SPRINGMVC configuration file, the annotation takes effect through Context:component-scan
• Inject with @resource or @autowired annotations
• @Autowired default by type, you must require dependent objects to 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 name assembly, we can use it in conjunction with @qualifier annotations.

• @Resource, the default installation name is assembled, and the name can be specified by the names property, if the Name property is not specified, and when the note is written on a field, the name of the field is installed by default, and if the annotation is written on the setter method, the attribute name is assembled by default. Assembly is done according to type when no bean matching the name is found. It should be noted, however, that if the Name property is specified, it will only be assembled by name.

注解示例GitHub(下载demo2):https://github.com/DaleyChao/SpringMVCPra/tree/7093e65517c2a504d4172427d4c71fcf18b869f3

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.