版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wu2374633583/article/details/83414943
application.properties的加载
database.driverName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/qinhe
database.username=root
database.password=root
方式1:采用@Value("${}")方式
DatabaseProperties.java
package com.wuk.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("databaseProperties")
public class DatabaseProperties {
@Value("${database.driverName}")
private String driverName;
@Value("${database.url}")
private String url;
@Value("${database.username}")
private String username;
@Value("${database.password}")
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) {
System.out.println(username);
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
System.out.println(password);
this.password = password;
}
}
测试
package com.wuk.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication
public class SpringbootIocApplication {
@Autowired
private DatabaseProperties databaseProperties;
public static void main(String[] args) {
SpringApplication.run(SpringbootIocApplication.class, args);
}
@RequestMapping("/index")
@ResponseBody
public String goIndex(){
return "Password="+databaseProperties.getPassword();
}
}
结果:
Password=root
方式2:采用@ConfigurationProperties(“database”)
application.properties
database.driverName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/qinhe
database.username=root
database.password=root
DatabaseProperties.java
package com.wuk.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("database")
public class DatabaseProperties {
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) {
System.out.println(username);
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
System.out.println(password);
this.password = password;
}
}
自定义文件jdbc.properties的加载
database.driverName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/qinhe
database.username=root
database.password=root
DatabaseProperties .java
@Component
@ConfigurationProperties("database")
@PropertySource("classpath:jdbc.properties")
public class DatabaseProperties {