最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot中動態(tài)語言groovy介紹

 更新時間:2022年09月14日 09:56:33   作者:Chuang-2  
Apache的Groovy是Java平臺上設(shè)計的面向?qū)ο缶幊陶Z言,這門動態(tài)語言擁有類似Python、Ruby和Smalltalk中的一些特性,可以作為Java平臺的腳本語言使用,這篇文章主要介紹了springboot中如何使用groovy,需要的朋友可以參考下

Groovy

Groovy是一種基于Java的語法的基于JVM的編程語言。Groovy支持動態(tài)輸入,閉包,元編程,運(yùn)算符重載等等語法。除此之外,Groovy還提供了許多類似腳本語言的功能,比如,多行字符串,字符串插值,優(yōu)雅的循環(huán)結(jié)構(gòu)和簡單的屬性訪問。另外,結(jié)尾分號是可選的。而這些都有足夠的理幫助開發(fā)人員為了提高開發(fā)效率。

換句話說,Groovy就是一種繼承了動態(tài)語言的優(yōu)良特性并運(yùn)行在JVM上的編程語言。由于Groovy的語法非常接近Java,所以Java開發(fā)人員很容易開始使用Groovy。 Spring Boot應(yīng)用中也支持使用Groovy編程語言進(jìn)行開發(fā)。

  • ResourceScriptSource:在 resources 下面寫groovy類
  • StaticScriptSource:把groovy類代碼放進(jìn)XML里
  • DatabaseScriptSource:把groovy類代碼放進(jìn)數(shù)據(jù)庫中

pom

<!-- groovy -->
<dependency>
    <artifactId>groovy</artifactId>
    <groupId>org.codehaus.groovy</groupId>
    <version>2.5.8</version>
    <scope>compile</scope>
</dependency>

ResourceScriptSource

@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);
    }
}
public interface GroovyService {
    String sayHello();
}

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="maple"/>
    </lang:groovy>
</beans>

DatabaseScriptSource

方法一:

實(shí)時讀取DB里的groovy腳本文件

利用GroovyClassLoader去編譯腳本文件

把class對象注入成Spring bean

反射調(diào)用腳本的方法

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';
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;
    }
}
/*
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.";
                }
            }
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);
}
@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);
    }
}

到此這篇關(guān)于Springboot中動態(tài)語言groovy介紹的文章就介紹到這了,更多相關(guān)Springboot groovy內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring整合Quartz實(shí)現(xiàn)動態(tài)定時器的示例代碼

    Spring整合Quartz實(shí)現(xiàn)動態(tài)定時器的示例代碼

    本篇文章主要介紹了Spring整合Quartz實(shí)現(xiàn)動態(tài)定時器的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-01-01
  • Java匿名內(nèi)部類的使用方法舉例詳解

    Java匿名內(nèi)部類的使用方法舉例詳解

    Java中的匿名內(nèi)部類是一種沒有名字的局部內(nèi)部類,主要用于一次性實(shí)現(xiàn)接口或繼承類的場合,它們常見于GUI事件處理、多線程編程等場景,簡化代碼結(jié)構(gòu)同時提高開發(fā)效率,需要的朋友可以參考下
    2024-09-09
  • Maven倉庫加載順序的實(shí)例解析

    Maven倉庫加載順序的實(shí)例解析

    Maven倉庫一般分為本地倉庫和遠(yuǎn)程倉庫。那么在實(shí)際開發(fā)中,在配置了多個倉庫的情況下,他們之間的加載訪問順序是怎么樣的呢,本文就詳細(xì)的來介紹一下
    2021-12-12
  • Java 關(guān)于eclipse導(dǎo)入項(xiàng)目發(fā)生的問題及解決方法(推薦)

    Java 關(guān)于eclipse導(dǎo)入項(xiàng)目發(fā)生的問題及解決方法(推薦)

    下面小編就為大家分享一篇Java 關(guān)于eclipse導(dǎo)入項(xiàng)目發(fā)生的問題及解決方法(推薦),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • hibernate一對多關(guān)聯(lián)映射學(xué)習(xí)小結(jié)

    hibernate一對多關(guān)聯(lián)映射學(xué)習(xí)小結(jié)

    這篇文章主要介紹了hibernate一對多關(guān)聯(lián)映射學(xué)習(xí)小結(jié),需要的朋友可以參考下
    2017-09-09
  • Java并發(fā)編程中的synchronized解析

    Java并發(fā)編程中的synchronized解析

    這篇文章主要介紹了Java并發(fā)編程中的synchronized解析,synchronized是一個重量級的鎖,使用不當(dāng)?shù)脑捚鋵?shí)會使我們程序執(zhí)行的效率大打折扣,今天我們就對其進(jìn)行講解,需要的朋友可以參考下
    2023-11-11
  • Java Spring @Lazy延遲注入源碼案例詳解

    Java Spring @Lazy延遲注入源碼案例詳解

    這篇文章主要介紹了Java Spring @Lazy延遲注入源碼案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Java SpringBoot自動裝配原理詳解

    Java SpringBoot自動裝配原理詳解

    這篇文章主要介紹了詳解Spring Boot自動裝配的原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-09-09
  • Java getRealPath(

    Java getRealPath("/")與getContextPath()區(qū)別詳細(xì)分析

    這篇文章主要介紹了Java getRealPath("/")與getContextPath()區(qū)別詳細(xì)分析,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Spring?MVC實(shí)現(xiàn)GET請求接收Date類型參數(shù)

    Spring?MVC實(shí)現(xiàn)GET請求接收Date類型參數(shù)

    這篇文章主要介紹了Spring?MVC實(shí)現(xiàn)GET請求接收Date類型參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07

最新評論

敦煌市| 阿坝| 宝清县| 金寨县| 四平市| 紫阳县| 孝昌县| 深泽县| 增城市| 邮箱| 阿鲁科尔沁旗| 长沙县| 三穗县| 锡林浩特市| 合水县| 凤翔县| 连城县| 大渡口区| 古田县| 彝良县| 察隅县| 库尔勒市| 台东县| 曲周县| 康定县| 玉环县| 辽中县| 津南区| 香格里拉县| 大同县| 宜昌市| 宁河县| 白水县| 石狮市| 隆德县| 慈溪市| 尼勒克县| 伊川县| 永宁县| 介休市| 遂川县|