gstackoverflow
gstackoverflow

Reputation: 37004

Could not make self injection in spring bean?

I went through several topics like this:

Self injection with Spring

And wrote this code:

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

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

But it doesn't work:

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)

What am I doing wrong?

looks like all the same as in the topic I linked

P.S.1

SPRING_BOOT_VERSION = "1.5.8.RELEASE"

P.S.2

I tried:

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;

But it haven't change anything

Upvotes: 1

Views: 3160

Answers (1)

lamplet
lamplet

Reputation: 103

@Lazy works for me.

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

Upvotes: 3

Related Questions