2010-10-02 104 views
1

1)修改$ CATALINA_HOME/conf/context.xml文件這一行的Tomcat 6自定義屬性資源

<Environment name="testValue" value="hello world" type="java.lang.String" override="true" /> 

2)在新的web應用程序的web.xml中添加此

<resource-env-ref> 
    <resource-env-ref-name>testValue</resource-env-ref-name> 
    <resource-env-ref-type>java.lang.String</resource-env-ref-type> 
</resource-env-ref> 

3 )retrive通過

Context ic = new InitialContext(); 
String value = (String)ic.lookup("testValue"); 
System.err.println("value = " + value); 

這個屬性得到以下錯誤:

javax.naming.NameNotFoundException: Name testValue is not bound in this Context 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:770) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:153) 
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152) 
    at javax.naming.InitialContext.lookup(InitialContext.java:392) 
    at it.skillbill.at.CustomProperties.contextInitialized(CustomProperties.java:18) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972) 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546) 
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:905) 
    at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:740) 
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500) 
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277) 
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321) 
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053) 
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:785) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) 
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) 
    at org.apache.catalina.core.StandardService.start(StandardService.java:519) 
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) 
    at org.apache.catalina.startup.Catalina.start(Catalina.java:581) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 

我只有一個問題: 爲什麼?

回答

1

我解決了這個問題,通過retrive這個屬性這一行:

(String)ic.lookup("java:comp/env/testValue"); 
3

你不需要「的resource-env-REF」在web.xml在這種情況下進入。當您想引用配置的資源(如DataSource)時使用此選項。如果您只需要配置一些環境屬性,您可以使用「環境」配置或者在web.xml中使用專用的「env-entry」。然後你可以通過使用「java:comp/env/testValue」