Java DI不注入抽象类的集合
我有一个抽象类Java DI不注入抽象类的集合,java,dependency-injection,spring-boot,abstract-class,autowired,Java,Dependency Injection,Spring Boot,Abstract Class,Autowired,我有一个抽象类 public abstract class SecSchema { ........ } 我有一些儿童课。我有以下所有接口实现的服务 @Service public class SecSchemaService { @Autowired Collection<SecSchema> secSchemas; ....getters and setters.... } 它工作正常。 但是,我有另一个抽象类: public abstract class Currecny
public abstract class SecSchema {
........
}
我有一些儿童课。我有以下所有接口实现的服务
@Service
public class SecSchemaService {
@Autowired
Collection<SecSchema> secSchemas;
....getters and setters....
}
它工作正常。
但是,我有另一个抽象类:
public abstract class Currecny {
@Autowired
SecSchemaService secSchemaService;
...... }
在子类中,我有以下代码:
@Component
public class USD extends Currecny implements PreValidateListener {
@PostConstruct
public void registerListeners() { secSchemaService.getSecSchemas(); }
我有NullPointerException,因为集合
secSchemaService.secSchemas
是空的。
我不知道为什么,但是集合是在USD类之后初始化的。我试图使用annotation@Dependson,但没用。
如果我注射
@Autowired
Collection<SecSchema> secSchemas;
在课堂上,一切正常。因此,只有当我注入集合SecSchemaService的包装时,它才起作用。我玩了很多有趣的游戏,尝试使用其他尚未初始化的bean进行后期构造
让它正常工作的一个有保证的方法是忘记post构造,使用一个bean实现ApplicationListener for ContextRefreshedEvent,然后在其中进行最终初始化。您可以在其中插入任何现有bean,并执行任何手动连接。虽然不理想,但效果很好。还有一个USD的类子级,它被标记为@Component。 这就是问题所在。只有层次结构中的最后一个类必须标记为@Component,因为我只需要这个类
@Autowired
Collection<SecSchema> secSchemas;