Spring Boot 使用属性文件

一、使用默认属性文件application.properties

1)@Value

  • 在application.properties配置文件中写入
database.driverName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306
database.username=root
database.password=123
  • 编写读取配置的类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class DataBaseProperties {
    @Value("${database.driverName}")
    private String driverName;
    @Value("${database.url}")
    private String url;
    private String username;
    private String password;

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }
    @Value("${database.username}")
    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }
    @Value("${database.password}")
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "DataBaseProperties{" +
                "driverName='" + driverName + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

@Value注解既可以加在属性上,也可以加在set方法上,使用${…}读取配置文件中的内容。

  • 编写controller类测试
import com.mvp.www.config.DataBaseProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController(value = "HelloController")
public class HelloController {
    @Autowired
    private DataBaseProperties dataBaseProperties;


    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String helloWorld() {
        System.out.println("--------------------");
        System.out.println(dataBaseProperties.toString());
        return "Hello World!";
    }
}

在这里插入图片描述
在这里插入图片描述
2)@ConfigurationProperties

  • 用到依赖,添加到pom.xml中
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
  • 编写配置类
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("database")
public class DataBasePro {
    private String driverName;
    private String url;
    private String username;
    private String password;

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "DataBasePro{" +
                "driverName='" + driverName + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

该注解将“database”与DataBasePro.java中的属性名称组成全限定名去配置文件查找。
在这里插入图片描述

二、自定义配置文件jdbc.properties

把数据库属性配置单独存在jdbc.properites文件中,用@PropertySource加载

@SpringBootApplication(scanBasePackages = {"com.mvp.www.*"})
@PropertySource(value = {"classpath:application.properties", "classpath:jdbc.properties"})
public class Part3Application {

    public static void main(String[] args) {
        SpringApplication.run(Part3Application.class, args);
    }

}

在这里插入图片描述

三、小记

慢慢探索、深入,争取写出点有意义的东西。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值