springboot实战之读取配置文件
来源:互联网 发布:无锡华云数据面试 编辑:程序博客网 时间:2024/05/16 05:41
大话springboot
我们聊一聊最近很火的springboot,它可以很快捷,快速的创建一个spring的web项目。说白一点,他是spring+springMVC+持久层框架+数据源连接池+web容器+。。。的脚手架。
springboot的设计目的是用来简化新Spring应用的初始搭建以及开发过程。对于很熟练的开发(以我所见,起码是处于开发一线3年以上期间的才算熟练),spingboot的出现无疑是福音。否则,强行去运用springboot反而适得其反,黑盒开发。如果一定要用springboot,那么一定要多动脑,多思考,多去开源社区走走开发前辈趟出来坑。
导包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version></parent><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies>
导入以上jar包即可进行简单的spingboot项目开发
配置文件和启动入口
resources文件夹下新建application.yml 文件。文件内容如下
spring: profiles: active: dev
resources文件夹下新建application-dev.yml 文件。文件内容如下
server: port: 9527
src文件夹下建包并创建java文件,内容如下:
@SpringBootApplicationpublic class SpringbootConfigApplication { public static void main(String[] args){ SpringApplication.run(SpringbootConfigApplication.class,args) ; }}
此时已经创建好基于springboot的web项目,可以启动SpringbootConfigApplication的main方法,然后用telnet localhost 9527 能够连接上服务。
读取项目配置文件内容
在resources文件夹下新建test.properties,内容如下
com.hsy.name=heshiyuancom.hsy.age=18
在SpringbootConfigApplication所在包下新建bean包(很重要,springboot只会扫描入口类层级一下的注解),并新建user.java文件,内容如下
@Configuration@PropertySource(value = "classpath:test.properties")@ConfigurationProperties(prefix = "com.hsy")public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
在application.yml添加如下内容,实现第二种配置属性读取。
my: name: heshiyuan age: 18 number: ${random.int} uuid : ${random.uuid} max: ${random.int(10)} value: ${random.value} greeting: hi,i'm ${my.name}
bean包下新建ConfigBean.java.
@ConfigurationProperties(prefix = "my")@Componentpublic class ConfigBean { private String name; private int age; private int number; private String uuid; private int max; private String value; private String greeting; //...省略seter,getter方法}
提供http api接口
在bean包统计目录创建controller包,并新建RestfulController.java
@RestController@RequestMapping(value="/api/restful/")@EnableConfigurationProperties({ConfigBean.class,User.class})public class RestfulController { @Autowired ConfigBean configBean; @RequestMapping(value = "/v1.0/configBean",method = RequestMethod.GET) public String beanValue(){ return configBean.getGreeting()+" >>>>"+configBean.getName()+" >>>>"+ configBean.getUuid()+" >>>>"+configBean.getMax(); } @Autowired User user; @RequestMapping(value = {"/v1.0/user/list","/users"},method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public String user(){ return user.getName()+user.getAge(); } @Value("${my.name}") private String name; @Value("${my.age}") private int age; @RequestMapping(value = "/v1.0/my",method = RequestMethod.GET) public String dataValue(){ return name+":"+age; }}
测试
用postman api工具进行测试
尾声
项目源码:https://github.com/shiyuan2he/springboot.git
- springboot实战之读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- 4.Springboot 之 自定义配置文件及读取配置文件
- SpringBoot读取配置文件乱码
- springboot读取自定义配置文件
- SpringBoot配置文件读取
- SpringBoot读取配置文件
- SpringBoot 读取自定义配置文件
- springboot读取配置文件
- 【SpringBoot】SpringBoot之入门配置文件
- SpringBoot 读取配置文件属性值
- SpringBoot 读取配置文件及profiles切换配置文件
- Springboot 之 多配置文件
- gradle 中使用FTP协同处理文件
- Matlab的regionprops详解 连通区域
- linux中lpr命令
- ApplicationContext读取xml配置文件 报错
- LINUX中操作命令
- springboot实战之读取配置文件
- 144. Binary Tree Preorder Traversal(JAVA)
- Java中Unsafe类详解
- 解决MySQL中UTF-8编码的数据在命令行显示乱码
- python os.path模块常用方法详解
- 高通ar,unity实现3D物体识别,图片识别
- 程序员职业规划
- h5页面点击按钮,触发手机自带的发短信
- CentOS安装Qt编译错误以及无法调试的问题解决( /usr/bin/ld:cannot find –lGL 和 Debugginghas failed)