在控制器中注入属性值的最佳方法是什么?

发布于 2024-10-29 02:14:34 字数 1485 浏览 6 评论 0原文

这真的是从属性文件向控制器注入属性的最简单方法吗?然后,它需要在每个需要一些属性的控制器上导入属性文件内容。在像我这样的项目中,大约有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2024-11-05 02:14:34

我认为对于这种情况,你的方法过于复杂。典型的方法是使用 。 声明

<context:property-placeholder location = "classpath:/simple.properties" />

您在一个地方

private @Value("${simple.country}") String country;

,并在控制器中使用它的属性,我不认为以这种方式使用 @ImportResource 是一个好主意,它违反了依赖注入原则 - 这些属性是上下文的一部分您的控制器正在其中工作,因此控制器不应该知道它们是如何加载的。

I think your approach is overcomplicated for this case. The typical approach is to use <context:property-placeholder>. You declare

<context:property-placeholder location = "classpath:/simple.properties" />

in a single place, and use its properties in controllers as

private @Value("${simple.country}") String country;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文