spring-boot集成spring-brick实现动态插件
import com.example.springbrick.application.helper.DynamicClass;
import com.example.springbrick.application.plugin.api.qwer.QwerService;
import com.example.springbrick.application.plugin.api.xyz.XyzService;
import com.example.springbrick.application.plugin.api.xyz.entity.IdInfo;
import com.example.springbrick.application.plugin.api.xyz.entity.User;
import com.gitee.starblues.integration.user.PluginUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;
/**
* 动态插件支持
*/
@RestController
@RequestMapping("/demo")
public class DemoController {
@Resource
private PluginUser pluginUser;
@GetMapping("hello-world")
public void helloWorld() {
User user = new User();
user.setName("张三");
user.setBirthday(LocalDateTime.now());
IdInfo idInfo = new IdInfo();
idInfo.setIdNumber("12345");
idInfo.setExt("non");
user.setIdInfo(idInfo);
/* ------------------------------ 示例1 ------------------------------ */
QwerService qwerService = pluginUser.getBeanByInterface("plugin-qwer", QwerService.class).get(0);
System.err.println(qwerService.helloWorld());
System.err.println(qwerService.helloWorld123("sdfsd"));
XyzService xyzService = pluginUser.getBeanByInterface("plugin-xyz", XyzService.class).get(0);
xyzService.helloWorld();
System.err.println(xyzService.userInfo(user));
System.err.println();
System.err.println();
/* ------------------------------ 示例2 ------------------------------ */
Map<String, Object> pluginBean = pluginUser.getBean(QwerService.BEAN_NAME, false).getPluginBean();
pluginBean.forEach((pluginId, springBean) -> {
System.err.println(pluginId + " " + DynamicClass.exec(springBean, QwerService::helloWorld));
System.err.println(pluginId + " " + DynamicClass.exec(springBean, QwerService.EMPTY_INSTANCE::helloWorld123, "123"));
});
pluginBean = pluginUser.getBean(XyzService.BEAN_NAME, false).getPluginBean();
pluginBean.forEach((pluginId, springBean) -> {
DynamicClass.exec(springBean, XyzService::helloWorld);
System.err.println(pluginId + " " + DynamicClass.exec(springBean, XyzService.EMPTY_INSTANCE::userInfo, user));
});
}
}