Spring循环依赖问题解决

在这里插入图片描述

报错信息

Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘myDao’: Requested bean is currently in creation: Is there an unresolvable circular reference?

Spring配置(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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.yangzc"/>
</beans>

Service类

package com.yangzc.service;

import com.yangzc.dao.MyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyService {

    private MyDao myDao;

    @Autowired
    public MyService(MyDao myDao) {
        this.myDao = myDao;
    }

    public void list(){
        myDao.query();
    }

    public void hello(){
        System.out.println("hello service!");
    }
}

Dao类

package com.yangzc.dao;

import com.yangzc.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyDao {

    private MyService myService;

    @Autowired
    public MyDao(MyService myService) {
        this.myService = myService;
    }

    public void query(){
        System.out.println("hello query!");
    }

    public void welcome(){
        myService.hello();
    }
}

主类

package com.yangzc;

import com.yangzc.dao.MyDao;
import com.yangzc.service.MyService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyService service = ac.getBean(MyService.class);
        MyDao dao = ac.getBean(MyDao.class);
        service.list();
        dao.welcome();
        System.out.println( "Hello World!" );
    }
}

分析

构造方法注入依赖的方式不能解决循环依赖的问题,可以通过接口注入或者setter注入的方式解决

修改Service类

package com.yangzc.service;

import com.yangzc.dao.MyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyService {

    @Autowired
    private MyDao myDao;

    public void list(){
        myDao.query();
    }

    public void hello(){
        System.out.println("hello service!");
    }
}

修改Dao类

package com.yangzc.dao;

import com.yangzc.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyDao {

    @Autowired
    private MyService myService;

    public void query(){
        System.out.println("hello query!");
    }

    public void welcome(){
        myService.hello();
    }
}

运行结果

在这里插入图片描述

参考资料

[01] Spring框架注解扫描开启之配置细节
[02] spring-配置注解扫描
[03] spring中的循环依赖解决方案
[04] idea创建一个spring项目

微信扫一扫关注公众号
image.png
点击链接加入群聊

https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值