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

springboot中使用groovy的示例代碼

 更新時間:2022年09月14日 10:23:58   作者:Chuang-2  
Groovy就是一種繼承了動態(tài)語言的優(yōu)良特性并運(yùn)行在JVM上的編程語言,Groovy支持動態(tài)輸入,閉包,元編程,運(yùn)算符重載等等語法,這篇文章主要介紹了springboot中使用groovy的相關(guān)知識,需要的朋友可以參考下

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

方法一:

實時讀取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中使用groovy的文章就介紹到這了,更多相關(guān)springboot 使用groovy內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 讀取excel內(nèi)容具體代碼

    java 讀取excel內(nèi)容具體代碼

    這篇文章介紹了java 讀取excel內(nèi)容具體代碼,有需要的朋友可以參考一下
    2013-10-10
  • SpringBoot實現(xiàn)WebSocket即時通訊的示例代碼

    SpringBoot實現(xiàn)WebSocket即時通訊的示例代碼

    本文主要介紹了SpringBoot實現(xiàn)WebSocket即時通訊的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • SpringBoot利用自定義json序列化器實現(xiàn)敏感字段數(shù)據(jù)脫敏詳解

    SpringBoot利用自定義json序列化器實現(xiàn)敏感字段數(shù)據(jù)脫敏詳解

    這篇文章主要介紹了SpringBoot利用自定義json序列化器實現(xiàn)敏感字段數(shù)據(jù)脫敏詳解,因為案例代碼用到了hutool提供的DesensitizedUtil數(shù)據(jù)脫敏工具類,這里要引入hutool的依賴,如果你需要自定義 數(shù)據(jù)脫敏的邏輯,可以不引入這個依賴,需要的朋友可以參考下
    2024-01-01
  • Spring?Boot?整合持久層之Spring Data JPA

    Spring?Boot?整合持久層之Spring Data JPA

    在介紹Spring Data JPA的時候,我們首先認(rèn)識下Hibernate。Hibernate是數(shù)據(jù)訪問解決技術(shù)的絕對霸主,使用O/R映射技術(shù)實現(xiàn)數(shù)據(jù)訪問,O/R映射即將領(lǐng)域模型類和數(shù)據(jù)庫的表進(jìn)行映射,通過程序操作對象而實現(xiàn)表數(shù)據(jù)操作的能力,讓數(shù)據(jù)訪問操作無須關(guān)注數(shù)據(jù)庫相關(guān)的技術(shù)
    2022-08-08
  • java分頁攔截類實現(xiàn)sql自動分頁

    java分頁攔截類實現(xiàn)sql自動分頁

    這篇文章主要為大家詳細(xì)介紹了java分頁攔截類可以實現(xiàn)sql自動分頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Mybatis核心組成部分之SQL映射文件揭秘詳解

    Mybatis核心組成部分之SQL映射文件揭秘詳解

    MyBatis真正的力量是在映射語句中,下面這篇文章主要給大家介紹了關(guān)于Mybatis核心組成部分之SQL映射文件揭秘的相關(guān)資料,現(xiàn)在分享給大家,給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 淺談ArrayList和LinkedList到底誰更快

    淺談ArrayList和LinkedList到底誰更快

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著ArrayList和LinkedList到底誰更快展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下
    2021-06-06
  • java多態(tài)注意項小結(jié)

    java多態(tài)注意項小結(jié)

    面向?qū)ο蟮娜筇匦裕悍庋b、繼承、多態(tài)。從一定角度來看,封裝和繼承幾乎都是為多態(tài)而準(zhǔn)備的。今天通過本文給大家介紹java多態(tài)注意項總結(jié),感興趣的朋友一起看看吧
    2021-10-10
  • Java 中的 String對象為什么是不可變的

    Java 中的 String對象為什么是不可變的

    String對象是不可變的,但這僅意味著你無法通過調(diào)用它的公有方法來改變它的值。本文給大家介紹java中的string對象為什么是不可變的,需要的朋友一起了解了解吧
    2015-10-10
  • Java實現(xiàn)的AES256加密解密功能示例

    Java實現(xiàn)的AES256加密解密功能示例

    這篇文章主要介紹了Java實現(xiàn)的AES256加密解密功能,結(jié)合完整實例形式分析了Java實現(xiàn)AES256加密解密功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02

最新評論

富平县| 崇信县| 桃园市| 翼城县| 新津县| 林甸县| 宣恩县| 阿城市| 浏阳市| 玛多县| 朝阳市| 遂平县| 灵璧县| 武宁县| 乌拉特中旗| 南涧| 三明市| 辰溪县| 牡丹江市| 郴州市| 铜陵市| 乐清市| 平山县| 清涧县| 高要市| 凌源市| 万山特区| 腾冲县| 新邵县| 宜春市| 东乌珠穆沁旗| 通城县| 漳浦县| 洛隆县| 沂水县| 南投市| 余姚市| 田林县| 鄂伦春自治旗| 昌黎县| 平阴县|