首页 > 编程知识 正文

spring解决循环依赖,springframework依赖

时间:2023-05-04 00:35:52 阅读:192390 作者:3291

文章目录 报错信息Spring配置(applicationContext.xml)Service类Dao类主类分析修改Service类修改Dao类运行结果参考资料

报错信息

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.hxddh"/></beans> Service类 package com.hxddh.service;import com.hxddh.dao.MyDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic 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.hxddh.dao;import com.hxddh.service.MyService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic 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.hxddh;import com.hxddh.dao.MyDao;import com.hxddh.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.hxddh.service;import com.hxddh.dao.MyDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class MyService { @Autowired private MyDao myDao; public void list(){ myDao.query(); } public void hello(){ System.out.println("hello service!"); }} 修改Dao类 package com.hxddh.dao;import com.hxddh.service.MyService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic 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项目

微信扫一扫关注公众号

点击链接加入群聊

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。