0

I have a Service class that has a generic type and a setController method that is based on the same generic type. the generic type of the servic object is only known at the time of declaration.

The problem is now when i define a ControllerImpl where the generic type is defined the @Autowired method of setController does not use that component.

Has somebody an idea how to fix it and keep the ServiceImpl generic. (it would work when i define the typ in ServiceImpl as well).

The following example show the problem i'm facing with:

@SpringBootTest
@ActiveProfiles("local")
public class AccessTest {

  @Autowired
  private ServiceA<BeanA> service;

  @Test
  void test(){
    Assertions.assertNotNull(service.controller);
  }

  interface ValueGetter{
  }

  static class BeanA implements ValueGetter{
  }

  static class AbstractService<B extends ValueGetter>{

    Controller<B> controller;

    @Autowired
    void setController(@Nullable Controller<B> controller){
      this.controller = controller;
    }
  }

  interface Controller<B extends ValueGetter>{
    void doSomething(B value);
  }


  //not inner class
  @Service
  public class ServiceA<B extends AccessTest.ValueGetter> extends AccessTest.AbstractService<B> {

  }


  //not inner class
  @Component
  public class ControllerImpl implements AccessTest.Controller<AccessTest.BeanA> {

    @Override
    public void doSomething(final AccessTest.BeanA value) {

    }
  }
}
sge
  • 391
  • 2
  • 16
  • 1
    Maybe take a look at this question: [How to Autowire Bean of generic type in Spring?](https://stackoverflow.com/questions/22603291/how-to-autowire-bean-of-generic-type-t-in-spring) – Enfield Li Oct 24 '22 at 08:17

0 Answers0