Home  >  Article  >  Java  >  How to create springboot test class annotations

How to create springboot test class annotations

王林
王林forward
2023-05-10 19:22:111341browse

    Create a TextHello class

    How to create springboot test class annotations

    How to create springboot test class annotations

    The code for the TextHello class is as follows

     @Controller
    @RequestMapping("/hello")
    public class TextHello {
    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
    return "hello,程程呀";
    }
    }

    I added the dependency in the pom.xml file as follows

    
    org.springframework.boot
    spring-boot-starter
    

    The result in the browser is as follows:

    How to create springboot test class annotations

    ##Automatic configuration: pom.xml spring-boot-dependencies: The core dependencies are in the parent project! We do not need to specify the version number when writing or introducing springboot dependencies because there are these version warehouse starters

    
    org.springframework.boot
    spring-boot-starter
    

    starter: it is the startup scenario of springboot, such as spring-boot-starter-web, he It will help us automatically import all dependencies in the web environment. And springboot will turn all scenarios into starters

    Annotations

    @SpringBootApplication: Contains @configuration, @ComponentScan, @EnableAutoConfiguration usually on the main class
    @SpringBootConfiguration : springboot configuration
    @Configuration : spring configuration class
    @Component : Description this is also a spring component
    @EnableAutoConfiguration : Automatic configuration
    @AutoConfigurationPackage : Automatic configuration package
    @ResponseBody: Return The information is data in json format. Generally speaking, the @RestRestController annotation will be used directly.
    @CrossOrigin: Solve cross-domain issues. If no special processing is done, the general @CrossOrigin will be added to the control layer class. Unless a gateway is introduced, there is no need to use annotations to resolve cross-domain issues.
    @MapperScan: In the past, @Mapper was used to define mappers one by one. With @MapperScan, you only need to specify the mapper package, and there is no need to use @Mapper annotations one by one.
    @EnableScheduling: Enable scheduled tasks, used on classes.
    @Value: Dynamically inject the value of the external configuration file. .
    @Transactional: Declarative transaction annotation.
    @Data: After using the annotations under lombok, there is no need to write the getter and setter methods of the entity class.
    @RestController: used to annotate control layer components, including @Controller and @ResponseBody.
    All automatic configuration of springboot is scanned and loaded at startup: spring.factories All automatic configuration classes are in it, but they may not take effect. To judge the corresponding start, there is Only when you have the corresponding launcher, the automatic assembly will take effect, and then the configuration will be successful.

    Main startup class

    @SpringBootApplication: Mark this class as a springboot application: all resources under the startup class are imported springboot understanding: automatic assembly, run() fully takes over the springMVC configuration!

    How to create springboot test class annotations

    Static: store static resources, such as: css, js, pictures Templates: template file application.porperties: springboot configuration file

    Configuration file format

    properties file: key-value format

    How to create springboot test class annotations

    yml file: ladder-shaped properties file

    How to create springboot test class annotations

    Difference

    • #.yml format does not support @PropertySource annotation import configuration.

    • When there are the same attributes in the properties file and the yml file, the properties file has a higher priority than the yml file

    • Look at the picture above: The formats of the two configuration files are different. When they are configured identically, the properties have priority over the yml file. However, it is recommended to use the yml file because the structure of this file is clearer.

    The above is the detailed content of How to create springboot test class annotations. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete