springboot读取linux文件_Springboot中使用@value读取配置文件

本文介绍了如何在Springboot应用中通过@Value注解读取配置文件,并展示了从Linux文件系统获取数据的步骤,包括配置pom.xml依赖,设置application.properties参数,创建DataBaseProperties类,编写Controller及主函数,最后通过浏览器访问获取JSON数据。
摘要由CSDN通过智能技术生成
972d67d64e573c142bad26fa34426e94.png

1.首先配置pom.xml里的依赖

org.springframework.bootspring-boot-configuration-processortrue

2.在:"项目所在文件夹springbootxxxsrcmainesourcesapplication.properties"路径下的application.properties文件配置参数,例如:

database.driverName=com.mysql.jdbc.Driverdatabase.url=jdbc:mysql://localhost:3306/my_mall?useUnicode=true&characterEncoding=utf-8&useSSL=falsedatabase.username=rootdatabase.password=root

3.创建一个新类DataBaseProperties

package com.springboot.chapter3Scan.propers;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/** * 描述:使用属性配置 * * @author  * @create 2019-03-16 9:35 /@Componentpublic class DataBaseProperties { @Value("${database.driverName}") private String driverName = null; @Value("${database.url}") private String url = null; private String userName = null; private String passWord = null; public void setDriverName(String driverName) { this.driverName = driverName; } public void setUrl(String url) { this.url = url; } @Value("${database.username}") public void setUserName(String userName) { this.userName = userName; } @Value("${database.password}") public void setPassWord(String passWord) { this.passWord = passWord; } public String getDriverName() { return driverName; } public String getUrl() { return url; } public String getUserName() { return userName; } public String getPassWord() { return passWord; }}

4.添加Controller类properController

package com.springboot.chapter3Scan.controller;import com.springboot.chapter3Scan.propers.DataBaseProperties;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * 描述: * * @author  * @create 2019-03-16 10:08 */@Controllerpublic class properController { @Autowired private DataBaseProperties dbp = null; @RequestMapping("/dbp") @ResponseBody public DataBaseProperties getDbp() { return dbp; } public void setDbp(DataBaseProperties dbp) { this.dbp = dbp; }}

5.使用主函数启动

package com.springboot.chapter3Scan.main;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import java.sql.SQLException;@SpringBootApplication(scanBasePackages = "com.springboot.chapter3Scan")//@ComponentScanpublic class Chapter3ScanApplication { public static void main(String[] args) throws SQLException { SpringApplication.run(Chapter3ScanApplication.class, args); }}

6.打开浏览器访问http://localhost:8080/dbp

即可访问返回的json数据

a324a5d61c9600c1d137cd8b7f29dfba.png

项目的路径如下:

49f628af6fce1b10396a21a3b12991a3.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值