我使用spring與我的應用程序,我可以從類路徑中的某個文件注入一些屬性到我的應用程序中,並且一切正常。即如何從主類中將屬性注入到spring bean中
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchContextAttributes" value="true" />
<property name="contextOverride" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
現在我可以在我的春天環境中使用${any.property.from.application.properties}
。而在我的主類:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appContext.xml");
它的工作原理,以及,我的問題是我怎麼注入的春天上下文屬性文件的位置,沒有它,首先在那裏,我要讓我的應用程序配置。如果我從C:\dir
或/user/home/dir
執行我的應用我認爲在應用程序上下文的值應爲C:\application.properties
或/user/home/dir/application.properties
我將如何將此注入Main類的應用程序上下文中? – London
你可以使用這樣的東西來注入一個屬性位置,確保你將這個屬性添加到屬性中,你可以這樣做:PropertyPlaceholderConfigurer configurer =(PropertyPlaceholderConfigurer)ctx.getBean (PropertyPlaceholderConfigurer.class); \t \t資源rsc = new FileSystemResource(「file:/// C:\\ Applications \\ one.properties」); \t \t configurer.setLocation(rsc);我不確定你爲什麼要這樣做,但是如果你在spring容器啓動後注入屬性文件,你不能在上下文文件中使用屬性。 –