前言
之前在springcloud子项目打包后运行出现如下错误
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: GlobalConfigUtils setMetaData Fail ! Cause:com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: Get proxy targetObject exception ! Cause:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:640) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
... 56 common frames omitted
Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: GlobalConfigUtils setMetaData Fail ! Cause:com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: Get proxy targetObject exception ! Cause:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:51) ~[mybatis-plus-core-3.0.5.jar:na]
at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.buildSqlSessionFactory(MybatisSqlSessionFactoryBean.java:602) ~[mybatis-plus-extension-3.0.5.jar:na]
at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.afterPropertiesSet(MybatisSqlSessionFactoryBean.java:386) ~[mybatis-plus-extension-3.0.5.jar:na]
at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.getObject(MybatisSqlSessionFactoryBean.java:683) ~[mybatis-plus-extension-3.0.5.jar:na]
at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration.sqlSessionFactory(MybatisPlusAutoConfiguration.java:165) ~[mybatis-plus-boot-starter-3.0.5.jar:na]
at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration$$EnhancerBySpringCGLIB$$14d356cb.CGLIB$sqlSessionFactory$1(<generated>) ~[mybatis-plus-boot-starter-3.0.5.jar:na]
at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration$$EnhancerBySpringCGLIB$$14d356cb$$FastClassBySpringCGLIB$$a7c3e6b6.invoke(<generated>) ~[mybatis-plus-boot-starter-3.0.5.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration$$EnhancerBySpringCGLIB$$14d356cb.sqlSessionFactory(<generated>) ~[mybatis-plus-boot-starter-3.0.5.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_261]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_261]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_261]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_261]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
... 57 common frames omitted
具体是这个错误 网上说是数据源没有配置好,不过之前我的程序一直能够跑起来,且配置并没有出现问题。看了一下依赖也没有版本冲突或者依赖缺失。
解决
因为数据源的配置实在application.properties中的,所以怀疑是不是打包并没有将配置文件打包。 打包的target目录
可以看到并没有配置文件和mapper的xml文件被编译进来
网上搜寻说springboot打包插件,默认不编译src/main/java下面的xml文件,并且在打包时推荐指定resources目录下的文件,使得打包时不至于忽略配置文件。于是在pom.xml中指定build
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<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>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
再次打包发现class文件中已经有配置文件编译了进来 运行成功
扩展
1.springcloud中公共依赖模块的打包
springcloud中往往存在公共依赖模块,他们并不需要主类和main方法,但是未配置的springboot打包插件,在打包时会找main类,找不到就报错。解决这种问题很简单,配置maven打包时不找main类就可以了
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>NONE</layout>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
2.springcloud父打包带动子模块一块儿打包
微服务是有许多springboot服务构成的挨个打包十分的浪费时间,可以设置父项目打吧,其他子项目就跟着打包
方法很简单从父指定打包方式为pom
<packaging>pom</packaging>
然后在module中管理子项目
<modules>
<module>service</module>
<module>common</module>
</modules>
如果这些子项目还是其他项目的父级项目,那么依旧按上面的来配置,比如service就是其他项目的父项目,于是 service的pom.xml中有如下配置:
<packaging>pom</packaging>
<modules>
<module>service_oosb</module>
<module>service_vod</module>
<module>service_edu</module>
<module>service_cmss</module>
<module>service_msm</module>
<module>service_user</module>
<module>service_acl</module>
</modules>
这些配置声明了项目的结构关系。
有了这种配置,依赖传递和打包配置的传递都可以实现,于是可以实现总父项目配置一次,其他各级子项目就继承。 当然如果项目需要自定义配置也可以在自己的pom.xml中配置打包方式。于是只需要guli_parent的pom.xml配置一次这种配置即可
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
假如有个别项目需要自定义配置就可以自己在pom.xml中实现了,这个配置会覆盖父项目的配置。如公共依赖模块common_util需要自己自定义配置,只需要自己实现即可
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>NONE</layout>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
|