zoukankan      html  css  js  c++  java
  • Springboot读取配置文件及自定义配置文件

    创建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 @Configuration
     9 @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-8
    
    test.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 @Component
     8 @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,可以看到输出结果

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

    转自:https://www.cnblogs.com/kellyJAVA/p/8030395.html

  • 相关阅读:
    2_C语言中的数据类型 (九)逻辑运算符与if语句、switch、条件运算符?、goto语句与标号
    2_C语言中的数据类型 (八)运算符
    Python_sklearn机器学习库学习笔记(七)the perceptron(感知器)
    C++ STL 学习笔记__(7)Set和multiset容器
    2_C语言中的数据类型 (七)printf与scanf
    郑捷《机器学习算法原理与编程实践》学习笔记(第五章 梯度寻优)5.1 最优化与计算复杂度
    机器学习笔记(一)机器学习与数学分析
    2_C语言中的数据类型 (七)类型限定
    郑捷《机器学习算法原理与编程实践》学习笔记(第四章 推荐系统原理)(三)SVD
    [译]Javascript timing事件
  • 原文地址:https://www.cnblogs.com/flywang/p/9617778.html
Copyright © 2011-2022 走看看