欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

  • ResourceScriptSource:在 resources 下面寫groovy類
  • StaticScriptSource:把groovy類代碼放進XML里
  • DatabaseScriptSource:把groovy類代碼放進數(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

方法一:

實時讀取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)文章

  • JVM執(zhí)行引擎和垃圾回收要點總結(jié)

    JVM執(zhí)行引擎和垃圾回收要點總結(jié)

    不論是在問題現(xiàn)場還是跳槽面試,我們面對JVM性能問題,依舊會束手無辭,它需要你對Java虛擬機的實現(xiàn)和優(yōu)化,有極為深刻的理解。所以我在這里整理了一下 JVM的知識點。今天說說虛擬機執(zhí)行引擎和垃圾回收,都是十足的干貨,請各位看官耐心批閱!
    2021-06-06
  • 如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容

    如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容

    這篇文章主要介紹了如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java?File類的概述及常用方法使用詳解

    Java?File類的概述及常用方法使用詳解

    Java?File類的功能非常強大,下面這篇文章主要給大家介紹了關(guān)于Java中File類的概述及常用方法使用,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • WeakHashMap的使用方法詳解

    WeakHashMap的使用方法詳解

    這篇文章主要介紹了WeakHashMap的使用方法詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • 詳解Java如何實現(xiàn)在PDF中插入,替換或刪除圖像

    詳解Java如何實現(xiàn)在PDF中插入,替換或刪除圖像

    圖文并茂的內(nèi)容往往讓人看起來更加舒服,如果只是文字內(nèi)容的累加,往往會使讀者產(chǎn)生視覺疲勞。搭配精美的文章配圖則會使文章內(nèi)容更加豐富。那我們要如何在PDF中插入、替換或刪除圖像呢?別擔(dān)心,今天為大家介紹一種高效便捷的方法
    2023-01-01
  • java括號匹配問題介紹

    java括號匹配問題介紹

    大家好,本篇文章主要講的是java括號匹配問題介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • JAVA開發(fā)環(huán)境Vs?code配置步驟詳解

    JAVA開發(fā)環(huán)境Vs?code配置步驟詳解

    這篇文章主要為大家介紹了JAVA開發(fā)環(huán)境Vs?code配置步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • 用java將GBK工程轉(zhuǎn)為uft8的方法實例

    用java將GBK工程轉(zhuǎn)為uft8的方法實例

    本篇文章主要介紹了用java將GBK工程轉(zhuǎn)為uft8的方法實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 簡單總結(jié)單例模式的4種寫法

    簡單總結(jié)單例模式的4種寫法

    今天帶大家學(xué)習(xí)java的相關(guān)知識,文章圍繞著單例模式的4種寫法展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 詳解JVM的分代模型

    詳解JVM的分代模型

    這篇文章主要介紹了JVM的分代模型的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java虛擬機相關(guān)知識,感興趣的朋友可以了解下
    2020-10-10

最新評論