spring中遇到的java.lang.ClassCastException中cannot be cast to报错(初学)

2年前 (2022) 程序员胖胖胖虎阿
339 0 0

关于这个报错,我们可能会遇到cannot be cast to(不能将什么类型转换成什么)我遇到问题的场景是在于复制包的时候没有导包。

applicationContext.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    使用name属性实现构造注入-->
    <bean id ="myStudent" class="com.bjpowernode.ba03.Student">
        <constructor-arg name="myname" value="张三"/>
        <constructor-arg name="myage" value="20"/>
        <constructor-arg name="mySchool" ref="myXuexiao"/>
    </bean>

<!--声明School对象-->
    <bean id="myXuexiao" class="com.bjpowernode.ba03.School">
        <property name="name" value="北京大学"/>
        <property name="address" value="北京海淀区"/>
    </bean>

School和Student如下

package com.bjpowernode.ba03;
public class School {
    private String name;
    private String address;
    public void setName(String name) {
        this.name = name;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "School{" +
                "name='" + name + ''' +
                ", address='" + address + ''' +
                '}';
    }
}

package com.bjpowernode.ba03;
public class Student {
    private String name;
    private int age;
//声明一个引用类型
    private School school;
    public Student (){
        System.out.println("spring会调用类的无参构造器方法创建对象");
    }
    public void setName(String name) {this.name = name; }
    public void setSchool(School school) {this.school = school; }
    public void setAge(int age) {this.age = age; }
    /**
     * 创建有参数构造方法
     */
    public Student(String myname,int myage,School mySchool){
        System.out.println("Student有参数构造方法");
        //属性赋值
        this.name=myname;
        this.age=myage;
        this.school=mySchool;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }
}

Mytest03如下

package com.bjpowernode.ba01;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest03 {
    @Test
    public void test01(){
        //使用spring容器创建对象
        //1.指定spring配置文件的名称
        String config = "ba03/applicationContext.xml";//注意:该引号内的配置文件必须是resources下的全限定名称,否则会报错
        //2.创建表示spring容器的对象,ApplicatContext
        //ApplicationContext就是表示spring容器,通过容器获取对象
        //ClassPathXmlApplicationContext:表示从类路径中加载spring的配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //从容器中获取某个对象,你要调用对象方法
        //getBean("配置文件中的bean的id值")
        //从容器中获取Student对象
        Student myStudent = (Student) ac.getBean("myStudent");
        System.out.println("student对象"+myStudent);
    }
}

当Mytest03执行时,会出现如下的错误

Student有参数构造方法

java.lang.ClassCastException: com.bjpowernode.ba03.Student cannot be cast to com.bjpowernode.ba01.Student

由“Student有参数构造方法”我们可以知道,配置文件创建了对象并且执行了,即问题不在配置文件和Student和School里面,那么问题只能在Mytest03里面。

解决方法导包(导入cannot be cast to 前面的包,即可):

import com.bjpowernode.ba03.Student;

此时

Student有参数构造方法
student对象Student{name=‘张三’, age=20, school=School{name=‘北京大学’, address=‘北京海淀区’}}

Process finished with exit code 0

相关文章

暂无评论

暂无评论...