views:

88

answers:

2

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

A: 

Apart from having an uppercase variable and no colon - it's fine.

You would, of course, need to define your class as a bean. Either by using the @Service (or another stereotype) annotation on it, or using <bean> in applicationContext.xml (see here for the annotation-based config introduced in spring 2)

Another thing: your member variables should be lowercase, not uppercase.

Bozho
@Bozho, i get error, if i call the method this way. please see my update
cometta
see mine..., ;)
Bozho
+3  A: 

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.

davetron5000