转发:https://www.iteye.com/blog/wiselyman-2212678

14.1 Scripting脚本编程

  • 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源码直接可运行;
  • 如果我们经常需要修改的某些代码,每一次我们至少要进行编译,打包,重新部署的操作,步骤相当麻烦;
  • 如果我们的应用不允许重启,这在现实的情况中也是很常见的;
  • 在spring中使用脚本编程给上述的应用场景提供了解决方案,即动态加载bean;
  • spring支持脚本语言包含JRuby,Groovy,BeanShell;
  • 本例以spring主推的Groovy语言作为示例;
  • 动态加载bean目前暂不支持java config(应该在spring4.2版本支持,参见:Add support for dynamic languages refreshable beans in @Configuration classes),暂且使用xml配置()

14.2 示例

14.2.1 增加groovy语言支持包到maven

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.3</version>
</dependency>

14.2.2 演示接口

package com.wisely.script;

public interface DemoService {
public String sayHello();
}

14.2.3 使用groovy作为接口实现

import com.wisely.script.DemoService
class DemoServiceImpl implements DemoService{
def msg
String sayHello(){
return 'hello'+msg+' ok' //第一次打印后修改成为'hello'+msg+' not ok'
}
}

14.2.4 使用xml配置bean

  • script-source指定groovy源码地址
  • refresh-check-delay指定刷新时间
  • lang:property可注入值到groovy bean,包含普通值或者spring的bean
<?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-4.1.xsd">
<lang:groovy id="demoService"
script-source="com/wisely/script/DemoService.groovy"
refresh-check-delay="5000">
<lang:property name="msg" value="1234"/>
</lang:groovy> </beans>

14.2.5 测试

package com.wisely.script;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:com/wisely/script/script.xml")//加载groovybean的配置文件
public class Main {
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.script");
DemoService ds = context.getBean(DemoService.class); System.out.println(ds.sayHello());
Thread.sleep(10000);
System.out.println(ds.sayHello());
context.close();
} }

说明 先执行sayHello输出groovy bean的执行结果,此时修改groovy bean的sayHello的内容,5000毫秒后会加载新的bean的内容,我们等10秒,等待新的bean被加载,然后输出新的sayHello

输出结果:

hello 1234 ok
hello 1234 not ok

14点睛Spring4.1-脚本编程的更多相关文章

  1. Redis 实战 —— 14. Redis 的 Lua 脚本编程

    简介 Redis 从 2.6 版本开始引入使用 Lua 编程语言进行的服务器端脚本编程功能,这个功能可以让用户直接在 Redis 内部执行各种操作,从而达到简化代码并提高性能的作用. P248 在不编 ...

  2. SHELL脚本编程的常识和VI常用技巧

    来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc3751808 ...

  3. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---14

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  4. Shell脚本编程30分钟入门

    Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...

  5. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  6. 【Linux】Shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  7. 脚本命令高级Bash脚本编程指南(31):数学计算命令

    题记:写这篇博客要主是加深自己对脚本命令的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 高等Bash脚本编程指南(31):数学盘算命令 成于坚持,败于止步 操作数字 factor ...

  8. 高级Bash脚本编程指南(27):文本处理命令(三)

    高级Bash脚本编程指南(27):文本处理命令(三) 成于坚持,败于止步 处理文本和文本文件的命令 tr 字符转换过滤器. 必须使用引用或中括号, 这样做才是合理的. 引用可以阻止shell重新解释出 ...

  9. 30分钟快速学习Shell脚本编程

    什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch ...

随机推荐

  1. Linux shell - 按时间和文件大小排序显示文件

    在工作中有这样的情况,需要显示所有的文件,按照时间先后或者文件大小先后排序显示 命令:ls 1.按时间排序显示文件 1 test@> ll -rt 2.按文件大小排序显示文件(文件大小单位:k, ...

  2. YAML_12 批量创建用户,分别设置用户组

    with_items标准循环 ansible]# vim add.yml --- - hosts: web2   remote_user: root   tasks:     - user:     ...

  3. php+ ueditor word粘贴上传

    最近公司做项目需要实现一个功能,在网页富文本编辑器中实现粘贴Word图文的功能. 我们在网站中使用的Web编辑器比较多,都是根据用户需求来选择的.目前还没有固定哪一个编辑器 有时候用的是UEditor ...

  4. C int类型的数组在内存中的地址示例

    #include <stdio.h> int main(void){ int age[5] = {5,6,7,20,99}; return 0; } //转换后 /*(gdb) p &am ...

  5. laravel 多控制器路由

    laravel 路由: ======================================= 公司的情况很不乐观...... 破产清算随时可能发生......

  6. Vue中的$emit组件事件运用

    话不多说上代码 vue>src>App.vue <template> <div id="app"> <!-- header --> ...

  7. js将数组分割成等长数组

    方法一: function group(array, subGroupLength) { let index = 0; let newArray = []; while(index < arra ...

  8. Python正则表达式【转载】

    原作者:LouieZhang 原文出处:https://www.cnblogs.com/LouieZhang/p/7399836.html 0x00 简介 正则表达式就是描述字符串排列的一套规则.利用 ...

  9. 【洛谷】P2261 [CQOI2007]余数求和

    题面?? 点我获得题面QAQ 我这个咕儿终于在csp初赛前夕开始学习数论了! 我是绝对不会承认之前不学数学是因为去年刚开始学OI的时候就跟yyq他们学莫比乌斯反演然后自闭的 分析 对于k mod i, ...

  10. VMware 下 Ubuntu 18.04 登录框消失解决

    问题:ubuntu18卡在登陆界面,但是没有出现选择登陆用户的框,只有紫色屏幕 解决方法:参考VMware 下 Ubuntu 18.04 登录框消失解决记录