从 spring 配置中引用一个值 [英] Referencing a value from a spring config

查看:120
本文介绍了从 spring 配置中引用一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先是一些设置信息:

我有一个基于多租户 Spring 的应用程序.多租户支持库是我必须使用的内部开发工具.它的工作原理是在应用程序的 servlet 前面设置了一个拦截器.当请求命中 servlet 时,它会加载一个租户特定的 spring 配置,用于在命中 servlet 的 url 上指定的租户所需的东西".

I have a multi-tenant spring based application. The multi-tenant enabling library is an in-house developed tool where I work that I have to use. How it works is that there is an interceptor that sets in front of the servlet for the application. Upon a request hitting the servlet it loads a tenant specific spring config for "stuff" needed for the tenant specified on the url hitting the servlet.

如上所述,以上只是一些背景知识.现在到问题/问题:

As stated, the above is just a bit of background. Now to the issue/question:

我想要做的是在加载的租户配置中创建一个值,我可以用它来注入我需要的地方.那么,有没有一种方法可以在 spring 配置中定义一个常量,然后在 java 代码中通过 @Value@Resource 引用它?

What I want to do is to create, in the tenant configuration that is loaded, a value that I can use to inject where I need. So, is there a way I can just define a constant in a spring config and then reference it via @Value or @Resource in java code?

它后面没有 bean 实现,它只是一个纯粹的键/值,我可以在我的应用程序中按名称引用它.所以,一些东西的效果:

There will be no bean implementation behind it, it would just be purely and only a key/value that I can reference where needed in my application by name. So, something to the effect of:

<bean name="MyIdentifier">
    <property name="theId" value="1001" />
</bean>

然后我可以做类似的事情吗?

And then can I do something like?

@Value{MyIdentifier.theId}
String theId;

并让 Spring 意识到并注入该值.问题是,做类似上面的事情 Spring 会抱怨 bean 没有实现.注意,没有为 bean 指定类.我想这样做的原因是每个租户配置文件都会包含这个 bean,但实际值会因租户而异.

And have Spring be aware of and inject the value. The problem is that doing something like above Spring complains there is no implementation for the bean. Notice, no class specified for the bean. The reason I want to do this is every tenant config file will contain this bean, but the actual value will vary per tenant.

在配置中是否有其他类型可用于执行此操作?如果是这样,配置中必须包含哪些架构?

Is there some other type to use in the config to do this? If so, what schemas have to be on the config?

我猜我要么试图让 Spring 做一些不想要的事情,要么,这太简单了,因为我盯着它看得太久了,我看不到它.无论如何,谢谢你的帮助.

I am guessing I am either trying to make Spring do something not intended, or, this is so simple I cannot see it since I have stared at it too long. Anyway, thanks for the help.

推荐答案

如果不提供类实现,则无法在配置文件中创建 bean 标记.如果你想注入字段的值,你必须去属性文件.

You can not create bean tag in configuration file without providing class implementation. If you want to inject the value of fields, you have to go for properties file instead.

创建属性文件如下:

application.properties

theId=1001

在您的配置中加载属性文件:

Load property file in your configuration:

 <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
          <list>
            <value>classpath:application.properties</value>
          </list>
        </property>
      </bean>

并访问您的具体类中的属性:

And access the property in your concrete class:

  @Value("${theId}")
    String theId;

这篇关于从 spring 配置中引用一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆