resteasy invoke service inside another service

2019-09-04 12:50发布

问题:

I have a services X and Y. If I want to invoke Y inside X. Is there a way to do that thru annotations. I don't want to configure a bean for X/Y, as all the other resources are Autowired for X.

Thanks!

回答1:

Spring can only inject managed instances:

@Service
public class X {

   @Resource
   private Y y;
}


@Service
public class Y {
}

If you do not like to add @Service to class Y, then you could use this (X will be the same)

@Configuration
public class AppConfig {
   @Bean
   public Y y {
      return new Y();
   }
}