SpringBoot项目在idea中运行正常,按照网上的方式打成jar包后在控制台运行报错。
第一个错误:
2018-09-06 09:38:35,222 ERROR (SpringApplication.java:833)- Application run fail
ed
java.lang.IllegalStateException: Error processing condition on org.springframewo
rk.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration.propertySourc
esPlaceholderConfigurer
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.
matches(SpringBootCondition.java:64)
(省略无数行………………………………………………………………)
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(Ref
lectionUtils.java:599)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:728)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.determineTargetType(AbstractAutowireCapableBeanFactory.java:669)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.predictBeanType(AbstractAutowireCapableBeanFactory.java:637)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFacto
ryBean(AbstractBeanFactory.java:1489)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFacto
ryBean(AbstractBeanFactory.java:1007)
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry.add
BeanTypeForNonAliasDefinition(BeanTypeRegistry.java:168)
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry.add
BeanType(BeanTypeRegistry.java:157)
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry.upd
ateTypesIfNecessary(BeanTypeRegistry.java:207)
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry.get
NamesForType(BeanTypeRegistry.java:114)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.coll
ectBeanNamesForType(OnBeanCondition.java:266)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getB
eanNamesForType(OnBeanCondition.java:255)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getM
atchingBeans(OnBeanCondition.java:197)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getM
atchOutcome(OnBeanCondition.java:116)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.
matches(SpringBootCondition.java:47)
... 22 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSessionF
actory
at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.base/java.lang.Class.getDeclaredMethods(Unknown Source)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(Reflectio
nUtils.java:641)
... 39 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.ibatis.session.SqlSessio
nFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown So
urce)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(U
nknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 43 common frames omitted
2018-09-06 09:38:35,226 INFO (AbstractApplicationContext.java:989)- Closing org.
springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplica
tionContext@14c00fc7: startup date [Thu Sep 06 09:38:34 CST 2018]; root of conte
xt hierarchy
经分析,错误关键出在红字部分。解决方法:将porm.xml中
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
version改为1.3.0
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
第二个错误:
Error starting ApplicationContext. To display the conditions report re-run your
application with 'debug' enabled.
2018-09-06 09:48:45,012 ERROR (SpringApplication.java:833)- Application run fail
ed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
bean with name 'userManageController': Unsatisfied dependency expressed through
field 'iUserService'; nested exception is org.springframework.beans.factory.Uns
atisfiedDependencyException: Error creating bean with name 'iUserService': Unsat
isfied dependency expressed through field 'userMapper'; nested exception is org.
springframework.beans.factory.UnsatisfiedDependencyException: Error creating bea
n with name 'userMapper' defined in URL [jar:file:/D:/Ideaworkspace/TicketManage
ment/classes/artifacts/ticketmanagement_jar/ticketmanagement.jar!/com/genequ/tic
ketmanagement/mapper/UserMapper.class]: Unsatisfied dependency expressed through
bean property 'sqlSessionFactory'; nested exception is org.springframework.bean
s.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactor
y' defined in class path resource [com/genequ/ticketmanagement/config/MyBatisCon
fig.class]: Bean instantiation via factory method failed; nested exception is or
g.springframework.beans.BeanInstantiationException: Failed to instantiate [org.a
pache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactoryBean'
threw exception; nested exception is java.lang.NoSuchMethodError: org.apache.iba
tis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanP
ostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.j
ava:587)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject
(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanP
ostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java
:373)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$
doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
y.getSingleton(DefaultSingletonBeanRegistry.java:228)
(省略无数行……………………………………………………………………………………)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanP
ostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.j
ava:584)
... 24 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Err
or creating bean with name 'userMapper' defined in URL [jar:file:/D:/Ideaworkspa
ce/TicketManagement/classes/artifacts/ticketmanagement_jar/ticketmanagement.jar!
/com/genequ/ticketmanagement/mapper/UserMapper.class]: Unsatisfied dependency ex
pressed through bean property 'sqlSessionFactory'; nested exception is org.sprin
gframework.beans.factory.BeanCreationException: Error creating bean with name 's
qlSessionFactory' defined in class path resource [com/genequ/ticketmanagement/co
nfig/MyBatisConfig.class]: Bean instantiation via factory method failed; nested
exception is org.springframework.beans.BeanInstantiationException: Failed to ins
tantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessi
onFactoryBean' threw exception; nested exception is java.lang.NoSuchMethodError:
org.apache.ibatis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.autowireByType(AbstractAutowireCapableBeanFactory.java:1439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.populateBean(AbstractAutowireCapableBeanFactory.java:1326)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$
doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
y.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
an(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.config.DependencyDescriptor.resolve
Candidate(DependencyDescriptor.java:251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanP
ostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.j
ava:584)
... 37 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creati
ng bean with name 'sqlSessionFactory' defined in class path resource [com/genequ
/ticketmanagement/config/MyBatisConfig.class]: Bean instantiation via factory me
thod failed; nested exception is org.springframework.beans.BeanInstantiationExce
ption: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Fact
ory method 'sqlSessionFactoryBean' threw exception; nested exception is java.lan
g.NoSuchMethodError: org.apache.ibatis.session.Configuration.setVfsImpl(Ljava/la
ng/Class;)V
at org.springframework.beans.factory.support.ConstructorResolver.instant
iateUsingFactoryMethod(ConstructorResolver.java:587)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:12
50)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$
doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
y.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
an(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.config.DependencyDescriptor.resolve
Candidate(DependencyDescriptor.java:251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.autowireByType(AbstractAutowireCapableBeanFactory.java:1424)
... 48 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to insta
ntiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSession
FactoryBean' threw exception; nested exception is java.lang.NoSuchMethodError: o
rg.apache.ibatis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V
at org.springframework.beans.factory.support.SimpleInstantiationStrategy
.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instant
iateUsingFactoryMethod(ConstructorResolver.java:579)
... 60 common frames omitted
Caused by: java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.
setVfsImpl(Ljava/lang/Class;)V
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSe
ssionFactoryBean.java:427)
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessio
nFactoryBean.java:380)
at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryB
ean.java:547)
at com.genequ.ticketmanagement.config.MyBatisConfig.sqlSessionFactoryBea
n(MyBatisConfig.java:54)
at com.genequ.ticketmanagement.config.MyBatisConfig$$EnhancerBySpringCGL
IB$$248104f.CGLIB$sqlSessionFactoryBean$1(
at com.genequ.ticketmanagement.config.MyBatisConfig$$EnhancerBySpringCGL
IB$$248104f$$FastClassBySpringCGLIB$$64f36b4e.invoke(
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.j
ava:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$Bea
nMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
at com.genequ.ticketmanagement.config.MyBatisConfig$$EnhancerBySpringCGL
IB$$248104f.sqlSessionFactoryBean(
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Nativ
e Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknow
n Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Un
known Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy
.instantiate(SimpleInstantiationStrategy.java:154)
... 61 common frames omitted
这个错误关键在最后一个cause by。这个错误在网上查了很多资料,有的说和问题1解决方案一样。其实不是的。正确解决方案:
错误发生由于porm.xml中多了
org.apache.ibatis
ibatis-core
3.0
在output中多了这个jar包导致使用了iBatis的sqlSessionFactory这个bean,而这个bean没有setVfsImpl方法。
删除该段xml后使用正确的mybatis-spring-boot-starter中的sqlSessionFactoryBean这个bean,成功调用方法。
修改后记得调整打包的Artifacts。