欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-10 14:55:58
1.创建maven工程,在pom文件中添加依赖 org.springframework.boot...

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.打开浏览器,输入 即可看到结果

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

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.打开浏览器,访问 ,可以看到输出结果

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

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

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

总结

以上所述是小编给大家介绍的springboot读取配置文件及自定义配置文件的方法,希望对大家有所帮助