方式一、
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:config/my.prop") @ConfigurationProperties(prefix = "aaa") public class PropConf { private String a; private String b; private String c; public String getA() {return a;} public void setA(String a) {this.a = a;} public String getB() {return b;} public void setB(String b) {this.b = b;} public String getC() {return c;} public void setC(String c) {this.c = c;} public void show(){ System.out.println("a --- > " + a); System.out.println("b --- > " + b); System.out.println("c --- > " + c); } }
方式二、
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyConf { @Value("${string.port}") private int intPort; @Value("${string.port}") private String stringPort; @Value("${db.link.url}") private String dbUrl; @Value("${db.link.driver}") private String dbDriver; @Value("${db.link.username}")private String dbUsername; @Value("${db.link.password}")private String dbPassword; public void show(){ System.out.println("==========================================="); System.out.println("intPort : " + (intPort + 1111)); System.out.println("stringPort : " + (stringPort + 1111)); System.out.println("string : " + dbUrl); System.out.println("string : " + dbDriver); System.out.println("string : " + dbUsername); System.out.println("string : " + dbPassword); System.out.println("==========================================="); } }
方式三、
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:config/my.prop") @ConfigurationProperties(prefix = "aaa") public class PropConf { private String a; private String b; private String c; public void setA(String a) { this.a = a; } public void setB(String b) { this.b = b; } public void setC(String c) { this.c = c; } public void show(){ System.out.println("a --- > " + a); System.out.println("b --- > " + b); System.out.println("c --- > " + c); } }