读取properties的简单方法,使用@Configuration
- 配置类代码如下
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()); } }
最后就能输出 需要的配置文件对应信息
版权声明:如需转载,请注明!PS:如是转载随便,请忽略

浙公网安备 33010602011771号