spring boot 注解极其知识点

 

1

@Value  从配置文件读取参数 例子  注解@Value("${name}")

@ConfigurationProperties 把yml里面一组配置参数封装成一个类 例子:

                @ConfigurationProperties(prefix = "girl")  prefix 映射的是yml里面的girl

@Component 向SpringBoot注册一个类  例子:

                                    private GirProperties girProperties; 报错的时候,引入@Component

@Autowired 注入一个类 例子:

                private GirProperties girProperties;

@RestController==@Controller +@ResponseBody  一般用@RestController就够了,这样不会涉及前端的。更好的前后的分离。

@RequestMapping(value={“/hello”,“/hi”} ,method=RequestMethod.GET) 同时可以写很多路径,把所有路径放到集合里面。

@RequestMapping(value={“/hello”,“/hi”} ,method=RequestMethod.POST) 和上面不同的事这里是POST

2

 yml配置文件的使用    yml配置文件,结构更清晰,语法更严谨   属性配置可以用.properties,也可以用.yml   例子:

server:
  port: 8080
girl:
  cupSize: B
  age: 18

多配置文件的使用  application-dev.yml  

开发环境配置文件   application-prod.yml

生产环境配置文件   application.yml

环境切换配置文件   spring:    

                                     profile:  

                                              active:dev

切换到开发环境,如果是prod切换到生产环境 或者是把工程编译打包之后,在命令行运行

以生产模式运行 java -jar GirlApplication-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod  

以开发模式运行: java -jar GirlApplication-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

Controller获取参数:

1:@RequestMapping(value = "/hello/{id}",      method = RequestMethod.GET)     

@PathVariable("id") Integer id,

          @RequestParam 获取请求参数的值     @PathVariable("id") Integer myId,http://localhost:8081/hello/say?id=100

获取url中的数据http:xx/hello/99

2:  @RequestParam,获取请求参数的值http:project/hh/name=XX           @RequestParam(value="id",required = false

(是否为必传),defaultValue = "0")

3:@GetMapping

组合注解 @GetMapping()       就是            @RequestMapping(value= ,method=RequestMethod.GET)

4:@PostMapping() 同理 

猜你喜欢

转载自blog.csdn.net/qq_40979622/article/details/82632267