后缀是 .properties的一种文件,他和和yml文件,json格式一样都是属性文件,内部以key=value格式存储内容,一般以这种文件设置一些参数,使代码更加灵活(比方说:数据库文件等等都是在properties文件中),使用这种文件在不同环境中只需要更改配置文件即可。
比如在properties文件中编写数据库的连接信息
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=密码
在Java中可以用 java.util.Properties 类读取 properties文件中的信息
public class PropertiesTest {
@Test
public void propertiesTest() throws IOException {
Properties properties = new Properties();
// 读取指定文件中的键值对内容,需要再src内(也可以使用流读取,用流读取时,properties可以在任意位置)
properties.load(PropertiesTest.class.getResourceAsStream("/database.properties"));
//根据key获取value
String driver = properties.getProperty("jdbc.driver");
String username = properties.getProperty("jdbc.username");
System.out.println(driver);
System.out.println(username);
}
}
@Test
public void ResourceBundleTest() throws IOException {
//这种方式不用写 properties 文件后缀
ResourceBundle database = ResourceBundle.getBundle("database");
String driver = database.getString("jdbc.driver");
String username = database.getString("jdbc.username");
System.out.println(driver);
System.out.println(username);
}

通过 context:property-placeholder 标签来加载properties 文件。
我们可以通过 properties 文件配置数据库连接 或对象的属性值
# 数据库连接信息
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/库名?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=密码
# User的属性值
userId=1
name=properties
pass=123456
deleted=true
在Spring的XML文件中加载properties
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="propertiesTestUser" class="com.yu.spring.entiy.User">
<property name="userId" value="${userId}"/>
<property name="username" value="${name}"/>
<property name="password" value="${pass}"/>
<property name="deleted" value="${deleted}"/>
bean>
beans>
测试

spring提供了 @PropertySource(“classpath:文件名”)注解来加载properties文件中的配置
@Configuration
@PropertySource(value = {"classpath:database.properties"},ignoreResourceNotFound = true)
public class SpringConfig {
}
在配置文件上添加了SpringConfig 注解后,就可以通过配置文件获取 properties文件中的内容了
在加载完properties文件后,可以通过@value、@Bean设置成相应的对象
@Configuration
@PropertySource(value = {"classpath:database.properties"},ignoreResourceNotFound=true)
public class PropertiesConfiguration {
@Value("${jdbc.driver}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
//设置数据库连接
@Bean
public DataSource annotationPropertiesDataSource() throws Exception {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Value("${userId}")
private int userId;
@Value("${name}")
private String name;
@Value("${pass}")
private String pass;
@Value("${deleted}")
private boolean deleted;
//设置User对象
@Bean
public User annotationPropertiesUser() throws Exception {
User user = new User();
user.setUserId(userId)
.setUsername(name)
.setPassword(pass)
.setDeleted(deleted);
return user;
}
}