1

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

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • They use the interface the class implements. It would work if your class EmailService would implement, let’s say SomeService and that you could inject – Alex P. Nov 16 '17 at 12:08
  • 2
    Ok sorry I didn’t pay attention. What is your usecase? Why would you ever need to inject the own bean? Can you name one usecase? – Alex P. Nov 16 '17 at 12:47
  • Well, have you considered https://stackoverflow.com/a/5153645/4763309 then? – mrkernelpanic Nov 16 '17 at 13:23
  • @mrkernelpanic, Yes i considered, but it doesn't work for me. may be because of inner classes – gstackoverflow Nov 16 '17 at 15:09

1 Answers1

3

@Lazy works for me.

@Lazy
@Resource(name = "emailService")
private EmailService self;
lamplet
  • 103
  • 1
  • 1
  • 8