gpt4 book ai didi

java - 无法在 Spring bean 中进行 self 注入(inject)?

转载 作者:行者123 更新时间:2023-12-02 12:00:06 29 4
gpt4 key购买 nike

我浏览了几个这样的主题:

Self injection with Spring

并编写了这段代码:

    @Service("emailService")
public static class EmailService {

@Resource(name = "emailService")
private EmailService self;
@PostConstruct
public void initialize() {
EmailMessage.emailService = self;
}

但它不起作用:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
16.11.17 15:01:45.692 [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'emailService': Bean with name 'emailService' has been injected into other beans [csvMappingConfiguration,emailService] 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.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:585)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at ****.Application.main(Application.java:12)

我做错了什么?

看起来与我链接的主题中的完全相同

附注1

SPRING_BOOT_VERSION = "1.5.8.RELEASE"

附注2

我尝试过:

public interface SomeService {
public void sendEmail(String from, String subject, String[] to, Map<String, ?> props, String templateFileName) throws Exception;
}

@Service("emailService")
public static class EmailService implements SomeService{

@Resource(name = "emailService")
private SomeService self;

但是并没有改变任何事情

最佳答案

@Lazy 适合我。

@Lazy
@Resource(name = "emailService")
private EmailService self;

关于java - 无法在 Spring bean 中进行 self 注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47329233/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com