如何在Spring Boot中使用嵌入式Tomcat容器创建JNDI上下文
import org.apache.catalina.Context;
import org.apache.catalina.deploy.ContextResource;
import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource("classpath:applicationContext.xml")
public class Application {
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder()
.showBanner(false)
.sources(Application.class)
.run(args);
}
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
};
}
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory = (TomcatEmbeddedServletContainerFactory) container;
tomcatEmbeddedServletContainerFactory.addContextCustomizers(new TomcatContextCustomizer() {
@Override
public void customize(Context context) {
ContextResource mydatasource = new ContextResource();
mydatasource.setName("jdbc/mydatasource");
mydatasource.setAuth("Container");
mydatasource.setType("javax.sql.DataSource");
mydatasource.setScope("Sharable");
mydatasource.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
mydatasource.setProperty("url", "jdbc:oracle:thin:@mydomain.com:1522:myid");
mydatasource.setProperty("username", "myusername");
mydatasource.setProperty("password", "mypassword");
context.getNamingResources().addResource(mydatasource);
}
});
}
}
};
}
}
我正在使用Spring Boot,并尝试使用为我的数据源创建JNDI上下文的嵌入式tomcat启动:
I'm using spring boot and trying to startup with an embedded tomcat that creates a JNDI context for my datasources:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-oracle</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
如果我删除@ImportResource,我的应用程序启动就很好了.我可以连接到tomcat实例.我可以检查所有执行器端点.使用JConsole,我可以连接到应用程序,并且可以在MBean中看到我的数据源(Catalina->资源->上下文->"/"-> localhost-> javax.sql.DataSource-> jdbc/mydatasource)
If I remove the @ImportResource my application starts up just fine. I can connect to the tomcat instance. I can check all of my actuator endpoints. Using JConsole, I can connect to the application I can see my datasource in the MBeans (Catalina -> Resource -> Context -> "/" -> localhost -> javax.sql.DataSource -> jdbc/mydatasource)
我还通过JConsole在此处显示了MBean(Tomcat-> DataSource->/-> localhost-> javax.sql.DataSource-> jdbc/mydatasource)
I also have MBeans showing up, via JConsole, here (Tomcat -> DataSource -> / -> localhost -> javax.sql.DataSource -> jdbc/mydatasource)
但是,当我@ImportResource时,实际上是通过JNDI查找mydatasource的,找不到它.
However, when I @ImportResource what is actually looking for mydatasource via JNDI, it's not finding it.
<bean id="myDS" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mydatasource"/>
</bean>
我导入的xml文件的相关部分
我上面配置的ContextResource具有与我在将应用程序部署到tomcat容器中时要部署的context.xml中使用的参数完全相同的参数.将导入的Bean和应用程序部署到tomcat容器后,它们可以正常工作.
The ContextResource that I'm configuring above is with the exact same parameters that I was using in the context.xml that is getting deployed when the application is deployed to a tomcat container. My imported beans and my application are working properly when deployed to a tomcat container.
所以看来我现在有一个上下文,但似乎命名不正确.我尝试了资源名称的各种组合,但似乎无法在这种情况下生成"comp"绑定.
So it appears that I have a context now, but it doesn't appear that the naming is right. I've tried to various combinations of the resource name, but can't seem to generate a "comp" bound in this context.
Caused by: javax.naming.NameNotFoundException: Name [comp/env/jdbc/mydatasource] is not bound in this Context. Unable to find [comp].
at org.apache.naming.NamingContext.lookup(NamingContext.java:819)
at org.apache.naming.NamingContext.lookup(NamingContext.java:167)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:156)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)
at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:231)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:217)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 30 more
默认情况下,嵌入式Tomcat中的JNDI被禁用,这导致了NoInitialContextException
.您需要致电 Tomcat.enableNaming()
启用它.最简单的方法是使用TomcatEmbeddedServletContainer
子类:
By default, JNDI is disabled in embedded Tomcat which is causing the NoInitialContextException
. You need to call Tomcat.enableNaming()
to enable it. The easiest way to do that is with a TomcatEmbeddedServletContainer
subclass:
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
};
}
如果采用这种方法,还可以通过覆盖TomcatEmbeddedServletContainerFactory
子类中的postProcessContext
方法来在JNDI中注册DataSource
.
If you take this approach, you can also register the DataSource
in JNDI by overriding the postProcessContext
method in your TomcatEmbeddedServletContainerFactory
subclass.
context.getNamingResources().addResource
将资源添加到java:comp/env
上下文中,因此资源的名称应为jdbc/mydatasource
而不是java:comp/env/mydatasource
.
context.getNamingResources().addResource
adds the resource to the java:comp/env
context so the resource's name should be jdbc/mydatasource
not java:comp/env/mydatasource
.
Tomcat使用线程上下文类加载器来确定应针对哪个JNDI上下文执行查找.您将资源绑定到Web应用程序的JNDI上下文中,因此需要确保在Web应用程序的类加载器是线程上下文类加载器时执行查找.您应该可以通过将jndiObjectFactoryBean
上的lookupOnStartup
设置为false
来实现此目的.您还需要将expectedType
设置为javax.sql.DataSource
:
Tomcat uses the thread context class loader to determine which JNDI context a lookup should be performed against. You're binding the resource into the web app's JNDI context so you need to ensure that the lookup is performed when the web app's class loader is the thread context class loader. You should be able to achieve this by setting lookupOnStartup
to false
on the jndiObjectFactoryBean
. You'll also need to set expectedType
to javax.sql.DataSource
:
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mydatasource"/>
<property name="expectedType" value="javax.sql.DataSource"/>
<property name="lookupOnStartup" value="false"/>
</bean>
这将为DataSource创建代理,并在首次使用时而不是在应用程序上下文启动期间执行实际的JNDI查找.
This will create a proxy for the DataSource with the actual JNDI lookup being performed on first use rather than during application context startup.
上述方法在此Spring Boot示例中进行了说明.