Spring 扩展 spring-nc

码农软件 · 软件分类 · Java开发工具 · 2019-11-06 13:27:41

软件介绍

spring-nc=spring no controller,一个不需要controller类的spring扩展,只需在对应的service接口上应用controller相关注解,便能提供如同controller一样直接访问对应接口实现类的service方法。

此扩展还同时兼容原有的controller写法,使用简单对service无侵入。

 使用步骤如下:

1.先定义service接口类:

package cn.hn.java.summer.springnctest.service;

import cn.hn.java.summer.springnc.annotation.RequestMapping;
import cn.hn.java.summer.springnc.annotation.RestController;

@RestController
@RequestMapping("/")
public interface IHello {

    @RequestMapping("/say")
    String say(String name);
}

注意:这里的RestController、RequestMapping注解用的是spring-nc包下的注解,需要用spring-nc包中代替spring原有的注解有:@Controller、@RestController、@RequestMapping、@PutMapping、@PostMapping、@PatchMapping、@GetMapping和@DeleteMapping,其它注解都是用原有的。

2.编写实现接口的service类:

package cn.hn.java.summer.springnctest.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService implements IHello {

    public String say(String name) {
        return "hello "+name;
    }
}

3.启动程序时增加spring-nc调用:

package cn.hn.java.summer.springnctest;

import cn.hn.java.summer.springnc.AutoControllerApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "cn.hn.java.summer.springnctest.service")
public class Application {

    public static void main(String[] args) {
        AutoControllerApplication.run(Application.class); //最前面添加spring-nc调用
        SpringApplication.run(Application.class, args);
    }
}

4.启动程序访问 localhost:8080/say?name=world

输出:hello world

很简单简捷有没有?再也不用去写一遍controller了~~~

本文地址:https://codercto.com/soft/d/18394.html

Introduction to Graph Theory

Introduction to Graph Theory

Douglas B. West / Prentice Hall / 2000-9-1 / USD 140.00

For undergraduate or graduate courses in Graph Theory in departments of mathematics or computer science. This text offers a comprehensive and coherent introduction to the fundamental topics of graph ......一起来看看 《Introduction to Graph Theory》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具