0.问题列表
- 使用mock进行单元测试时报错:
java.lang.UnsupportedClassVersionError: chap1/Test01 : Unsupported major.minor version 52.0
- Spring+Junit4单元测试时遇到的错误:
Java.lang.ExceptionInInitializerError ....
- 学习XML装配Bean时单元测试又报错了:
Caused by: java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to load an ApplicationContext from [MergedContextConfiguration@69379752 testClass = HppyMusicTest, locations = ‘{}’, classes = ‘{}’, contextInitializerClasses = ‘[]’, activeProfiles = ‘{}’, propertySourceLocations = ‘{}’, propertySourceProperties = ‘{}’, contextCustomizers = set[[empty]], contextLoader = ‘org.springframework.test.context.support.DelegatingSmartContextLoader’, parent = [null]].
- Spring xmlsetting方式注入测试时又出错了:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘normalMusic’ defined in class path resource [chap2/musicplayer3.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [chap2.NormalMusic]: No default constructor found; nested exception is java.lang.NoSuchMethodException: chap2.NormalMusic.
() - 创建自定义的properties文件时报错:
Contens is not allowed prolog
- 学习Spring AOP时出错:
warning no match for this type name: name [Xlint:invalidAbsoluteTypeName]
- SpringMVC配置web.xml后启动Tomcat报错:
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
- 使用Spring表单标签库时出错:
The absolute uri: http://www.springframework.org/tags/form cannot be resolved in either web.xml or the jar files deployed with this application
- SpringMVC国际化过程中出错:
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'message' for locale 'zh_CN'.
1.问题1-5解决
1)使用mock进行单元测试时报错:
- 错误:
Unsupported major.minor version 52.0 ...一堆没用的信息
- 搜了一大堆网上的资料,没一个对的上的。
最后发现是maven的jdk版本问题。将maven的版本修改为jdk1.8就可以了。
52.0对应的jdk就是1.8,说明低版本不支持高版本。为了以后不报错,修改maven本地setting.xml文件配置如下:
详情参考maven学习笔记2。<profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>
2)Spring+Junit4测试时出错:
- 更多错误:
java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ...省略n行 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:102) ... 19 more
- 解决方案:
主要看那个course by:
将pom.xml中的Junit版本改为更高的即可:java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher
3)学习XML装配Bean时单元测试又报错了:
- 错误日志(只截取了course by那段):
Caused by: java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to load an ApplicationContext from [MergedContextConfiguration@69379752 testClass = HppyMusicTest, locations = ‘{}’, classes = ‘{}’, contextInitializerClasses = ‘[]’, activeProfiles = ‘{}’, propertySourceLocations = ‘{}’, propertySourceProperties = ‘{}’, contextCustomizers = set[[empty]], contextLoader = ‘org.springframework.test.context.support.DelegatingSmartContextLoader’, parent = [null]]. - 错误原因:
在测试类中使用了@RunWith(SpringJUnit4ClassRunner.class)
注解而未使用@ContextConfiguration()注解
,而使用手动加载上下文,这样子就会出错。 - 解决方法:
将@RunWith(SpringJUnit4ClassRunner.class)去除,不使用Spring提供的测试,手动加载xml配置文件的上下文:
或者将手动加载的去除,使用注解的locations属性自动加载上下文。AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MusicPlayer2Config.class);
4) Spring xml setting方式注入测试时又出错了:
- 错误代码(核心部分):
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘normalMusic’ defined in class path resource [chap2/musicplayer3.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [chap2.NormalMusic]: No default constructor found; nested exception is java.lang.NoSuchMethodException: chap2.NormalMusic.
() - 错误原因:
NormalMusic没有默认的构造方法。
说明Setting注入配置的bean都需要有默认为空的构造方法。 - 解决,添加默认的构造方法就行:
... public NormalMusic(){ } ...
5) 创建自定义的properties文件时报错:
- 详细错误:
Contens is not allowed prolog
- 出错原因,一般是.xml文件强转为.properties时的格式问题。
- 解决方案
将格式转换为UTF-8,等一段时间就好了。
2.问题6~10的解决方案
6)学习Spring AOP时出错
- 错误简介:
warning no match for this type name: name [Xlint:invalidAbsoluteTypeName]
出错代码定位:
@Aspect @Component public class DanceMoniter { @Pointcut("execution(** chap4.Performance2.perform(String) and args(name)") public void perform(String name){} @Around("perform(name)") public void AroundPerform(ProceedingJoinPoint joinPoint){ try{ System.out.println("performance begin!"); joinPoint.proceed(); System.out.println("performance end!"); System.out.println("the performance is great!"); }catch(Throwable e){ System.out.println("the performance is awful!"); } } }
- 原因:
Pointcut中语法错误。
proceed没有参数。(参数不匹配) 修改如下:
@Aspect @Component public class DanceMoniter { @Pointcut("execution(** chap4.Performance2.perform(String)) and args(name)") public void perform(String name){} @Around("perform(name)") public void AroundPerform(ProceedingJoinPoint joinPoint,String name){ try{ System.out.println("performance begin!"); joinPoint.proceed(new String[]{name}); //主要是这里 System.out.println("performance end!"); System.out.println("the performance is great!"); }catch(Throwable e){ System.out.println("the performance is awful!"); } } }
终于解决了。如果使用@Before等则不会出现这个问题。
7)SpringMVC配置web.xml后启动报错:
- 完整错误:
严重: Servlet [springDispatcherServlet] in web application [/SpringMVC-1] threw load() exception java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518) at ... at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
- 出错原因:
/WEB-INF下无法访问外部的jar包 - 解决:
项目右击–>properties–>Deployment Assembly–>add–>Java Build Path Entries–>导入所有依赖的Jar包,重新start tomcat即可。
8)使用Spring表单标签库时报错:
- 报错信息:
org.apache.jasper.JasperException: The absolute uri: http://www.springframework.org/tags/form cannot be resolved in either web.xml or the jar files deployed with this application org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:55) org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:277) org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:75) ......(省略n多行)
- 出错原因:未知
- 解决方法一:
在Tomcate部署设置中将Serve modules without publishing
选项去除。
该选项可以让tomcat直接使用WebContent下的东西。 - 解决方法二:详见博客
https://blog.csdn.net/flashdelover/article/details/59486535 - 解决方案三:
在仓库中找到相关的jar包添加到/WEB-INF/lib中(最直接最简单)
9)SpringMVC国际化过程出错:
- 错误信息:
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'message' for locale 'zh_CN'. org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:560) ... javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
- 错误原因及解决:
网上找了很多,发现都解决不了,后来发现是springmvc-servlet.xml配置messageSource时出错。id写成了messageResource。
正确配置:<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/jsp5/form"></property> <property name="defaultEncoding" value="UTF-8"></property> </bean>
- 详情查看笔记:
SpringMVC学习(六):EL表达式及国际化
最后更新: 2018年05月28日 01:07
原始链接: https://zjxkenshine.github.io/2018/04/14/Spring系列框架学习中遇到的问题/