Spring Boot 2.0 常见问题总结(一)

2019-08-12  本文已影响0人  RookieMZL
   <build>
       <plugins>
         <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
       </plugins>
    </build>

如果没加相关依赖,执行 maven 打包,运行后会报错: no main manifest attribute, in XXX.jar。

原因解释:
将 jar 文件文件后缀改为 zip 解压后得到目录如下:

 example.jar
             |
             +-META-INF
             |  +-MANIFEST.MF
             +-org
             |  +-springframework
             |     +-boot
             |        +-loader
             |           +-<spring boot loader classes>
             +-BOOT-INF
                +-classes
                |  +-mycompany
                |     +-project
                |        +-YourClasses.class
                +-lib
                   +-dependency1.jar
                   +-dependency2.jar

当没有引用 maven 插件的时候,就没有生成的 MANIFEST.MF 文件,而在 MANIFEST.MF 中有 Main-Class: org.springframework.boot.loader.JarLauncher,这会启动类加载器,类加载器会加载应用的主要函数,即执行 Start-Class: com.rookie.BaseProjectApplication,这就是程序入口。运行后会报错:no main manifest attribute, in XXX.jar,就是找不到 MANIFEST.MF 的 Start-Class: xx.xx.xxApplication。入口函数都找不到,你说如何启动加载呢?肯定会加载失败的。

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency></pre>
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1</pre>

原因是:创建 Spring Boot 项目时,在选择组件时添加了 mysql、mybatis 组件,添加了数据库组件,所以 autoconfig 会去读取数据源配置,而新建的项目还没有配置数据源,所以会导致异常出现。

解决方案:
①:需要在启动类的 @EnableAutoConfiguration 或 @SpringBootApplication 中添加 exclude = {DataSourceAutoConfiguration.class},排除此类的autoconfig。

②:添加数据库配置信息
spring.datasource.jdbc-url=jdbc:mysql://localhost:3316/test1
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver</pre>

上一篇下一篇

猜你喜欢

热点阅读