spring中org.springframework.context.support.AbstractApplicationContext refresh报错处理

报错场景:

        使用name属性实现构造注入或者使用index属性时出现ref和value使用不恰当引起的报错(报错如下,只显示最主要的报错)

四月 07, 2022 2:48:54 下午 org.springframework.context.support.AbstractApplicationContext refresh

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myStudent2' defined in class path resource [ba03/applicationContext.xml]: Unsatisfied dependency expressed through constructor parameter 2: Could not convert argument value of type [java.lang.String] to required type [com.bjpowernode.ba03.School]: Failed to convert value of type 'java.lang.String' to required type 'com.bjpowernode.ba03.School'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.bjpowernode.ba03.School': no matching editors or conversion strategy found

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>

<!--    使用index属性-->
    <bean id="myStudent2" class="com.bjpowernode.ba03.Student">
        <constructor-arg index="0" value="李四"/>
        <constructor-arg index="1" value="26"/>
        <constructor-arg index="2" value="myXuexiao"/>
    </bean>


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

测试Mytest03如下

package com.bjpowernode.ba01;
import com.bjpowernode.ba03.Student;
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("myStudent2");
        System.out.println("student对象"+myStudent);
    }
}

当该Mytest03运行时,则会出现文章开头的错误

我们只需要在applicationContext.xml配置文件中将使用<!-- 使用index属性-->下的第三个value修改为ref就可以避免该错误(index属性中的第三个value是表示构造方法的形参类型是简单类型的使用value,而该配置文件需要的是引用类型的)

以下内容为该文章的相关内容

di:给属性赋值

1.set注入(设置注入):

spring调用类的set类,你可以在set方法中完成属性赋值

1)简单类型的set注入 :

<bean id = "xx" class="yy">

        <property name="属性名字" value= "此属性的值"/>

        一个property只能给一个属性赋值                                  

         <property....> <bean>

2) 引用类型的set注入:spring调用类的set方法

<bean id="xxx" class="yyy">

        <property name="属性名称" ref="bean的id(对象名称)"/>

</bean>

2.构造注入:spring调用类有参数构造方法,在创建对象的同时,在构造方法属性中给属性赋值 构造注入使用<constructor-arg>标签

<constructor-arg>标签:

一个<constructor-arg>表示构造方法一个参数 <constructor-arg>标签属性:

name:表示构造方法的形参名

index:表示构造方法的参数位置,参数从左往右位置是0,1,2的顺序

value:构造方法的形参类型是简单类型的,使用value

ref:构造方法的形参类型是引用类型的,使用ref

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值