读取properties的简单方法,使用@Configuration

本文介绍了如何使用Spring框架配置类来读取配置文件,并通过示例代码展示了配置类及测试接口类的具体实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 配置类代码如下
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Value("${test}")
    private String test;

    public String getTest() {
        return test;
    }
    
}

  2.Spring配置

<!-- 获取配置属性值 -->
    <bean id="configProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:app/env/config.properties</value>
                <value>classpath:app/config/database.properties</value>
                <value>classpath:app/config/redis.properties</value>            
            </list>
        </property>
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties" />
    </bean>        

  3.测试接口类

@Service
public class SsoInterface {

    @Autowired
    private SsoConfig SsoConfig;


  public void test(){
          System.out.println(SsoConfig.getTest());
    }  

}

最后就能输出 需要的配置文件对应信息

 

转载于:https://www.cnblogs.com/zwdx/p/7682953.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值