无法在春豆中自我注射?

时间:2017-11-16 12:04:57

标签: java spring spring-boot dependency-injection

我经历了以下几个主题:

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)

我做错了什么?

看起来与我链接的主题

完全相同

P.S.1

SPRING_BOOT_VERSION =" 1.5.8.RELEASE"

P.S.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;

但它没有改变任何东西

1 个答案:

答案 0 :(得分:0)

@Lazy为我工作。

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