java.lang.IllegalArgumentException异常处理的一种方法
我遇到的问题
用spring注解来加载bean,都是一个简单的了,但是还是报了这样一个错java.lang.IllegalArgumentException
网上查了一下,说是jdk版本要1.7的,然后我就改了,就好了。
Person.java
1 package com.xiaostudy.service; 2 3 import org.springframework.stereotype.Component; 4 5 @Component("person") 6 public class Person { 7 8 }
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <context:component-scan base-package="com.xiaostudy.service"></context:component-scan> 11 </beans>
测试类Test.java
1 package com.xiaostudy.service; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.xiaostudy.service.Person; 7 8 public class Test { 9 10 public static void main(String[] args) { 11 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 12 Person person = ac.getBean("person", Person.class); 13 System.out.println(person); 14 } 15 16 }
错误信息
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [E:\IT\spring3\demo9\bin\com\xiaostudy\service\Person.class]; nested exception is java.lang.IllegalArgumentException
解决问题
1、把eclipse的jdk调整为1.7
2、新建项目的jdk选择1.7
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)