Piyush
Piyush

Reputation: 1568

ContextRefreshedEvent is not triggring on AnnotationConfigApplicationContext loading

I created Spring application and I'm loading Application context in standalone Java application. My code is like following

public class Application {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
    }
}

I want to copy some data in in-memory database before application start. If I put code after ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class); It starts copying data after context initialize. but application also start processing orders before I copy data in in-memory database. I want to copy data before application start and after all beans initialize(context load).

I tried like following

public class AppPostLoader implements ApplicationListener<ContextRefreshedEvent> {

    public void onApplicationEvent(ContextRefreshedEvent event) {
        RedisTemplate<String, String> redisTemplate = (RedisTemplate) event.getApplicationContext().getBean("redisTemplate");
        //Coping data
    }
}

But It's not calling onApplicationEvent method.

How I can call it before application start and after context load.

Upvotes: 0

Views: 339

Answers (1)

Mudassar
Mudassar

Reputation: 3205

Add @Component on the listener class

Upvotes: 1

Related Questions