Giter Site home page Giter Site logo

fake-spring's Introduction

Fake Spring

基于内嵌的Tomcat实现(假的)Spring框架

在Spring Boot中通常执行SpringApplication类方法run()启动Spring应用

public class Application {

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

}

fake-spring使用了类似的方法

import me.lee.fakespring.framework.starter.FakeSpringApplication;

public class Application {

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

}

创建一个Controller, 为Controller添加@Controller注解

import me.lee.fakespring.framework.web.annotation.Controller;

@Controller
public class EchoController {
}

创建一个用于返回消息的Service Bean, 为Service添加@Bean注解

@Bean
class EchoService {
    
    String echo(String msg) {
        return "You said \"" + msg + "\"";
    }

}

在Controller中添加一个用于处理echo请求的RequestMapping

import me.lee.fakespring.framework.web.annotation.Controller;
import me.lee.fakespring.framework.web.annotation.RequestMapping;
import me.lee.fakespring.framework.web.annotation.RequestParam;

@Controller
public class EchoController {

    @RequestMapping("/echo")
    public String echo(@RequestParam(value = "msg", required = false, defaultValue = "nothing") String msg) {
    }

}

在Controller中注入Service

import me.lee.fakespring.framework.web.annotation.Controller;
import me.lee.fakespring.framework.web.annotation.RequestMapping;
import me.lee.fakespring.framework.web.annotation.RequestParam;
import me.lee.fakespring.framework.beans.Autowired;

@Controller
public class EchoController {

    @Autowired
    private EchoService echoService;

    @RequestMapping("/echo")
    public String echo(@RequestParam(value = "msg", required = false, defaultValue = "nothing") String msg) {
        return echoService.echo(msg);
    }

}

构建、运行app

./gradlew clean build
java -jar app/build/libs/app-1.0-SNAPSHOT.jar

请求echo

curl "127.0.0.1:8000/echo?msg=hello"

You said "hello"

Just for fun.

fake-spring's People

Contributors

neillee95 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.