在控制器中注入属性值的最佳方法是什么?
这真的是从属性文件向控制器注入属性的最简单方法吗?然后,它需要在每个需要一些属性的控制器上导入属性文件内容。在像我这样的项目中,大约有 30 个控制器,其中 10 个控制器需要这个国家财产,我想这看起来会很混乱。 我是否正确理解了 @Value
的用法?
@Controller
@RequestMapping(value = "/simple")
@ImportResource("classpath:/META-INF/properties-config.xml")
public class SimpleController {
private @Value("#{exampleProperties['simple.country']}") String country;
}
properties-config.xml (跳过了 xml 和架构内容)
<beans>
<util:properties id="exampleProperties" location="classpath:/simple.properties" />
</beans>
另外,当尝试在多个控制器中导入properties-config.xml 资源时,我会收到此类消息。这似乎不是正确的做法,但我无法找出更好的方法..
01 Apr 2011 04:52:29,859 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory []: Overriding bean definition for bean 'exampleProperties': replacing [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
Is this really the easiest way how to inject a property in the controller from properties file? It then needs to import the property file stuff on each controller which needs some properties. In a project like mine with about 30 controllers and 10 of them needing this country property it would look like mess i guess..
Did I understand the usage of @Value
correctly?
@Controller
@RequestMapping(value = "/simple")
@ImportResource("classpath:/META-INF/properties-config.xml")
public class SimpleController {
private @Value("#{exampleProperties['simple.country']}") String country;
}
properties-config.xml (skipped the xml and schema stuff)
<beans>
<util:properties id="exampleProperties" location="classpath:/simple.properties" />
</beans>
Also when trying to import the properties-config.xml resource in more than one controller I get such messages. It just doesn't seem the right way how to do it, but i can't figure out a better one..
01 Apr 2011 04:52:29,859 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory []: Overriding bean definition for bean 'exampleProperties': replacing [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为对于这种情况,你的方法过于复杂。典型的方法是使用
。 声明您在一个地方
,并在控制器中使用它的属性,我不认为以这种方式使用 @ImportResource 是一个好主意,它违反了依赖注入原则 - 这些属性是上下文的一部分您的控制器正在其中工作,因此控制器不应该知道它们是如何加载的。
I think your approach is overcomplicated for this case. The typical approach is to use
<context:property-placeholder>
. You declarein a single place, and use its properties in controllers as
Also I don't think it's a good idea to use
@ImportResource
this way, it violates Dependency Injection principle - these properties are parts of context in which your controllers are working, therefore controllers shouldn't know how they are loaded.