最近在搭建框架时遇到很多细节问题,时间久了就很容易忘记,在此记录一下。
1.问题:Warning:java: 来自注释处理程序 'org.antlr.v4.runtime.misc.NullUsageProcessor' 的受支持 source 版本 'RELEASE_6' 低于 -source '1.8'
解决办法:这个问题是依赖包版本导致,更新依赖包即可。
<dependency> <groupId>org.antlr</groupId> <artifactId>antlr4</artifactId> <version>4.7.2</version> </dependency>
2.问题:Warning:java: 未知的枚举常量 javax.annotation.meta.When.MAYBE
解决办法:原因是找不到默认的 javax.annotation.meta.When的类文件,缺少对应第三方依赖包,添加对应依赖包即可。
<dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>annotations</artifactId> <version>3.0.1</version> </dependency>
3.问题:在构建springboot的时候,内部报了一个错误信息,但是不影响正常使用。
logback [main] INFO o.h.e.j.e.i.LobCreatorBuilderImpl - HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException: null at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl.useContextualLobCreation(LobCreatorBuilderImpl.java:113) at org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl.makeLobCreatorBuilder(LobCreatorBuilderImpl.java:54) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.<init>(JdbcEnvironmentImpl.java:271) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:114) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:861) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888) at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1753) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1690) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1087) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) at com.digitized.server.DigitizedServerApplication.main(DigitizedServerApplication.java:14) Caused by: java.sql.SQLFeatureNotSupportedException: 这个 org.postgresql.jdbc.PgConnection.createClob() 方法尚未被实作。 at org.postgresql.Driver.notImplemented(Driver.java:640) at org.postgresql.jdbc.PgConnection.createClob(PgConnection.java:1241) ... 44 common frames omitted
解决办法:添加如下配置信息,原因是数据库默认使用的解析方式不匹配,这里是连接greenplum数据时出现的错误。
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
4.问题:用 java -jar 运行spring boot jar包时报没有主清单属性错误。
解决办法:查看jar包内部META-INF目录下的MANIFEST.MF文件里面是否包含Main-Class 配置。如果没有则设置打包配置。
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 指定该Main Class为全局的唯一入口 --> <mainClass>com.zsm.apidoc.ApidocApplication</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中--> </goals> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>*.xml</include> <include>*.properties</include> </includes> <excludes> <exclude>*.log</exclude> <exclude>*.txt</exclude> </excludes> </resource> </resources> </build>
5.问题:SpringBoot扫描不到依赖项目的类。有一个公共项目common,要扫描的类在com.zsm.common包下,现在有一个业务项目com.zsm.User,依赖了公共项目common,在User项目Application启动类在包com.zsm.user下,项目启动后找不到common项目中的Bean类。
解决办法:在Application启动类添加注解 @ComponentScan(basePackages = {"com.zsm"}) 即可解决问题,或者配置扫描多个包@ComponentScan(basePackages= {“com.zsm.common”,”com.zsm.user”}),或者添加@SpringBootApplication注解,修改包路径。添加注解后,spring将只扫描注解配置的包,而不再自动扫描启动类所在包及子包下的文件。
注意:springboot 默认扫描的路径,是该工程application启动类所在包以及所有子包下的所有文件。如果不加@SpringBootApplication注解,spring将只扫描注解配置的包而不再自动扫描启动类所在包及子包下的文件。
6.问题:打包项目,报Liquibase缺少配置文件 db/changelog/db.changelog-master.yaml。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot find changelog location: class path resource [db/changelog/db.changelog-master.yaml] (please add changelog or check your Liquibase configuration) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:139) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE] Caused by: java.lang.IllegalStateException: Cannot find changelog location: class path resource [db/changelog/db.changelog-master.yaml] (please add changelog or check your Liquibase configuration) at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.0.11.RELEASE.jar:5.0.11.RELEASE] at org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration.checkChangelogExists(LiquibaseAutoConfiguration.java:113) ~[spring-boot-autoconfigure-2.0.7.RELEASE.jar:2.0.7.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:309) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE] ... 29 common frames omitted
解决办法:一、可以设置不需要liquibase,spring.liquibase.enabled=false 。 二、在资源文件中添加对应的配置文件信息,配置文件参考 http://www.liquibase.org/documentation/yaml_format.html 。
Liquibase简介
Liquibase是一个数据库表结构迭代演进变更的管理工具。开发人员可以不是直接针对某个特定的数据库编写SQL的创建、更新或删除数据库对象,而是在通过XML、YAML、JSON等文件中定义描述他们所需的数据库表结构的变更。这些变更可以包含tables、views、columns、indexes、foreignkeys、primarykeys、uniqueconstraints、data等。
Liquibase特性
- 不依赖特定数据库,像Hibernate一样支持几乎所有的主流数据库如MySql,Oracle,PostgreSQL,H2等等
- 支持数据库的操作非常丰富,如创建或修改表(table)、视图(views)、索引(index)以及数据(data)等
- 对数据库的变更描述支持多种格式XML、YAML、JSON等
- 可以记录数据库的变更历史,是通过创建特定的表结果来实现这个功能
- 可以 diff 两个数据库的差异
- 可以轻松的把比如mysql库导出其他支持数据库的sql脚本
- 支持java命令、Maven、Gradle、Spring Boot集成运行
- 提供了Eclipse和IDEA的数据库重构插件,但是多年未更新不推荐使用
Liquibase Maven依赖:
<dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> </dependency>