Using a configuration file for spring application context

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using a configuration file for spring application context

jjamsa

I am converting a Griffon 1.x application to Griffon 2.x.  The former used spring for dependency injection, and I would like to reuse its spring configuration file(s) for the latter.  Is that possible?

 

I see that there is something called spring-guice (https://github.com/spring-projects/spring-guice ).  How might that fit into the Griffon 2.x framework (if at all)?   In one example for spring-guice, it programmatically creates a module from a spring application context.  That doesn’t really fit into the way Griffon discovers modules and subsequently creates a single injector:

 

AnnotationConfigApplicationContext context =  new AnnotationConfigApplicationContext(ApplicationConfiguration.class);

Injector injector = Guice.createInjector(new SpringModule(context), new MyModule());

Service service = injector.getInstance(Service.class);

 


This e-mail, including attachments, may include confidential and/or
proprietary information, and may be used only by the person or entity
to which it is addressed. If the reader of this e-mail is not the intended
recipient or his or her authorized agent, the reader is hereby notified
that any dissemination, distribution or copying of this e-mail is
prohibited. If you have received this e-mail in error, please notify the
sender by replying to this message and delete this e-mail immediately.

Reply | Threaded
Open this post in threaded view
|

Re: Using a configuration file for spring application context

aalmiray
Administrator
The solution is to register the Guice based Module as part of the chain of modules that the Griffon runtime may discover. The easiest way to do it is by annotating a Guice module with `@ArtifactProviderFor`, as shown at http://griffon-framework.org/guide/2.7.0/#_the_guice_injector

Say you have a Spring based configuration class that looks like this

----
package org.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApplicationConfiguration {
    @Bean
    public Foo foo() {
        return new Foo();
    }
}
----

You may register this configuration file using spring-guice's SpringModule like this

----
package org.example;

import com.google.inject.Module;
import org.kordamp.jipsy.ServiceProviderFor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.guice.module.SpringModule;

@ServiceProviderFor(Module.class)
public class MySpringModule extends SpringModule {
    public MySpringModule() {
        super(createApplicationContext());
    }

    private static ApplicationContext createApplicationContext() {
        return new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
    }
}
----

And that's it. Now it's up to you to decide how much of Spring is used in this application as the spring-guice project mentions some limitations on the current implementation.

Cheers,
Andres