springboot在tomcat中的兼容性很好,但是如果要把Springboot项目发布在weblogic,尤其是老版本的Weblogic就会出现各种问题。经过本人的不懈努力及查询资料,终于将Springboot在weblogic中完美运行,所以记录一下,也给大家一个参考。
本文环境Springboot 1.5.21 Weblogic版本为10.3.6 JDK为1.7。
1.首先要修改项目中pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-legacy</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
这两个依赖重点讲一下,因为是在Weblogic中发布,所以要去除调spring-boot-start-web中tomcat的部分。由于spring4.3X依赖servlet3.1,因此这个jar也是需要的。另外一个是增加对Servlet2.5的支持依赖,因为springboot基于servlet3.1,而weblogic10.3.6z只能支持到servlet2.5,为了能够让springboot支持Servlet2.5,所以要添加该依赖。具体看下该项目在gitHub中的说明。具体可以参看下这个地址:https://github.com/dsyer/spring-boot-legacy
Spring Boot is built on Servlet 3.1. Older servlet versions can be used with Spring Boot, but some workarounds are needed. This project is a library that gives you a start with those. There is a sample that is running on Google Appengine at http://dsyerboot.appspot.com/. Copy the
web.xml
from the sample to get started with your own project
2.项目中要添加web.xml 及weblogic.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
5
6 <context-param>
7 <param-name>contextConfigLocation</param-name>
8 <param-value>填写你的Application全路径</param-value>
9 </context-param>
10
11 <listener>
12 <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
13 </listener>
14
15
16 <servlet>
17 <servlet-name>appServlet</servlet-name>
18 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
19 <init-param>
20 <param-name>contextAttribute</param-name>
21 <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
22 </init-param>
23 <load-on-startup>1</load-on-startup>
24 </servlet>
25
26 <servlet-mapping>
27 <servlet-name>appServlet</servlet-name>
28 <url-pattern>/</url-pattern>
29 </servlet-mapping>
30
31 </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
<wls:context-root>/spring-boot-weblogic-app</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
这里解释下weblogic.xml里wls:prefer-application-packages的作用,就是说如果一个jar在项目里和weblogic里面同时存在,告诉weblogic去使用项目里面自带的jar包。就是为了防止jar包冲突。
3.修改Springboot的启动类。
@MapperScan("com.xxx.xxx.mapper")
@SpringBootApplication
public class NontaxPayableExchangeApplication extends SpringBootServletInitializer implements WebApplicationInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(NontaxPayableExchangeApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(NontaxPayableExchangeApplication.class, args);
}
}
4.部署发布项目war
经过上面这几个配置的修改,就可以把项目通过Maven 打包后发布在Weblogic里面了。
PS:项目的发布报错有时候不一定是代码的问题,还有可能是JDK版本的问题,除此之外,引入的jar包对JDK的要求也是不一样的,一定要保持这三者的JDK环境要求一致。