Springboot读取配置文件及自定义配置文件的方法

 更新时间:2017年12月13日 08:43:28   作者:Java从入门到跑路  
这篇文章主要介绍了Springboot读取配置文件及自定义配置文件的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下

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

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.9.RELEASE</version>
  </parent>
 <dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!-- 单元测试使用 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
  </dependency>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

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

package com.kelly.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration //自动加载配置信息
@ComponentScan("com.kelly")//使包路径下带有注解的类可以使用@Autowired自动注入
public class StartApplication {
  public static void main(String[] args) {
    SpringApplication.run(StartApplication.class, args);
  }
}
package com.kelly.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration //自动加载配置信息
@ComponentScan("com.kelly")//使包路径下带有注解的类可以使用@Autowired自动注入
public class StartApplication {
  public static void main(String[] args) {
    SpringApplication.run(StartApplication.class, args);
  }
}
package com.kelly.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class FirstController {
  @Value("${test.name}")
  private String name;
  @Value("${test.password}")
  private String password;
  @RequestMapping("/")
  @ResponseBody
  String home()
  {
    return "Hello Springboot!";
  }
  @RequestMapping("/hello")
  @ResponseBody
  String hello()
  {
    return "name: " + name + ", " + "password: " + password;
  }
}

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

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

  DefineEntity.java

package com.kelly.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="defineTest")
@PropertySource("classpath:define.properties")
public class DefineEntity {
  private String pname;
  private String password;
  public String getPname() {
    return pname;
  }
  public void setPname(String pname) {
    this.pname = pname;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
}

SecondController.java

package com.kelly.controller;
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;
import com.kelly.entity.DefineEntity;
@Controller
public class SecondController {
  @Autowired
  DefineEntity defineEntity;
  @RequestMapping("/define")
  @ResponseBody
  String define()
  {
    return "test.name:" + defineEntity.getPname() + ", test.password:" + defineEntity.getPassword();
  }
}

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

补充:我的项目的目录结构

总结

以上所述是小编给大家介绍的Springboot读取配置文件及自定义配置文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Spring-cloud Feign 的深入理解

    Spring-cloud Feign 的深入理解

    这篇文章主要介绍了Spring-cloud Feign 的深入理解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • java字符串格式化(String类format方法)

    java字符串格式化(String类format方法)

    这篇文章主要介绍了java字符串格式化(String类format方法),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • mybatis拦截器实现数据库数据权限隔离方式

    mybatis拦截器实现数据库数据权限隔离方式

    通过Mybatis拦截器,在执行SQL前添加条件实现数据权限隔离,特别是对于存在用户ID区分的表,拦截器会自动添加如user_id=#{userId}的条件,确保SQL在执行时只能操作指定用户的数据,此方法主要应用于Mybatis的四个阶段
    2024-11-11
  • springboot多数据源配合docker部署mysql主从实现读写分离效果

    springboot多数据源配合docker部署mysql主从实现读写分离效果

    这篇文章主要介绍了springboot多数据源配合docker部署mysql主从实现读写分离,通过使用docker获取mysql镜像,具体内容详情跟随小编一起看看吧
    2021-09-09
  • 详解获取Spring MVC中所有RequestMapping以及对应方法和参数

    详解获取Spring MVC中所有RequestMapping以及对应方法和参数

    本篇文章主要介绍了详解获取Spring MVC中所有RequestMapping以及对应方法和参数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • SpringBoot整合OpenCV的实现示例

    SpringBoot整合OpenCV的实现示例

    这篇文章主要介绍了SpringBoot整合OpenCV的实现示例。文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • JAVA8发送带有Body的HTTP GET请求

    JAVA8发送带有Body的HTTP GET请求

    本文主要介绍了JAVA8发送带有Body的HTTP GET请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 简单探索 Java 中的惰性计算

    简单探索 Java 中的惰性计算

    这篇文章主要介绍了简单探索 Java 中的惰性计算,惰性计算(尽可能延迟表达式求值)是许多函数式编程语言的特性。惰性集合在需要时提供其元素,无需预先计算它们,这带来了一些好处。,需要的朋友可以参考下
    2019-06-06
  • SpringBoot实现轻量级动态定时任务管控及组件化的操作步骤

    SpringBoot实现轻量级动态定时任务管控及组件化的操作步骤

    文章介绍了一种在SpringBoot中实现动态定时任务的解决方案,基于COLA架构理论,封装到了组件层,该组件支持类级别和方法级别的定时任务注册,并提供了易用性和扩展性,组件使用Maven形式引入,并且可以通过YAML配置文件进行设置,感兴趣的朋友一起看看吧
    2024-11-11
  • javaweb登录验证码的实现方法

    javaweb登录验证码的实现方法

    这篇文章主要为大家详细介绍了javaweb登录验证码的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11

最新评论