SpringBoot 中针对同一类型的参数,可以用一个类来集中访问
来源:互联网 发布:哪个直播软件好 编辑:程序博客网 时间:2024/06/05 18:39
SpringBoot 中针对同一类型的参数,可以用一个类来集中访问,降低使用value(“{xxx.xxxx}”)在调用中的耦合.比如在AmazonProperties 类中收集参数,@ConfigurationProperties(“amazon”)用来标记参数是由amazon开头,属性associateId的值则来自application.properties中的amazon.associateId=xxx的值“xxx”。具体代码如下:
`
package com.bookcity.readingList;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 收集amazon属性
*
* @author Mr.c
* @create 2017-11-06
*/
@Component
@ConfigurationProperties(“amazon”)
public class AmazonProperties {
private String associateId;
public String getAssociateId() { return associateId;}public void setAssociateId(String associateId) { this.associateId = associateId;}
}
`
在Controller中的调用为
package com.bookcity.controller;import com.bookcity.readingList.AmazonProperties;import com.bookcity.readingList.Book;import com.bookcity.readingList.ReadingListRepository;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.*;import java.util.List;/** * 阅读列表控制器 * * @author Mr.c * @create 2017-11-03 * Copyright: Copyright (c) 2017 */@RequestMapping(value = "/readingList")@RestController@Api(value ="ReadingListController",description = "ReadingListController")public class ReadingListController { private ReadingListRepository readingListRepository; private AmazonProperties amazonProperties;//注入AmazonProperties //构造器 @Autowired public ReadingListController(ReadingListRepository readingListRepository,AmazonProperties amazonProperties){ this.readingListRepository=readingListRepository; this.amazonProperties=amazonProperties; } @ApiOperation(value = "查询读书列表",notes = "通过读者查询列表") @RequestMapping(value = "/{reader}",method = RequestMethod.GET) public String readersBooks(@PathVariable("reader")@ApiParam(value = "reader",name = "reader",required = true) String reader,@RequestBody Model model){ List<Book> readingList = readingListRepository.findByReader(reader); if(!readingList.isEmpty()){ model.addAttribute("books",readingList); model.addAttribute("reader",reader); //参数调用 amazonProperties.getAssociateId() model.addAttribute("amazonID", amazonProperties.getAssociateId()); }else{ model.addAttribute("books",readingList); } return "readingList"; } @RequestMapping(value="/{reader}",method = RequestMethod.POST) public String addToreadingList(@PathVariable("reader") String reader,Book book){ book.setReader(reader); readingListRepository.save(book); return "redirect:/readingList/{reader}"; }}
阅读全文
0 0
- SpringBoot 中针对同一类型的参数,可以用一个类来集中访问
- 一个类实现一个接口,可以重写接口中方法的参数类型吗?
- Java开发中利用枚举类来列举各个同一类型不同的值的情况
- 集中在一个特定类型的股票
- 针对小外包写的一个数据库访问基类
- C++中类的访问权限针对的是类而不是对象!(为什么类中的函数可以访问对象的私有成员?)
- jsp中访问action的集中方式
- 针对SpringBoot 封装的一个方便快捷的web 程序
- 关于glsl集中参数类型
- js获取src请求 得到一个参数和多个参数写法(这是集中网上的多个方法,自己cv过去就可以使用)
- java中可以出现的中文乱码的集中解决
- vector中针对自定义类型的排序
- error C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型
- MFC:error C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型
- 引用 error C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型
- 如何处理“AfxMessageBox” 2 个重载中没有一个可以转换所有参数类型?
- 错误C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型
- 错误C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型
- Linux下添加一个sudo超级用户
- 解决 setTimeout 传递带参数的函数无效果
- const和 枚举的一些知识。
- git commit
- utf-8和utf-8-sig
- SpringBoot 中针对同一类型的参数,可以用一个类来集中访问
- 计算机网络基础
- Linux下最新版LAMP环境(源码版)搭建详细解读
- static、const、define的一些理解
- 最小路径和
- linux下的简单操作
- PAT甲级 1033. To Fill or Not to Fill (25)
- C++结构中的位字段和共用体
- bmfont的设置