springboot在tomcat中的兼容性很好,可是若是要把Springboot項目發佈在weblogic,尤爲是老版本的Weblogic就會出現各類問題。通過本人的不懈努力及查詢資料,終於將Springboot在weblogic中完美運行,因此記錄一下,也給你們一個參考。java
本文環境Springboot 1.5.21 Weblogic版本爲10.3.6 JDK爲1.7。git
1.首先要修改項目中pom.xml github
<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>
這兩個依賴重點講一下,由於是在Weblogic中發佈,因此要去除調spring-boot-start-web中tomcat的部分,另一個是增長對Servlet2.5的支持依賴,由於springboot基於servlet3.1,而weblogic10.3.6z只能支持到servlet2.5,爲了可以讓springboot支持Servlet2.5,因此要添加該依賴。具體看下該項目在gitHub中的說明。具體能夠參看下這個地址:https://github.com/dsyer/spring-boot-legacyweb
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 projectspring
2.項目中要添加web.xml 及weblogic.xmltomcat
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包衝突。springboot
3.修改Springboot的啓動類。oracle
@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.部署發佈項目warapp
通過上面這幾個配置的修改,就能夠把項目經過Maven 打包後發佈在Weblogic裏面了。
ide
PS:項目的發佈報錯有時候不必定是代碼的問題,還有多是JDK版本的問題,除此以外,引入的jar包對JDK的要求也是不同的,必定要保持這三者的JDK環境要求一致。