2

Here are two objects I want to receive ContextRefreshedEvent

@Component
public class InitDB {
                  
    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent e) {
        //
    }
}

InitDB works as expected, but in this case not:

@Service
public class MyService implements IMyService{
    
    @Autowired
    private MyDao _dao;             // Autowired WORKs
     
    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent e) {
        // DOESN'T WORK
    }
    
    [...]
     
}

Any Idea what I'm doing wrong?

Here are my maven properties

<properties>
    <java-version>1.7</java-version>
    <org.springframework-version>4.2.2.RELEASE</org.springframework-version>
    <spring-security-web-version>3.2.5.RELEASE</spring-security-web-version>
    <org.aspectj-version>1.7.2</org.aspectj-version>
    <org.slf4j-version>1.5.10</org.slf4j-version>
    <hibernate-version>4.3.6.Final</hibernate-version>
    <json-jackson-version>2.4.1</json-jackson-version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <skipTests>true</skipTests>
</properties>
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
alex
  • 8,904
  • 6
  • 49
  • 75

1 Answers1

0

I had the same problem and for me, it proved my listener bean wasn't loaded in the dependencies by Spring.

If you use Spring Boot Actuator, you can check the loaded beans at https://localhost:8443/beans.

The bean wasn't detected by Spring because I had a namespace typo. My main namespace was com.foo (with packages like com.foo.security and com.foo.controllers), and when I added a new package logging, I introduced a typo in the namespace: com.ffoo.logging.

Because of this, Spring could not find the new Components/Services I was adding.

For this particular question, it doesn't matter if it's annotated with @Service, because @Service is a sub-class of @Component (see post)

Community
  • 1
  • 1
Andrei Epure
  • 1,746
  • 1
  • 21
  • 30