问题
I have a lot of Websphere servers with different JNDI name of my DB connection so I have to build many ears files for each server. I'd like to do something like this:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${SPECIFIC_JNDI_NAME}"/>
</bean>
How can I create this SPECIFIC_JNDI_NAME variable?
回答1:
The proper Java EE way to do it, is using resource references in your code like: java:comp/env/jdbc/myDSRef
, then this resource reference is bind to actual JNDI name during the installation process.
You either define references via @Resource
tag, or entry in the deployment descriptor (web.xml
or ejb-jar.xml
).
You map it to the JNDI name via admin console, wsadmin
installation script, or ibm-web-bnd.xml
file placed in the WEB-INF folder.
It is possible to use references with Spring.
回答2:
This is the wrong way to go about it. One advantage of JNDI is that you can bind objects (in this case a datasource) under one JNDI name without a care for where it came from, how it was instantiated, etc. as long as it was there at the time it was first accessed.
You (or whoever configures the JNDI names) are basically trying to take away that advantage by binding different datasources on different JNDI names.
A workaround could be to bind the 'custom' name to a 'standard' JNDI name such that your application can still refer to the 'standard' name and the onus for providing the right bean is on those who configure the JNDI but really, if you go that far you can also just give the datasource the standard name. Also, I'm not sure that is even possible in JNDI, I just know that it used to be possible in Spring's own configuration.
来源:https://stackoverflow.com/questions/31025654/passing-the-jndi-name-dynamically