使用属性的 JNDI 查找来确定 spring 导入

发布于 2024-12-22 13:17:31 字数 1207 浏览 1 评论 0原文

我正在尝试导入属性文件以确定我需要哪个导入。 我查看了以下内容,他们似乎没有确切的答案:

http://blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/ http://stackoverflow.com/questions/1520055/import-spring-config-file-based-on-property-in-properties-file

它们很接近,但不完全是我想要的。

<beans:bean id="propertiesResource" class="org.springframework.jndi.JndiObjectFactoryBean"
    p:jndiName="java:comp/env/url/resource/avcs" p:defaultObject="classpath:avcs.properties"/>

<beans:bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <beans:property name="location" ref="propertiesResource"/>
</beans:bean>

<context:property-placeholder properties-ref="propertiesResource" system-properties-mode="ENVIRONMENT"/>

<beans:import resource="applicationContext.${application.context.import}.xml"/>

因此,我需要使用 JNDI 查找来获取文件,然后加载属性,然后导入特定的应用程序上下文。 如果我仅将属性占位符与类路径资源一起使用,那么我相信一切都会好起来,但在这种情况下,我需要首先进行 JNDI 查找,并且优先顺序似乎将导入放在 JNDI 之前。

有没有人能给我答案?

我还有其他一些想法,但我只是想看看是否有人已经解决了这个问题。

提前致谢。

I am trying to import a properties file to determine which import I need.
I have looked at the following and they don't seem to have the exact answer:

http://blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/
http://stackoverflow.com/questions/1520055/import-spring-config-file-based-on-property-in-properties-file

they are close but not exactly what I'm looking for.

<beans:bean id="propertiesResource" class="org.springframework.jndi.JndiObjectFactoryBean"
    p:jndiName="java:comp/env/url/resource/avcs" p:defaultObject="classpath:avcs.properties"/>

<beans:bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <beans:property name="location" ref="propertiesResource"/>
</beans:bean>

<context:property-placeholder properties-ref="propertiesResource" system-properties-mode="ENVIRONMENT"/>

<beans:import resource="applicationContext.${application.context.import}.xml"/>

So I need to use the JNDI look-up to get the file and then load the properties then import the specific application context.
If I were to use the property-placeholder with just a class path resource then all would be well I believe but in this situation I need to do the JNDI look up first, and it seem the orfer of precedence puts the import before the JNDI.

Any chance anyone out there has an answer for me?

I have a few other ideas but I just wanted to see if there was anyone that might already have tackled this.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

假装爱人 2024-12-29 13:17:31

我相信你将不得不做这样的事情。我还没有测试过它,但基本上 PropertyPlaceholderConfigurer 中的 setLocations 方法接受一个 Resource 数组(在我们的例子中 UrlResource - http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/core/io/UrlResource.html),它又具有一个带有文件路径的构造函数。

<jee:jndi-lookup id="mypropsfile1" jndi-name="myPropsFile1" default-value="file:///C:/defaultPath" resource-ref="true"/>
<jee:jndi-lookup id="mypropsfile2" jndi-name="myPropsFile2" resource-ref="true"/>

<bean id="propertyConfigurer" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="mypropsfile1,mypropsfile2">
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
  <property name="locations">
        <list>
           <bean class="org.springframework.core.io.UrlResource">
               <constructor-arg><ref bean="mypropsfile1"/></constructor-arg>
           </bean>
           <bean class="org.springframework.core.io.UrlResource">
               <constructor-arg><ref bean="myPropsFile2"/></constructor-arg>
           </bean>
        </list>

  </property>
</bean>

此处 .依赖将帮助设置特定 bean 的依赖关系。

I believe you will have to do something like this . I haven't tested it but basically the setLocations method in PropertyPlaceholderConfigurer takes in an array of Resource(In our case UrlResource - http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/core/io/UrlResource.html) which in turn has a constructor with the file path .

<jee:jndi-lookup id="mypropsfile1" jndi-name="myPropsFile1" default-value="file:///C:/defaultPath" resource-ref="true"/>
<jee:jndi-lookup id="mypropsfile2" jndi-name="myPropsFile2" resource-ref="true"/>

<bean id="propertyConfigurer" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="mypropsfile1,mypropsfile2">
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
  <property name="locations">
        <list>
           <bean class="org.springframework.core.io.UrlResource">
               <constructor-arg><ref bean="mypropsfile1"/></constructor-arg>
           </bean>
           <bean class="org.springframework.core.io.UrlResource">
               <constructor-arg><ref bean="myPropsFile2"/></constructor-arg>
           </bean>
        </list>

  </property>
</bean>

Check for this discussion here . The depends-on will help set the dependencies for a particular bean .

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