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