天天看點

springboot與guice開發

一、采用協作架構

springboot與guice開發

二、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xxxx</groupId>
    <artifactId>guiced</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>guiced</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-servlet</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
           

三、代碼結構

springboot與guice開發

四、代碼

package com.xxxx.guiced.controller;

import com.xxxx.guiced.handler.GreetingHandler;
import com.xxxx.guiced.module.SpringAwareServletModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.annotation.RequestScope;

import javax.annotation.Resource;

/**
 * @author nick
 */
@RestController
public class TestController {

    @Bean
    private Injector injector(ApplicationContext context) {
        return Guice.createInjector(
                new SpringAwareServletModule(context));
    }

    @Bean @RequestScope
    private GreetingHandler greetingHandler(
            Injector injector) {
        return injector.getInstance(GreetingHandler.class);
    }

    @Resource
    private GreetingHandler greetingHandler;

    @GetMapping("/greeting")
    public String greeting(@RequestParam("name") String name) {
        return greetingHandler.getByName(name);
    }
}

           
package com.xxxx.guiced.dao;

import org.springframework.stereotype.Component;

@Component
public class SampleDao {

  public void save(String data) {
    System.out.println(data + " saved.");
  }

  public String getPersonData(String name) {
    System.out.println("Getting person data for: " + name);
    return name;
  }
}

           
package com.xxxx.guiced.filter;

import com.google.inject.servlet.GuiceFilter;

import javax.servlet.annotation.WebFilter;

@WebFilter
public class SpringAwareGuiceFilter extends GuiceFilter {

}

           
package com.xxxx.guiced.handler;

import com.xxxx.guiced.dao.SampleDao;
import com.google.inject.servlet.RequestScoped;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author nick
 */
@RequestScoped
public class GreetingHandler {

  @Autowired
  private SampleDao sampleDao;

  public String getByName(String name) {
    sampleDao.save(name);
    return name;
  }
}

           
package com.xxxx.guiced.module;

import com.xxxx.guiced.dao.SampleDao;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.servlet.ServletModule;
import org.springframework.context.ApplicationContext;

/**
 * @author nick
 */
public class SpringAwareServletModule extends AbstractModule {

  private final ApplicationContext context;

  public SpringAwareServletModule(ApplicationContext context) {
    this.context = context;
  }

  @Override
  public void configure() {
    install(new ServletModule());
    bind(ApplicationContext.class).toInstance(context);
  }

  @Provides
  public SampleDao getSampleDao(ApplicationContext context) {
    return context.getBean(SampleDao.class);
  }
}

           
package com.xxxx.guiced;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * @author nick
 */
@SpringBootApplication
@ServletComponentScan
public class GuicedApplication {

    public static void main(String[] args) {
        SpringApplication.run(GuicedApplication.class, args);
    }

}

           

繼續閱讀