Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call service inside service layer

in my service layer

public class  MyServiceLayerImpl{   
    public void method1(){
       MyServicelayer.method();  //is this correct?
    }

    public void method2(){
    }

    @Autowired
    MyServiceInterface MyServiceLayer;
}

if i have method inside service layer that need to call another service inside service layer. i cannot use this._method ,because, i'm using AOP for caching. In order for the caching to work, i have to use @Autowired to get the service. Therefore, is the above style ok?

i get below error

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.company.iss.services.MyServiceLayerImpl#85aedd': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.company.iss.services.MyServicelayer com.company.iss.services.MyServiceLayerImpl.MyServiceLayer; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.company.iss.services.MyServiceLayer] is defined: Unsatisfied dependency of type [interface com.company.iss.services.MyServiceLayer]: expected at least 1 matching bean

like image 771
cometta Avatar asked Dec 03 '25 19:12

cometta


1 Answers

It's hard to tell from the weird formatting and naming, but if you want to call one service from another:

public interface MasterService {
  void someMethod();
}

public class MasterServiceImpl implements MasterService {
  private OtherService otherService;

  public void someMethod() {
    this.otherService.someCallOnOtherService();
  }

  @Autowired
  public void setOtherService(OtherService otherService) {
    this.otherService = otherService;
  }
}

Now, you must have configured both MasterServiceImpl and whatever implements OtherService. There are many ways to do this, the most popular being explicitly in your XML configuration with annotation-based configured a close second.

Also note that AOP tends to be very flaky if you aren't using interfaces. In your code, your Impl doesn't actually implement anything. I would recommend against that.

like image 197
davetron5000 Avatar answered Dec 06 '25 11:12

davetron5000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!