有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

@Repository类中的java自引用导致循环引用

我发现@Repository中的自引用不起作用,例如:

@Repository
public class HelloService {
    static final Logger Log = LoggerFactory.getLogger(HelloService.class);

    @Autowired
    HelloService self;  // <----- self reference
    ...
}

有循环参考误差

o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'helloService': Bean with name 'helloService' has been injected into other beans [helloService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.

然而,当把Repository更改为@Component@Service时,这个技巧可能会起作用。作为一种解决方法,我必须在self中添加一个@Lazy


共 (0) 个答案