Spring Jndi Context and PropertyPlaceholderConfigurer

前端 未结 4 847
鱼传尺愫
鱼传尺愫 2020-12-19 10:15

Using Spring, I want to read a variable inside the context of Webspehere.

Read a Environment Variable in Java with Websphere

To define the data.... inside we

4条回答
  •  醉梦人生
    2020-12-19 10:51

    If you just want to get the value of a variable that was defined in the container context and use it as a String without creating a placeholder object, you can do the following (this was tested in Tomcat but most likely works the same in other container / JEE servers such as WebSphere):

    Define the environment variable in Tomcat's context.xml (or use your own server's syntax) :

    
    

    In the Spring XML context file :

    Add the jee namespace to the root element :

    xmlns:jee="http://www.springframework.org/schema/jee"
    

    and in the the xsi:schemaLocation :

    http://www.springframework.org/schema/jee       http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    

    Now you can easily look up a value (note that you don't have to specify the java:/comp/env stuff, Spring does that for you):

    
    

    Then you can use it, for example pass it to the constructor of a bean as a reference :

    
        
    
    

    The bean will receive "hello" as its construcotr arg.

    EDIT :

    If you run your Spring context outside the container (Tomcat, Websphere...) for integration testing, the lookup will not work. So if you have a special test context, just add the following String definition that will overrides the jee:lookup and set the value you want to use for testing :

    
    
        
    
    

提交回复
热议问题