ApplicationContextInitializer in context without Spring web UI?

I created an ApplicationContextInitializer implementation to load properties from a custome (ZooKeeper) source and add them to the ApplicationContext property source list.

All the documentation I can find relates to Spring web applications, but I want to use this in a standalone application consuming posts.

The right approach to instantiate my implementation, create a context, and then pass the context of my implementation "manually"? Or am I missing some kind of automatic framework function that will apply my initializer to my context?

+7
source share
3 answers

I found it simple enough to implement SpringMVC's strategy for initializing a context by initializing with an empty context. In normal application contexts, there is nothing that uses ApplicationContextInitializer, so you have to execute it yourself.

There is no problem, though, since in the framework of a regular J2SE application, if you have rights to the context loader block, you will have access to each stage of the life cycle.

 // Create context, but dont initialize with configuration by calling // the empty constructor. Instead, initialize it with the Context Initializer. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); MyAppContextInitializer initializer = new MyAppContextInitializer(); initializer.initialize( ctx ); // Now register with your standard context ctx.register( com.my.classpath.StackOverflowConfiguration.class ); ctx.refresh() // Get Beans as normal (eg Spring Batch) JobLauncher launcher = context.getBean(JobLauncher.class); 

Hope this helps!

+8
source

If I understand the problem correctly, you can find the solution in the Spring documentation Section 4. IoC container

An example of how to start your application is 4.2.2 Creating a container instance

See also 5.7 Application Contexts and Resource Paths.

+1
source

Not sure about other versions, but in Spring 4:

 AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(yourConfig.class); 
0
source

All Articles