DI没有注入抽象类的集合

时间:2015-04-02 16:14:30

标签: java dependency-injection spring-boot abstract-class autowired

我有一个抽象类

public abstract class SecSchema {
........
}

我有一些儿童班。我有以下服务与所有接口的实现。

@Service
public class SecSchemaService {

@Autowired
Collection<SecSchema> secSchemas;
....getters and setters....
}

当我在控制器中进行DI时:

    @Autowired
private SecSchemaService secSchemaService;

它运作正常。 但是我有另一个抽象类:

public abstract class Currecny {

@Autowired
SecSchemaService secSchemaService;
...... }

在子课程中,我有以下代码:

@Component
public class USD extends Currecny implements PreValidateListener {
@PostConstruct
public void registerListeners() { secSchemaService.getSecSchemas(); }

因为集合

,我得到了NullPointerException
  

secSchemaService.secSchemas

为空。 我不知道为什么,但收集在课后美元初始化。我尝试使用注释@Dependson,但它没有帮助。 如果我注射

@Autowired
Collection<SecSchema> secSchemas;

在美元课程中它可以正常工作。因此,只有当我注入集合SecSchemaService的包装器时,它才能正常工作

2 个答案:

答案 0 :(得分:0)

我有很多乐趣和游戏结构试图使用尚未初始化的其他bean。

保证其工作的方法是忘记post构造并拥有一个为ContextRefreshedEvent实现ApplicationListener的bean,然后在那里进行最终的初始化。您可以在其中注入任何现有的bean,并进行所需的任何手动连接。不理想,但工作正常。

答案 1 :(得分:0)

还有一个美元的班级孩子,它被标记为@Component。 那就是问题所在。只有层次结构中的最后一个类必须标记为@Component,因为我只需要这个类。

相关问题