Java 当在Spring上下文(JNDI)中定义属性占位符时,幕后会发生什么?

Java 当在Spring上下文(JNDI)中定义属性占位符时,幕后会发生什么?,java,spring,tomcat,dependency-injection,jndi,Java,Spring,Tomcat,Dependency Injection,Jndi,我有一个使用Spring和JSF的Java web应用程序,部署在Tomcat中。 对于记录,在web.xml中,仅配置了Facesservlet;没有SpringServlet 我的Spring上下文的入口点是applicationContext.xml,如下所示: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmln

我有一个使用Spring和JSF的Java web应用程序,部署在Tomcat中。 对于记录,在
web.xml
中,仅配置了Facesservlet;没有SpringServlet

我的Spring上下文的入口点是
applicationContext.xml
,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <context:component-scan base-package="my.pack" annotation-config="true" />
    <context:property-placeholder location="file:${admin.config.path}/database.properties"/>

    <context:spring-configured />
    <context:annotation-config />
    <aop:aspectj-autoproxy />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/cntxt">
    <Environment name="admin.config.path" value="d:/conf/"
         type="java.lang.String" override="false"/>
</Context>
@Value("${java:comp/env/admin.config.path}")
private String confDir;
我还在我的一个Spring托管bean中注入了这个环境变量,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <context:component-scan base-package="my.pack" annotation-config="true" />
    <context:property-placeholder location="file:${admin.config.path}/database.properties"/>

    <context:spring-configured />
    <context:annotation-config />
    <aop:aspectj-autoproxy />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/cntxt">
    <Environment name="admin.config.path" value="d:/conf/"
         type="java.lang.String" override="false"/>
</Context>
@Value("${java:comp/env/admin.config.path}")
private String confDir;
(这是因为我还将该目录用于其他一些配置文件,这些文件不是
.properties
文件)

通过上面的配置,一切正常-所有的布线都没有任何缺陷

但是,在某个时刻,我不再需要通过
database.properties
中的属性占位符configurer注入属性(但我仍然需要
admin.config.path
中的其他文件),因此我从
applicationContext.xml
中删除了
行。 此时,使用
@Value
的注入在我的Spring管理bean中停止工作

有人知道到底发生了什么吗?如果未在Spring上下文中定义属性占位符配置器,JNDI注入似乎会失败。
是否有任何方法可以重新启用此类注入(无需定义指向空属性文件的虚拟属性占位符)?

您需要一个属性占位符配置器实例,其唯一目的是用实际值替换占位符。如果删除它,则不会替换占位符

添加
并省去
位置
字段,或者添加类型为
属性资源占位符配置器
的bean。任何一种方式都将重新启用占位符支持

小建议删除属性的
java:comp/env
部分,默认情况下,如果无法在其他位置(servlet上下文、属性、系统属性等)解析该属性,它将执行JNDI查找