spring annotation spare

1、@Value

       Read parameters from the current configuration file in the format

    @Value("${TypeName}")
    private Type typeName;

      Type conversion will be performed automatically.

2、@Component

      Only classes with this annotation can be used by @Autowired.

Table of contents

1、@Value

2、@Component

3、@ConfigurationProperties

4. Use annotations to construct IoC containers


      Reads an entire collection type from the current configuration file. For example, there is a collection configured in the current configuration file as

   dog:
     age: 10
     type: "哈士奇"
     favorite: "拆家"

      Then write a class such as Dog. Note that get and set methods must be added to all attributes.

@Component
@ConfigurationProperties(prefix = "dog")
public class Dog {
    private int age;
    private String type;
    private String favorite;

      Configure the @ConfigurationProperties annotation for the class, the parameter prefix is ​​the prefix of the collection in the configuration file, and then add the @Component annotation to it, you can introduce it in other classes, as follows

@Autowired 
private Dog dog;

By the way, the annotation of the control class in springboot is @RestController, which is equivalent to @Controller plus @ResponseBody.

      The method annotation in the control class is @RequestMapping, the format is as follows, multiple request names are separated by commas

@RequestMapping(value = {"/dog"} , method = RequestMethod.GET)

    There are also shorthand methods @GetRequestMapping and @PostRequestMapping, which are equivalent to omitting the following methods.

4. Use annotations to construct IoC containers

Use annotations to register beans with the Spring container . <context:component-scan base-package="pagkage1[,pagkage2,…,pagkageN]"/> needs to be registered in applicationContext.xml .

For example: specify a package in base-package

1 <context:component-scan base-package="cn.gacl.java"/>

Indicates that in the cn.gacl.java package and its sub-packages, if a class has a specific annotation [@Component/@Repository/@Service/@Controller] on the header, this object will be registered as a Bean in the Spring container. . You can also specify multiple packages in <context:component-scan base-package=” ”/>, such as:

1 <context:component-scan base-package="cn.gacl.dao.impl,cn.gacl.service.impl,cn.gacl.action"/>

Table of contents

1、@Value

2、@Component

4、@RestController 和 @RequestMapping


     Quote: https://blog.csdn.net/qq_32881383/article/details/77982227

 

Guess you like

Origin blog.csdn.net/qq_37511875/article/details/81514441