springboot打包war部署到weblogic,涉及Filter以及Filter中的@Value处理
基于Maven构建。
1:修改pom.xml配置文件
<packaging>war</packaging> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--排除内置tomcat--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 部署weblogic需要 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
2:修改Application
@ServletComponentScan @SpringBootApplication public class XtqmServerApplication extends SpringBootServletInitializer implements WebApplicationInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(XtqmServerApplication.class); } public static void main(String[] args) { SpringApplication.run(XtqmServerApplication.class, args); } }
3:Filter
@Component @WebFilter(filterName = "xtqmFilter", urlPatterns = {"/xtqm/*"}) public class XtqmFilter implements Filter { @Override public void init(FilterConfig filterConfig){} @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { } @Override public void destroy(){} }
(1)必须覆写init与destroy方法,否则部署到weblogic中会报抽象方法异常错误;
(2)在Filter中不能直接使用@Value获取配置文件中的变量信息,通过静态工具类的方式实现:
@Component public class SecretProperty { private static String secres; public static String getSecres() { return secres; } @Value("${jk.secret}") public void setSecres(String secres) { this.secres = secres; } }
4:weblogic.xml
<?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://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd"> <wls:container-descriptor> <wls:prefer-application-packages> <wls:package-name>org.slf4j</wls:package-name> </wls:prefer-application-packages> </wls:container-descriptor> <wls:context-root>/xxx</wls:context-root> </wls:weblogic-web-app>