概述
文章目录
- Groovy
- pom
- ResourceScriptSource
- service
- serviceImpl
- spring-groovy.xml
- 主启动类
- StaticScriptSource
- service
- spring-groovy.xml
- 启动类
- DatabaseScriptSource
- 方式一
- sql
- spring-groovy.xml
- 测试
- 脚本
- 基本使用
- 使用bean
- 动态脚本调用bean
Groovy
Groovy是一种基于Java的语法的基于JVM的编程语言。Groovy支持动态输入,闭包,元编程,运算符重载等等语法。适用于一些可变、和规则配置性的需求。除此之外,Groovy还提供了许多类似脚本语言的功能,比如,多行字符串,字符串插值,优雅的循环结构和简单的属性访问。另外,结尾分号是可选的。而这些都有足够的理帮助开发人员为了提高开发效率。
换句话说,Groovy就是一种继承了动态语言的优良特性并运行在JVM上的编程语言
- ResourceScriptSource:在 resources 下面写groovy类
- StaticScriptSource:把groovy类代码放进XML里
- DatabaseScriptSource:把groovy类代码放进数据库中
参考:https://blog.csdn.net/qq_33101675/article/details/108812075
https://www.jianshu.com/p/c7803626c09d
https://juejin.cn/post/7139877924676567048
groovy语法:https://www.w3cschool.cn/groovy/groovy_basic_syntax.html
变量,判断,列表,映射,模板,数据库,对象,
博主你好,请问可以分享一下spring中使用groovy(https://blog.csdn.net/qq_33101675/article/details/108812075)的源码吗
pom
<!-- groovy -->
<dependency>
<artifactId>groovy</artifactId>
<groupId>org.codehaus.groovy</groupId>
<version>2.5.8</version>
<scope>compile</scope>
</dependency>
ResourceScriptSource
service
// com.example.demo.groovy.GroovyService1
public interface GroovyService1 {
String sayHello();
}
serviceImpl
注意:GroovyService1Impl.groovy
// resources/groovy/GroovyService1Impl.groovy
class GroovyService1Impl implements GroovyService1 {
String name;
@Override
public String sayHello() {
return "Hello $name. Welcome to resource in Groovy.";
}
}
spring-groovy.xml
// src/main/resources/spring-groovy.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd">
<lang:groovy id="GroovyService1" script-source="classpath: groovy/GroovyService1Impl.groovy">
<lang:property name="name" value="maple"></lang:property>
</lang:groovy>
</beans>
主启动类
@SpringBootApplication
public class GroovyResourceApplication {
public static void main(String[] args) {
//SpringApplication.run(GroovyResourceApplication.class, args);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-groovy.xml");
GroovyService1 bean = context.getBean(GroovyService1.class);
String sayHello = bean.sayHello();
System.out.println("spring-groovy.xml " + sayHello);
}
}
// 输出:
// Hello test. Welcome to resource in Groovy.
StaticScriptSource
service
// // com.example.demo.groovy.GroovyService
public interface GroovyService {
String sayHello();
}
spring-groovy.xml
// src/main/resources/spring-groovy.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd">
<lang:groovy id="helloService">
<lang:inline-script>
import com.example.demo.groovy.GroovyService
class HelloServiceImpl implements GroovyService {
String name;
@Override
String sayHello() {
return "Hello $name. Welcome to static script in Groovy.";
}
}
</lang:inline-script>
<lang:property name="name" value="test"/>
</lang:groovy>
</beans>
启动类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
// ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-groovy.xml");
GroovyService bean = context.getBean(GroovyService.class);
String sayHello = bean.sayHello();
System.out.println(sayHello);
}
}
// Hello test. Welcome to resource in Groovy.
DatabaseScriptSource
方式一
- 实时读取DB里的groovy脚本文件
- 利用GroovyClassLoader去编译脚本文件
- 把class对象注入成Spring bean
- 反射调用脚本的方法
sql
CREATE TABLE `groovy_script` (
`id` bigint NOT NULL AUTO_INCREMENT,
`script_name` varchar(64) NOT NULL COMMENT 'script name',
`script_content` text NOT NULL COMMENT 'script content',
`status` varchar(16) NOT NULL DEFAULT 'ENABLE' COMMENT 'ENABLE/DISENABLE',
`extend_info` varchar(4096) DEFAULT NULL,
`created_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
`modified_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='groovy script';
spring-groovy.xml
INSERT INTO book_shop2.groovy_script
(id, script_name, script_content, status, extend_info, created_time, modified_time)
VALUES(1, 'groovyService', 'import com.example.demo.groovy.GroovyService
class HelloServiceImpl implements GroovyService {
@Override
String sayHello() {
return "sayHello";
}
@Override
String sayHello(String name) {
return "Hello " + name + ". Welcome to static script in Groovy.";
}
}', 'ENABLE', NULL, '2020-09-26 17:16:36.477818000', '2022-09-04 22:54:51.421959000');
测试
@RestController
public class GroovyController {
@Autowired
GroovyScriptMapper groovyScriptMapper;
@GetMapping("/aaaa")
private String groovyTest() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
GroovyScript groovyScript = this.groovyScriptMapper.getOne(1L);
System.out.println(groovyScript.getScriptContent());
Class clazz = new GroovyClassLoader().parseClass(groovyScript.getScriptContent());
Object o = clazz.newInstance();
SpringContextUtils.autowireBean(o);
Method method = clazz.getMethod("sayHello", String.class);
String aaaaaaa = (String) method.invoke(o, "aaaaaaa");
System.out.println(aaaaaaa);
return aaaaaaa;
}
}
// Hello aaaaaaa. Welcome to static script in Groovy.
public interface GroovyScriptMapper extends BaseMapper<GroovyScript> {
@Select({"select script_content from groovy_script where id = #{id}"})
@Result(column = "script_content", property = "scriptContent")
GroovyScript getOne(Long id);
}
脚本
基本使用
public static void main(String[] args) {
test1();
test2();
}
private static void test1() {
GroovyShell groovyShell = new GroovyShell();
//装载解析脚本代码
Script script = groovyShell.parse("" +
"def add(int a, int b) {n" +
" return a + bn" +
"}n" +
"n" +
"def mapToString(Map<String, String> paramMap) {n" +
" StringBuilder stringBuilder = new StringBuilder();n" +
" paramMap.forEach({ key, value ->n" +
" stringBuilder.append("key:" + key + ";value:" + value)n" +
" })n" +
" return stringBuilder.toString()n" +
"}");
//执行加法脚本
Object[] params1 = new Object[]{1, 2};
int sum = (int) script.invokeMethod("add", params1);
System.out.println("a加b的和为:" + sum);
//执行解析脚本
Map<String, String> paramMap = new HashMap<>();
paramMap.put("科目1", "语文");
paramMap.put("科目2", "数学");
Object[] params2 = new Object[]{paramMap};
String result = (String) script.invokeMethod("mapToString", params2);
System.out.println("mapToString:" + result);
}
private static void test2() {
Binding groovyBinding = new Binding();
groovyBinding.setVariable("map", new HashMap<>());
GroovyShell groovyShell = new GroovyShell(groovyBinding);
String scriptContent = "import com.example.demo.groovy.GroovyService1n" +
"map.put('a', '1');" +
"def query = map.get('a');" +
"println('aaaaaa' + query)";
Script script = groovyShell.parse(scriptContent);
script.run();
}
使用bean
@Component
public class SpringContextUtils implements ApplicationContextAware {
static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.context = applicationContext;
}
public static void autowireBean(Object bean) {
context.getAutowireCapableBeanFactory().autowireBean(bean);
}
public static ApplicationContext getContext() {
return context;
}
public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}
public static <T> T getBean(String name) {
return (T) context.getBean(name);
}
}
演示例子,在spring中使用
@GetMapping("/aaaa")
private String groovyTest() throws Exception {
test3();
return aaaa;
}
private static void test3() {
GroovyShell groovyShell = new GroovyShell();
//装载解析脚本代码
Script script = groovyShell.parse("" +
"n" +
"import com.example.demo.service.UserServicen" +
"import com.example.demo.utils.SpringUtilsn" +
"def getBean() {n" +
" UserService userService = SpringUtils.getBean(UserService.class);n" +
" println(userService.getUserName())n" +
"}");
//执行
script.invokeMethod("getBean", null);
}
动态脚本调用bean
@RestController
@RequestMapping("/groovy/script")
public class GroovyScriptController {
@Autowired
private Binding groovyBinding;
private GroovyShell groovyShell;
@PostConstruct
public void init(){
System.out.println("com.example.demo.groovy.GroovyScriptController.init -----");
GroovyClassLoader groovyClassLoader = new GroovyClassLoader(this.getClass().getClassLoader());
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setSourceEncoding("utf-8");
compilerConfiguration.setScriptBaseClass(TestScript.class.getName());
this.groovyShell = new GroovyShell(groovyClassLoader, this.groovyBinding, compilerConfiguration);
}
@RequestMapping(value = "/execute", method = RequestMethod.POST)
public String execute(@RequestBody String scriptContent) {
Script script = this.groovyShell.parse(scriptContent);
return String.valueOf(script.run());
}
}
@Configuration
public class GroovyBindingConfig implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Bean("groovyBinding")
public Binding groovyBinding() {
Binding groovyBinding = new Binding();
// 获取所有bean放入map
Map<String, Object> beanMap = this.applicationContext.getBeansOfType(Object.class);
//遍历设置所有bean,可以根据需求在循环中对bean做过滤
for (String beanName : beanMap.keySet()) {
if (beanName.equals("userServiceImpl")) {
System.out.println("com.example.demo.groovy.GroovyBindingConfig.groovyBinding --- userServiceImpl");
}
groovyBinding.setVariable(beanName, beanMap.get(beanName));
}
return groovyBinding;
}
/*@Bean("groovyBinding1")
public Binding groovyBinding1() {
Map<String, Object> beanMap = applicationContext.getBeansOfType(Object.class);
return new Binding(beanMap); //如果不需要对bean做过滤,直接用beanMap构造Binding对象即可
}*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
最后
以上就是细腻御姐为你收集整理的springboot中使用groovyGroovy的全部内容,希望文章能够帮你解决springboot中使用groovyGroovy所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复