一、 通过 @PropertySource 注解实现配置文件加载

在java类文件中使用 PropertySource 注解:

@PropertySource(value={"classpath:jdbc.properties"})
public class ReadProperties {
@Value(value="${jdbc.username}")
private String username;
}

二**、通过 context:property-placeholder 标签实现配置文件加载**

  1. 用法示例: 在spring.xml配置文件中添加标签

    代码如下:

    <context:property-placeholder ignore-unresolvable="true" location="classpath:redis-key.properties"/>
    
  2. 在 spring.xml 中使用配置文件属性:

    <!-- 基本属性 url、user、password -->
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    
  3. 在java文件中使用:

    @Value("${jdbc_url}")
    ivate String jdbcUrl; // 注意:这里变量不能定义成static