Reputation: 953
My application is currently working fine on weblogic. But due to upgrade the application, I want to use spring boot and embedded tomcat. I have a JndiTemplate
bean such as:
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${java.naming.factory.initial}</prop>
<prop key="java.naming.provider.url">${java.naming.provider.url}</prop>
</props>
</property>
</bean>
This template is a helper to lookup for jndi objects like datasources and jms.
As you know the factory is weblogic.jndi.WLInitialContextFactory
and url is t3://SERVER:PORT
when we are on weblogic. but speaking of tomcat I dont know how to configure this template.
Upvotes: 2
Views: 4180
Reputation: 513
Tomcat is not a Java EE container like Weblogic. If you really want to keep using JNDI, have a look at TomEE.
Personally, I would let Spring manage the database connection. The advantage of using a framework like Spring is that it takes alot of responsibilities away from the container your application is running in, like eg. the database connection.
Spring Boot autoconfigures a DataSource by looking at:
application.yml
for the JDBC url and username/passwordpom.xml
to see which database driver it will use (Spring Boot can also derive that from your JDBC url, but you'll have to add the driver library)There's really nothing more to do.
You can now autowire the DataSource
or use Spring's JdbcTemplate
to avoid alot of boilerplate code.
Upvotes: 2
Reputation: 168
This can be configured using Config files using @Bean annotation to initialize the TomcatEmbeddedServletContainerFactory . Please see below code snippet which might be helpful. Below is one of the format that needs to be updated as per your need.
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
/* (non-Javadoc)
* @see org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory#postProcessContext(org.apache.catalina.Context)
*/
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setName("your_app_DS_name");
resource.setType("your_app_property");
resource.setProperty("driverClassName", "your_app_Drive");
resource.setProperty("factory", "your_app_factory_property_details");
//similarly configure other needed and dependent properties.
context.getNamingResources().addResource(resource);
}
};
}
Upvotes: 1