简体   繁体   中英

How to insert JNDI-name dynamic into Spring

My problem starts here. I have a rest service and I want to lookup remote-ejbs. I am using Jersey and Spring as frameworks for that. My actual approache works but, it is not dynamic enough.

I defined in my context something like that:

<jee:remote-slsb id="myServiceBean" 
                 jndi-name="ejb/ServiceBean" 
                 business-interface="..." 
                 >
      <jee:environment>
      ...
      </jee:environment>
</jee:remote-slsb>

I would like to do something like

<jee:remote-slsb id="myServiceBean" 
                     jndi-name="${ejb.ServiceBean.JNDI_NAME}" 
                     business-interface="..." 
                     >
    <jee:environment>
    ...
    </jee:environment>
</jee:remote-slsb>

I need to do that because the JNDI_NAME can change because of the versions.

ServiceBean{
     public static String JNDI_NAME = ejb/2.1/ServiceBean
}

I was thinking to do this by writing Java-Code which replace the value when i build my Project with maven. But that feels not right.

So the generic question would be how can i get a String from a Java class into an xml when building a project with maven.

Use maven-resources-plugin and Filtering :

src/main/config
    +dev
        +app.properties
    +int
        +app.properties

src/main/resources
    +context.xml

app.properties (dev)
    +ejb.ServiceBean.JNDI_NAME=XXX
app.properties (int)
    +ejb.ServiceBean.JNDI_NAME=YYY

In context.xml
<jee:remote-slsb id="myServiceBean" 
                     jndi-name="${ejb.ServiceBean.JNDI_NAME}" 
                     business-interface="..." 
                     >
<jee:environment>

In pom.xml

<properties> 
    <env_dyn>dev</env_dyn> 
...
... 
<build>
    <resources> 
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
    </resources> 
    <filters>
            <filter>src/main/config/${env_dyn}/app.properties</filter>
    </filters>
...
...

mvn clean install=> ejb.ServiceBean.JNDI_NAME=XXX in context.xml because default properties>env_dyn=dev     
mvn clean install -Denv_dyn=dev => ejb.ServiceBean.JNDI_NAME=XXX in context.xml
mvn clean install -Denv_dyn=rec => ejb.ServiceBean.JNDI_NAME=YYY in context.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM