匿名函數託管器 spring-boot-func-starter

spring-boot-func-starter

spring-boot-func-starter 介紹

項目地址: //gitee.com/yiur/spring-boot-func-starter

基於SpringBoot的匿名函數託管器,在springboot中可以將接口的方法進行各種操作

平時的java開發中繼承接口要將接口的方法全實現,而func匿名函數託管不需要進行全部

實現可以用@FuncLambda進行單獨實現,沒有進行匿名實現的接口方法不能進行調用,

func匿名函數還可以動態實現接口方法,配合代碼達到更多操作,此外還有函數回調的

功能,在web開發中使用func匿名函數的對象是默認調用回調方法的,你也可以通過繼承

Callback接口編寫自己想要的(then, error)回調方法

error目前不兼容springboot的熱部署

@FuncConfiguration 匿名配置

@FuncLambda 匿名函數配置

註解模式開發

項目搭建

定義接口,此接口的方法由匿名函數代理

public interface WebInitOutInfo {

    String out(String message, String... args);

}
  • @FuncConfiguration 匿名函數的總配置
  • @FuncLambda 匿名函數
    • classFile 代理的接口
    • funcCallback 回調函數
      • callbackClass 綁定的回調函數類
@FuncConfiguration
public class WebFuncLinkConfig {

    @FuncLambda(classFile = WebInitOutInfo.class, 
                funcCallback = @FuncCallback(callbackClass = BlogCallback.class))
    public String out(@FuncParameter("message") String message, @FuncParameter("args") String... args) {
        return FuncString.format(message, args);
    }

}

回調函數實現Callback接口即可

public class BlogCallback implements Callback {

    @Override
    public Object then(Object o) {
        return FuncString.format("then blogCallBack:value(?)", o);
    }

    @Override
    public Object error(Exception e) {
        return ((InvocationTargetException)e).getTargetException().getMessage();
    }

}

啟動類開啟匿名函數自動裝配 @EnableFuncLambda

@EnableWebMvc
@EnableFuncLambda
@SpringBootApplication
public class WebBlogApplication {

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

}

配置web進行測試,編寫controller

@Controller
public class BlogController {

    @Autowired
    public WebInitOutInfo webInitOutInfo;

    @ResponseBody
    @RequestMapping("/replace")
    public String value(String message, String... args) {
        return webInitOutInfo.out(message, args);
    }

}

測試成功回調[//localhost:8888/replace?message=value ? ?&args=12, hello func](//localhost:8888/replace?message=value ? ?&args=12, hello func)

image

測試失敗回調[//localhost:8888/replace?message=value ? ??&args=12,%20hello%20func](//localhost:8888/replace?message=value ? ??&args=12, hello func)

image