Including Common Subtype Dependencies Using Spring

I am trying to introduce a special Generic bean into a common subtype, but Spring cannot resolve the dependency. By removing the 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 the target class:

  @Service public BaseService{ @Autowire public AbstractBean<AbstractFrom> baseBean; // <- NoSuchBeanDefinitionException // @Autowire // public AbstractBean baseBean; <- Injection works as is expected } 

Active profile dependencies are initialized only by CompanyBean or EmployeeBean , and not both. I also tried setting the same name using beans annotation and us @Qualifier .

Is there any way to use this bean using diamond syntax? Can Spring resolve this dependency? Using Spring 4.2.x.


Edit: When using Spring 4.3 should be possible. See JΓΌrgen Hover's discussion

+5
source share
1 answer

I know that you probably won't like it, but why not split EmployeeForm and CompanyForm into two separate base forms, and then in BaseService do TWO strong> for Autowire.

This is not Spring's answer per se, but it is what I would do, as quick work, to see if this would work. Separating them is not a terrible design compromise.

0
source

Source: https://habr.com/ru/post/1245000/


All Articles