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環境要求一致。