springBoot 读取properties文件

2024-09-08 01:38

本文主要是介绍springBoot 读取properties文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.创建maven工程,在pom文件中添加依赖

复制代码

 1   <parent>2     <groupId>org.springframework.boot</groupId>3     <artifactId>spring-boot-starter-parent</artifactId>4     <version>1.5.9.RELEASE</version>5     </parent>6 7   <dependencies>8     <dependency>9         <groupId>org.springframework.boot</groupId>
10         <artifactId>spring-boot-starter-web</artifactId>
11     </dependency>
12     <!-- 单元测试使用 -->
13     <dependency>
14         <groupId>org.springframework.boot</groupId>
15         <artifactId>spring-boot-starter-test</artifactId>
16     </dependency>
17   
18     <dependency>
19       <groupId>junit</groupId>
20       <artifactId>junit</artifactId>
21       <scope>test</scope>
22     </dependency>
23    
24   </dependencies>

复制代码

 

2.创建项目启动类 StartApplication.java

复制代码

 1 package com.kelly.controller;2 3 import org.springframework.boot.SpringApplication;4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;5 import org.springframework.context.annotation.ComponentScan;6 import org.springframework.context.annotation.Configuration;7 8 @Configuration9 @EnableAutoConfiguration //自动加载配置信息
10 @ComponentScan("com.kelly")//使包路径下带有注解的类可以使用@Autowired自动注入
11 public class StartApplication {
12     public static void main(String[] args) {
13         SpringApplication.run(StartApplication.class, args);
14     }
15 }

复制代码

3.编辑配置文件application.properties及自定义配置文件define.properties

  application.properties

复制代码

#访问的根路径
server.context-path=/springboot
#端口号
server.port=8081
#session失效时间
server.session-timeout=30
#编码
server.tomcat.uri-encoding=utf-8test.name=kelly
test.password=admin123

复制代码

  define.properties

defineTest.pname=test
defineTest.password=test123

4.读取application.properties配置文件中的属性值

  FirstController.java

复制代码

 1 package com.kelly.controller;2 3 import org.springframework.beans.factory.annotation.Value;4 import org.springframework.stereotype.Controller;5 import org.springframework.web.bind.annotation.RequestMapping;6 import org.springframework.web.bind.annotation.ResponseBody;7 8 9 @Controller
10 public class FirstController {
11     
12     @Value("${test.name}")
13     private String name;
14     
15     @Value("${test.password}")
16     private String password;
17     
18     @RequestMapping("/")
19     @ResponseBody
20     String home()
21     {
22         return "Hello Springboot!";
23     }
24     
25     @RequestMapping("/hello")
26     @ResponseBody
27     String hello()
28     {
29         return "name: " + name + ", " + "password: " + password;
30     }
31 }

复制代码

5.打开浏览器,输入 http://localhost:8081/springboot/hello 即可看到结果

 

 

 

6.使用java bean的方式读取自定义配置文件 define.properties

  DefineEntity.java

复制代码

 1 package com.kelly.entity;2 3 import org.springframework.boot.context.properties.ConfigurationProperties;4 import org.springframework.context.annotation.PropertySource;5 import org.springframework.stereotype.Component;6 7 @Component8 @ConfigurationProperties(prefix="defineTest")9 @PropertySource("classpath:define.properties")
10 public class DefineEntity {
11     
12     private String pname;
13     
14     private String password;
15 
16     public String getPname() {
17         return pname;
18     }
19 
20     public void setPname(String pname) {
21         this.pname = pname;
22     }
23 
24     public String getPassword() {
25         return password;
26     }
27 
28     public void setPassword(String password) {
29         this.password = password;
30     }
31     
32     
33 }

复制代码

  SecondController.java

复制代码

 1 package com.kelly.controller;2 3 import org.springframework.beans.factory.annotation.Autowired;4 import org.springframework.stereotype.Controller;5 import org.springframework.web.bind.annotation.RequestMapping;6 import org.springframework.web.bind.annotation.ResponseBody;7 8 import com.kelly.entity.DefineEntity;9 
10 @Controller
11 public class SecondController {
12     
13     @Autowired
14     DefineEntity defineEntity;
15     
16     @RequestMapping("/define")
17     @ResponseBody
18     String define()
19     {
20         return "test.name:" + defineEntity.getPname() + ", test.password:" + defineEntity.getPassword();
21     }
22 }

复制代码

7.打开浏览器,访问 http://localhost:8081/springboot/define,可以看到输出结果

这篇关于springBoot 读取properties文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1146747

相关文章

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

springMVC返回Http响应的实现

《springMVC返回Http响应的实现》本文主要介绍了在SpringBoot中使用@Controller、@ResponseBody和@RestController注解进行HTTP响应返回的方法,... 目录一、返回页面二、@Controller和@ResponseBody与RestController

JAVA集成本地部署的DeepSeek的图文教程

《JAVA集成本地部署的DeepSeek的图文教程》本文主要介绍了JAVA集成本地部署的DeepSeek的图文教程,包含配置环境变量及下载DeepSeek-R1模型并启动,具有一定的参考价值,感兴趣的... 目录一、下载部署DeepSeek1.下载ollama2.下载DeepSeek-R1模型并启动 二、J

springboot rocketmq配置生产者和消息者的步骤

《springbootrocketmq配置生产者和消息者的步骤》本文介绍了如何在SpringBoot中集成RocketMQ,包括添加依赖、配置application.yml、创建生产者和消费者,并展... 目录1. 添加依赖2. 配置application.yml3. 创建生产者4. 创建消费者5. 使用在

Spring Retry 实现乐观锁重试实践记录

《SpringRetry实现乐观锁重试实践记录》本文介绍了在秒杀商品SKU表中使用乐观锁和MybatisPlus配置乐观锁的方法,并分析了测试环境和生产环境的隔离级别对乐观锁的影响,通过简单验证,... 目录一、场景分析 二、简单验证 2.1、可重复读 2.2、读已提交 三、最佳实践 3.1、配置重试模板

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分