我们正在组织中尝试从基于Weblogic的部署转移到基于Spring Boot的微服务。第一步,我试图创建一个Spring Boot应用程序,在这里我想将在weblogic中运行的Web应用程序(.war)作为应用程序加载到嵌入式tomcat。我想要一种配置,使运行在同一tomcat上的spring boot应用程序和webapp将通过JNDI查找共享数据源。
@Bean
public ServletWebServerFactory servletContainer() {
return new TomcatServletWebServerFactory() {
@Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs();
TomcatWebServer tomcatWebServer;
try {
tomcat.enableNaming();
Context context = tomcat.addWebapp("<context>", "<path to webapp>");
context.setParentClassLoader(getClass().getClassLoader());
WebappLoader loader =new WebappLoader(Thread.currentThread().getContextClassLoader());
context.setLoader(loader);
tomcatWebServer = super.getTomcatWebServer(tomcat);
Container[] children = tomcatWebServer.getTomcat().getHost().findChildren();
for (Container child : children) {
if (child instanceof Context) {
ClassLoader contextClassLoader =
((Context)child).getLoader().getClassLoader();
Thread.currentThread().setContextClassLoader(contextClassLoader);
break;
}
}
} catch (Exception ex) {
throw new IllegalStateException("Failed to add webapp", ex);
}
return tomcatWebServer;
}
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setAuth("Container");
resource.setName("*****");
resource.setType(DataSource.class.getName(`enter code here`));
resource.setProperty("driverClassName","*****");
resource.setProperty("url", "*****");
resource.setScope("Sharable");
resource.setProperty("password", "****");
resource.setProperty("username", "*****");
resource.setProperty("global", "****");
ContextResourceLink contextResourceLink = new ContextResourceLink();
contextResourceLink.setName("****");
contextResourceLink.setGlobal("*****");
contextResourceLink.setType(DataSource.class.getName());
context.getNamingResources().addResource(resource);
context.getNamingResources().addResourceLink(contextResourceLink);
}