spring-nc Spring 扩展 项目简介
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了~~~
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了~~~