springboot去掉内置tomcat使用外部tomcat容器


最近springboot项目想去掉内置的tomcat,进而使用外置的tomcat容器进行开发和部署,这边我分享下。

(1)启动入口部分Application

public class XXXXXXApplication
        extends SpringBootServletInitializer
{
//原来的入口注掉
//	public static void reg(String[] args) {
//		ConfigurableApplicationContext context = SpringApplication.run(SepaBeanApplication.class, args);
//		SpringUtil.setApplicationContext(context);
//	}
	@Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(SepaBeanApplication.class);
   }
    @Override
    protected WebApplicationContext run(SpringApplication application) {
        SpringUtil.setApplicationContext(application.run(new String[0]));
       return (WebApplicationContext)SpringUtil.getApplicationContext();
    }
}

(2)parent的pom文件打包部分

<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>*.*</include>
					<include>**/*.*</include>
				</includes>
				<excludes>
					<exclude>*.*.bak</exclude>
					<exclude>**/*.bak</exclude>
				</excludes>
				<filtering>false</filtering>
			</resource>
		</resources>


		<plugins>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<executions>
					<execution>
						<id>attach-sources</id>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>

(3)需要打包的web的pom文件部分
改成打war包即可

    <artifactId>SepaBean-Web</artifactId>
    <packaging>war</packaging>

(4)如果使用了log4j作为日志输出的
也需要修改log4j的properties文件

log4j.rootLogger=debug, stdout, logFile, errorFile
#控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n
log4j.appender.CONSOLE.Encoding=UTF-8


#日志文件输出  超过1M产生新log文件
log4j.appender.logFile=org.apache.log4j.RollingFileAppender
#log4j.appender.logFile.File=${catalina.base}/logs/log.log
log4j.appender.logFile.File=../logs/log.log
log4j.appender.logFile.MaxFileSize=20000KB
log4j.appender.logFile.MaxBackupIndex=10
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
log4j.appender.ROLLING_FILE.Encoding=UTF-8

#报错输出
log4j.appender.errorFile=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.errorFile.File=${catalina.base}/logs/error.log
log4j.appender.errorFile.File=../logs/error.log
log4j.appender.errorFile.Append = true
log4j.appender.errorFile.Threshold = ERROR
log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
log4j.appender.DAILY_ROLLING_FILE.Encoding=UTF-8
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐