首先新建一个springboot项目,此处省略。

1.新建一个application.properties

person.name=kevin
person.age=6
person.sex=male

 

2.新建一个类,自动读取对应字段的值

有两种方式,

第一种

package cn.wq;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@EnableConfigurationProperties
@PropertySource("classpath:application.properties")
public class Person2Properties {
    @Value("${person.name}")
    private String name;

    @Value("${person.age}")
    private String age;

    @Value("${person.sex}")
    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

 

第二种

package cn.wq;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "person") /*如果使用prefix,则属性名中不能使用@Value来注解,但是必须在spring启动时,添加注解 @EnableConfigurationProperties(PersonProperties.class)*/
public class PersonProperties {

    private String name;
    private String age;
    private String sex;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

 

3.启动主程序:

package cn.wq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(PersonProperties.class)
@SpringBootApplication
public class Application2  implements CommandLineRunner {
    public static void main(String[] args){
        SpringApplication.run(Application2.class,args);
    }

    @Autowired
    PersonProperties personProperties;

    @Autowired
    Person2Properties person2Properties;

    @Override
    public void run(String... args) {
        System.out.println("程序实际上的入口在这里。");
        System.out.println("name:"+personProperties.getName());
        System.out.println("age:"+personProperties.getAge());
        System.out.println("sex:"+personProperties.getSex());


        System.out.println("2-name:"+person2Properties.getName());
        System.out.println("2-age:"+person2Properties.getAge());
        System.out.println("2-sex:"+person2Properties.getSex());
    }
}

 

4.运行,输出结果:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

2019-06-19 20:20:50.348  INFO 18344 --- [           main] cn.wq.Application2                    : Starting Application2 on cnki5213 with PID 18344 (C:\AppConsoleSpringBoot\target\classes started by Administrator in C:\AppConsoleSpringBoot)
2019-06-19 20:20:50.354  INFO 18344 --- [           main] cn.wq.Application2                    : No active profile set, falling back to default profiles: default
2019-06-19 20:20:51.303  INFO 18344 --- [           main] cn.wq.Application2                    : Started Application2 in 1.564 seconds (JVM running for 3.74)
程序实际上的入口在这里。
name:kevin
age:6
sex:male
2-name:kevin
2-age:6
2-sex:male

Process finished with exit code 0

 

鸣谢

参考文献:https://www.cnblogs.com/V1haoge/p/7183408.html 

posted on 2019-06-19 20:25  踏歌&而行  阅读(1110)  评论(0编辑  收藏  举报