春天的 @PropertySource
Spring的@PropertySource注解用于向Spring环境提供属性文件。该注解与@Configuration类一起使用。Spring的PropertySource注解是可重复的,意味着可以在一个@Configuration类上有多个PropertySource。如果您使用的是Java 8或更高版本,则可以使用此功能。
一个Spring PropertySource的例子
让我们快速浏览一个简单的春季应用程序,我们将从属性文件中读取数据库配置详细信息并创建数据库连接。我们将在控制台打印一些数据库的元数据信息。创建一个简单的maven项目并添加Spring和MySQL依赖项。您也可以使用其他数据库作为示例,只需相应地更改配置。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>

package com.Olivia.spring;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
private String driverClass;
private String dbURL;
private String userName;
private char[] password;
private Connection con;
public DBConnection(String driverClass, String dbURL, String userName, char[] password) {
this.driverClass = driverClass;
this.dbURL = dbURL;
this.userName = userName;
this.password = password;
}
public Connection getConnection() {
if (this.con != null)
return con;
Connection con = null;
try {
System.out.println("Creating DB Connection");
Class.forName(driverClass);
con = DriverManager.getConnection(dbURL, userName, String.valueOf(password));
System.out.println("Successfully Created DB Connection");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
this.con = con;
return con;
}
public void close() {
System.out.println("DBConnection close called");
if (this.con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
注意:如果你正在创建一个真实世界的应用程序,可以使用Spring ORM。这样,Spring将负责数据库连接的管理,你可以专注于编写业务逻辑。现在让我们创建一个Spring配置类,在这里我们将使用PropertySource注解。
package com.Olivia.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource("classpath:db.properties")
@PropertySource("classpath:root.properties")
public class DBConfiguration {
@Autowired
Environment env;
@Bean
public DBConnection getDBConnection() {
System.out.println("Getting DBConnection Bean for App: "+env.getProperty("APP_NAME"));
DBConnection dbConnection = new DBConnection(env.getProperty("DB_DRIVER_CLASS"), env.getProperty("DB_URL"), env.getProperty("DB_USERNAME"), env.getProperty("DB_PASSWORD").toCharArray());
return dbConnection;
}
}
注意到我正在将多个属性文件加载到Spring环境中。让我们来看看这些属性文件的内容。db.properties
#MYSQL Database Configurations
DB_DRIVER_CLASS=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://localhost:3306/Test
DB_USERNAME=scdev
DB_PASSWORD=scdev
根配置文件
APP_NAME=PropertySource Example
让我们创建主类并获取数据库详细信息。
package com.Olivia.spring;
import java.sql.Connection;
import java.sql.SQLException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringMainClass {
public static void main(String[] args) throws SQLException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.Olivia.spring");
context.refresh();
DBConnection dbConnection = context.getBean(DBConnection.class);
Connection con = dbConnection.getConnection();
System.out.println(con.getMetaData().getDatabaseProductName());
System.out.println(con.getMetaData().getDatabaseProductVersion());
// close the spring context
context.close();
}
}
只需将上述类作为Java应用程序运行,它将产生以下输出。
Getting DBConnection Bean for App: PropertySource Example
Creating DB Connection
Successfully Created DB Connection
MySQL
5.7.18
DBConnection close called
为了提高可读性,我已删除了Spring日志输出到控制台的调试信息。
Spring @PropertySource 多个文件 – @PropertySources
有另一种方法可以为配置类加载多个属性文件。
@PropertySources({
@PropertySource("classpath:db.properties"),
@PropertySource("classpath:root.properties")})
public class DBConfiguration {
}
从Java 8开始,PropertySource注解成为可重复使用的。对于早期的Java版本,@PropertySources是为配置类提供多个属性文件的方式。
覆盖Spring PropertySource的值
我们可以将多个属性文件加载到Spring环境中。如果多个文件中存在相同的键,则最后加载的属性文件将覆盖先前的值。因此,如果您的属性值不符合预期,请检查其他属性文件中是否存在相同的键,以及加载这些属性文件的顺序是什么。
Spring 属性源外部文件
有时候我们的配置文件会存在特定的位置,它们并不属于项目的类路径。我们可以配置PropertySource来从文件系统加载属性文件。
@PropertySource("file:/Users/scdev/db.properties")
春天的属性源环境变量
请注意,上述配置从外部位置读取属性文件的方式在我的本地系统上能正常工作,但对其他人或服务器则不适用。我们还可以在PropertySource中读取系统变量,所以下面的配置对每个人都能正常工作。
@PropertySource("file:${HOME}/db.properties")
春季PropertySource忽略了FileNotFoundException。
如果找不到属性文件,则会出现FileNotFoundException。有时,我们不希望抛出异常,因为我们的应用程序也可以使用默认值工作。我们可以将PropertySource中的ignoreResourceNotFound设置为true,以告诉Spring框架在找不到文件时不要抛出异常。
@PropertySource(value = "classpath:root.properties", ignoreResourceNotFound=true)
这就是关于Spring PropertySource示例的所有内容了。您可以从我们的GitHub仓库中查看源代码和Maven项目。
春季属性源项目