Spring循环依赖问题解决
程序员文章站
2022-05-06 18:18:57
...
报错信息
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项目
微信扫一扫关注公众号
点击链接加入群聊
https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105