Java 如何使用Spring加载属性文件?
我很想知道如何使用Spring加载application.properties文件或任何其他属性文件 下面是实现这一点的XMLJava 如何使用Spring加载属性文件?,java,spring,Java,Spring,我很想知道如何使用Spring加载application.properties文件或任何其他属性文件 下面是实现这一点的XML <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schem
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id = "myProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
</beans>
classpath*:application.properties
如您所见,application.properties
文件正在使用propertyplaceholderconfigure
类加载
而locations
是类PropertyPlaceholderConfigurer
中Resource
类型的实例变量。因此,上面示例classpath*:application.properties
中的值是实现资源接口的类的实例名。对吗
如果是,那么在那之后,spring后端如何进一步加载文件
谁能分享一下吗
谢谢是的,您是对的,在将属性文件加载到spring环境之后,这是xml配置的相应java代码。通过使用
java.reflection
spring将把值注入到springbean中
@Bean
public static PropertyPlaceholderConfigurer myProperties() {
PropertyPlaceholderConfigurer ppc
= new PropertyPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[]
{ new ClassPathResource( "application.properties" ) };
ppc.setLocations( resources );
ppc.setIgnoreUnresolvablePlaceholders( true );
return ppc;
}
您的意思是说PropertyPlaceHolderConfigure的myProperties实例在spring框架中自动调用properties()(您提到的)方法?XML编译的时刻。。!我想您误解了,spring将加载此
xml
,并创建名为myProperties
的PropertyPlaceholderConfigurer
(此bean用于在JMX中注册bean)@jon我可以知道吗?您是如何知道您提到的代码在spring加载xml后会被执行的?它是用框架写的吗?只是想知道……!:)这段代码反映了您所拥有的xml,您的问题对我来说并不十分清楚@j