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