Springboot问题集

1.jar包方式和Eclipse Tomcat方式都可以正常运行,打成War包就无法运行了

JAR包方式和Eclipse Tomcat方式,@SpringBootApplication必须有main方法,且pom.xml,<packaging>jar</packaging>,另外还需要配置一个springboot专用打jar包插件,缺失,运行时会提示【找不到主清单属性】(若需要以jar包方式运行,插件不可缺少)

<build>
		<outputDirectory>target/classes</outputDirectory>
		<finalName>demo</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

如果需要在开发中可以DEBUG模式,需要下面的依赖,<scope>provided</scope>很重要,如果不加,在jar,eclipse中没问题,但是war包方式就会报错了

<!-- 开发模式-热加载 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>provided</scope>
		</dependency>

 另附上<scope>解释,对于scope=compile的情况(默认scope),也就是说这个项目在编译,测试,运行阶段都需要这个artifact对应的jar包在classpath中。而scope=provided则表明只在编译测试使用,运行时会排除掉,即不加入classpath:/lib中

2.war包方式无法运行

原因是pom.xml中配置的打包插件是

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

打成war包一定要使用下面插件,且该插件可和spring-boot-maven-plugin同时存在

            <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>

这里贴一下打成WAR包 的其他步骤

(1)<packaging>war</packaging>

(2)exclusion元素不是必须的,看个人自己的环境吧,我的引入了太多东西,LOG4J跟logback冲突了,所以要排除掉,选择使用log4j的话需要加入log4j配置文件

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>ch.qos.logback</groupId>
					<artifactId>logback-classic</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
<!--声明spring boot内嵌tomcat的作用范围 在运行时不起作用 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

(3)springboot主类需要实现SpringBootServletInitializer,并重写configure方法,其实都固定代码了,copy完事。

@ComponentScan("com.xxx.**")
@SpringBootApplication
public class MyApp  extends SpringBootServletInitializer
{
	private static Class< MyApp > applicationClass = MyApp;
	
	public static void main(String[] args) throws Exception {
		
		SpringApplication.run(MyApp, args);
	}
	
	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(applicationClass);
    }
	
	@Bean
	public CharacterEncodingFilter initializeCharacterEncodingFilter() {
		CharacterEncodingFilter filter = new CharacterEncodingFilter();
		filter.setEncoding("UTF-8");
		filter.setForceEncoding(true);
		return filter;
	}
}

4.war方式运行时,如果发现控制台运行了2次项目,肯定是配置没配对了,优先检查那个打包插件是不是配置对了,我遇到的就是配置了spring-boot-maven-plugin,然后以war方式跑,发现很多错误,其中一个就是发现BOOT的启动标志出现了2次。

最后贴出DEMO的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.jianlejun</groupId>
	<artifactId>demo</artifactId>
	<version>1.1.0</version>
    <!--改成jar即可以jar包方式运行-->
	<packaging>war</packaging>

	<name>mydemo</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
	</parent>

	<dependencies>
		<!--web支持 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>ch.qos.logback</groupId>
					<artifactId>logback-classic</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!--jsp页面使用jstl标签 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
		<!--用于编译jsp -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<!--<scope>provided</scope> -->
		</dependency>
		<!--声明spring boot内嵌tomcat的作用范围 在运行时不起作用 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>
		</dependency>
		<dependency>
			<groupId>com.github.ulisesbocchio</groupId>
			<artifactId>jasypt-spring-boot-starter</artifactId>
			<version>1.14</version>
		</dependency>
		<!-- 开发模式-热加载 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- spring-data-jpa -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.dozer</groupId>
			<artifactId>dozer</artifactId>
			<version>5.4.0</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.11</version>
		</dependency>
	</dependencies>

	<build>
		<outputDirectory>target/classes</outputDirectory>
		<finalName>demo</finalName>
<!--JAR方式不可缺少-->
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
<!--WAR方式不可缺少-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<!-- 设置阿里云镜像 -->
	<repositories>
		<repository>
			<id>aliyun-repository</id>
			<url>http://maven.aliyun.com/nexus/content/groups/public//</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
				<updatePolicy>always</updatePolicy>
			</snapshots>
		</repository>
	</repositories>
</project>

然后是springboot主启动类,爬楼2-(3),这样配置好,就三种方式都能正常运行了,不会出现jar包方式可以,war包方式不可以,反之亦然,

(完)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值