Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 另一个服务中的服务弹簧注入_Spring_Dependency Injection_Annotations_Javabeans - Fatal编程技术网

Spring 另一个服务中的服务弹簧注入

Spring 另一个服务中的服务弹簧注入,spring,dependency-injection,annotations,javabeans,Spring,Dependency Injection,Annotations,Javabeans,我正在尝试设置两个服务和一个控制器,如下所示: 控制器类: @Controller public class MyController { @Autowired IMyService1 service; } 服务2: @Service public class MyService2 implements IMyService2 { } 服务1: @Service public class MyService1 implements IMyService1 { @Au

我正在尝试设置两个服务和一个控制器,如下所示:

控制器类:

@Controller
public class MyController {

   @Autowired
   IMyService1 service;
}
服务2:

@Service
public class MyService2 implements IMyService2 {   }
服务1:

@Service
public class MyService1 implements IMyService1 {

   @Autowired
   IMyService2 myService2;  // this bean is not getting created
}
所有内容都在beans.xml中正确设置,以便在组件扫描中提取组件,并且所有内容都在同一个基本包下

第一个服务已正确注入控制器类,但第二个服务未能注入第一个服务(BeanCreationException)


有没有人遇到过这个问题,或者对我的错误有什么想法/建议?

我对这个问题做了一些研究,但是我找不到解决这个问题的答案,我只能动态实例化第二个服务,而没有
@service
注释

控制器:不要触摸,它是一样的

@Controller
public class MyController {
   @Autowired
   IMyService1 service;
}
服务2:从第二个服务中删除
@Service

public class MyService2 implements IMyService2 {
    public void doSomething() {
        // your code
    }
}
@Service
public class MyService1 implements IMyService1 {
   public void actionWithService2() {
       new MyService2().doSomething();
   }
}
服务1:删除
@Autowired
并动态实例化第二个服务

public class MyService2 implements IMyService2 {
    public void doSomething() {
        // your code
    }
}
@Service
public class MyService1 implements IMyService1 {
   public void actionWithService2() {
       new MyService2().doSomething();
   }
}

您是否尝试过@Resource(name=“myService2”)而不是@Autowired?您是否尝试过通过contex.getBean(myService2.class)获取myService2?它是返回您的服务还是抛出和异常?