Moment For Technology

Guice interacts with Spring

Posted on Aug. 8, 2022, 6:16 p.m. by 遊信宏
Category: The back-end Tag: The back-end

Cause: Because two open need to modify the source code, the source framework is Guice, to springboot related need to access each other's bean objects, etc

1. Access Spring's utility classes

import; import; import; import; import; import org.apache.log4j.Logger; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import org.springframework.web.context.WebApplicationContext; @component Public class GuiceBeanProviderFactoryT extends AbstractModule implements ApplicationContextAware, BeanFactoryAware { private final Logger log = Logger.getLogger(GuiceBeanProviderFactory.class); protected WebApplicationContext applicationContext; protected static BeanFactory beanFactory; private static BeanFactory getBeanFactory() { return beanFactory; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if (! (applicationContext instanceof WebApplicationContext)) { this.log.warn("Application context is not instance of WebApplicationContext unable to provide Guice registersthrough Spring."); } this.applicationContext = ((WebApplicationContext) applicationContext); } public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } public T get(Class clazz) throws ClassNotFoundException { return (T) injector().getInstance(clazz); } public ProviderT getNamed(final Class clazz, final String name) { return new Provider() { public Object get() { return GuiceBeanProviderFactory.this.injector().getInstance(Key.get(clazz, Names.named(name))); }}; } @Override protected void configure() { BeanFactory beanFactory = getBeanFactory(); if (beanFactory ! = null) { bind(BeanFactory.class).toInstance(beanFactory); } else this.log.warn("BeanFactory not initialized Spring - Guice Integration will not function"); } protected Injector injector() { return (Injector) this.applicationContext.getServletContext().getAttribute("guice-injector"); }}Copy the code

2. Access guice's configuration core (groups use this)

public class ComposeApplicationRunner implements ApplicationRunner {

    ApiComposeProperties apiComposeProperties;
    private Injector injector;

        Injector bootstrapInjector = Guice.createInjector(new BootstrapModule());
        ModulesProvider modulesProvider = bootstrapInjector.getInstance(ModulesProvider.class);
        injector = Guice.createInjector(modulesProvider.get());

    public Injector injector(){
        return injector;

    public void run(ApplicationArguments args) throws Exception {
Copy the code

3. Spring injection

import cn.getech.poros.apicompose.service.IComposeService; import; import; import org.springframework.stereotype.Service; /** * p * * /p * * @author * @since 2021/5/31 15:55 */ @Service public class ComposeServiceImpl implements IComposeService { private final WorkflowService workflowService; ComposeServiceImpl(Injector injector){ workflowService = injector.getInstance(WorkflowService.class); } @Override public void getHttpTask() { System.out.println("----------"); }}Copy the code
About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.