欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot报错: Error creating bean with name freeMarkerConfigurer defined in class path resource

程序员文章站 2022-05-19 10:01:23
...

今天写代码没有看黄历,加了点代码,项目运行不起来,项目报错如下所示:

2020-05-25 10:00:03.913 - [ERROR] - [org.springframework.boot.SpringApplication:reportFailure:858] - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'freeMarkerConfigurer' defined in class path resource [org/springframework/boot/autoconfigure/freemarker/FreeMarkerServletWebConfiguration.class]: Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
        at com.yueniu.yngp.YngpServer.main(YngpServer.java:34)
        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.tanukisoftware.wrapper.WrapperSimpleApp.run(WrapperSimpleApp.java:240)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ExceptionInInitializerError: null
        at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.newConfiguration(FreeMarkerConfigurationFactory.java:327)
        at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.createConfiguration(FreeMarkerConfigurationFactory.java:257)
                                                                     

度娘发现都是说jar包版本过低,需要升级freemaker的版本。可是我已经是比较高的版本。看来问题不是出在这里。然后使用谷歌搜索,发现:
SpringBoot报错: Error creating bean with name freeMarkerConfigurer defined in class path resource
解决方案有两个:

1:在配置文件中,设置:spring.freemarker.check-template-location=false

可是我并不是使用freemaker做页面处理的。所以这种解决方案pass掉。

2:在自动注解中过滤到freemakerConfiguration这个类就可以。

具体解决:在你的spring boot启动类中添加:

@EnableAutoConfiguration(exclude = { FreeMarkerAutoConfiguration.class })

即可。这样就过滤掉了。

重启启动项目,正常访问了。

总结:

这个问题主要是因为spring boot的@EnableAutoConfiguration注解引起的。

这是我得微信公众号:程序猿微刊 更多文章请关注微信公众号
SpringBoot报错: Error creating bean with name freeMarkerConfigurer defined in class path resource