Dependency injection of Generic subtype with Spring

Viewed 1135

I am trying to inject specific Generic bean to Generic subtype, but Spring is unable to resolve dependency. By removing type from the bean, everything works as expected. Here is an example:

public class AbstractFrom{
    // ...
}

public class EmployyForm extends AbstractFrom{
    // ...
}

public class CompanyForm extends AbstractFrom{
    // ...  
}

abstract class AbstractBean<T extends AbstractFrom>{

    public abstract void calculate(T form); 
}

@Component
public CompanyBean extends AbstractBean<CompanyForm>{

    public void calculate(CompanyForm form){
        // specific impl
    }   
}

@Component
public EmployeeBean extends AbstractBean<EmployyForm>{

    public void calculate(EmployyForm form){
        // specific impl
    }   
}

Here is a target class:

 @Service
    public BaseService{

        @Autowire
        public AbstractBean<AbstractFrom> baseBean; // <- NoSuchBeanDefinitionException

        // @Autowire
        // public AbstractBean baseBean; <- Injection works as is expected
    }

Depends of active profile is initialized only CompanyBean or EmployeeBean, never both. I've also tried set the same name given beans and us @Qualifier annotation.

Does exist any way how inject this bean using diamond syntax? Does Spring is able to resolve given dependency? Using Spring 4.2.x.


Edit: With Spring 4.3 is should be possible. See Juergen Hoeller's talk

1 Answers
Related