Java 如何将从控制器读取的属性文件的值注入bean?
我想使用控制器从.properties文件中读取属性,并通过将检索到的属性存储在pojo中,在jsp文件(使用依赖项注入的视图)中显示其值。属性将由spring加载,因此控制器无需执行此操作。您可以直接将属性插入视图。试试这个Java 如何将从控制器读取的属性文件的值注入bean?,java,spring,properties,dependency-injection,Java,Spring,Properties,Dependency Injection,我想使用控制器从.properties文件中读取属性,并通过将检索到的属性存储在pojo中,在jsp文件(使用依赖项注入的视图)中显示其值。属性将由spring加载,因此控制器无需执行此操作。您可以直接将属性插入视图。试试这个 @Component class MyComponent { @Property(key = "proo.xmlurl") public void setUrlString(String urlStr) { try { t
@Component
class MyComponent {
@Property(key = "proo.xmlurl")
public void setUrlString(String urlStr) {
try {
this.url = new URL(urlStr);
} catch(MalformedURLException e) {
throw new IllegalArgumentException(urlStr + " is not a valid http url", e);
}
}
}
在您的属性文件中放入此
proo.xmlurl=${proo.xmlurl}
AppContext可以具有以下功能:
<context:property-placeholder location="classpath:my.properties" ignore-unresolvable="true"/>
其中属性文件包含此项或类似项
language = java
alllanguages = java and \
c++
somethingelse = whatever
请尝试提交一点,这样它可以帮助挖掘您的问题。为什么您要将这些值存储在POJO中,我认为这是关于MessageSource org.springframework.context.support.ReloadableResourceBundleMessageSource的-下面是google建议的第一个链接:嗨,这可能是个愚蠢的问题,但我不知道答案。。如果spring通过PropertyPlaceholder加载属性文件,我如何在jsp(视图)中访问它
language = java
alllanguages = java and \
c++
somethingelse = whatever