0

I have a Java EE datasource which I just want to be invoked if a property is set. Can a system property or vm argument be set so as to just use this Java EE datasource if its true. Spring attempts to connect to this jndi datasource on server startup but I need to control when this connection is made. So in certain situations Spring should not attempt to make a connection to datasource "myDataSource" . Here is the config within my context file :

<jee:jndi-lookup id="myDataSource" jndi-name="jdbc/mydb"
        resource-ref="true" expected-type="javax.sql.DataSource" />

Can I use spring expressions to control this :

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html

Reading the accepted answer here :

Conditional statement inside Spring config I may be able to use a vm argument to indicate if the jndi datasource should be used ?

3 Answers 3

1

I think what you want is spring profiles. They're used like this:

<beans profile="dev">
    <!-- ... -->
</beans>
<beans profile="prod">
    <jee:jndi-lookup id="myDataSource" jndi-name="jdbc/mydb" />
</beans>

And then you enable one or more profiles with system properties: -Dspring.profiles.active=dev. This became available in spring 3.1.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like a good case for Spring profiles.

You need to put your <jee:jndi-lookup> into a profile and activate it only when you need it.

Then you can either use a built-in system property (spring.profiles.active) to activate necessary profiles, or activate them manually when initializing application context based on presence of some custom property (if you have a web application you'll need to use ApplicationContextInitializer to customize application context initialization).

Comments

1

One approach could be to use the Spring bean profiles, set your datasource bean to be activated when a specific profile is active, this way:

<beans profile="myprofile">
<jee:jndi-lookup id="myDataSource" jndi-name="jdbc/mydb"
        resource-ref="true" expected-type="javax.sql.DataSource" />
</beans>

There are multiple ways to activate this profile, if you have a webapplication you can activate it this way:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>myprofile</param-value>
    </init-param>
</servlet>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.