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.citydo</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>

三、代码结构
在这里插入图片描述
四、代码

package com.citydo.guiced.controller;

import com.citydo.guiced.handler.GreetingHandler;
import com.citydo.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.citydo.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.citydo.guiced.filter;

import com.google.inject.servlet.GuiceFilter;

import javax.servlet.annotation.WebFilter;

@WebFilter
public class SpringAwareGuiceFilter extends GuiceFilter {

}

package com.citydo.guiced.handler;

import com.citydo.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.citydo.guiced.module;

import com.citydo.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.citydo.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);
    }

}

发布了226 篇原创文章 · 获赞 515 · 访问量 69万+

猜你喜欢

转载自blog.csdn.net/qq_32447301/article/details/103758577
今日推荐