一、使用默认属性文件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);
}
}
三、小记
慢慢探索、深入,争取写出点有意义的东西。