转发: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. HiveQL 查询

    一.select ...... from 语句 1.使用正则表达式来指定列 1)从表stocks中选择symbol列和列名以price作为前缀的列 select symbol,`price.*`  f ...

  2. SaltStack 在 Windows 上的操作基础

    SaltStack 在 windows上的操作基础 1.删除文件: salt '172.16.3.11' file.remove 'D:\downup\111.msu' 2.删除文件夹 salt '1 ...

  3. JSP迭代标签

    1. 新建LoopTag类,代码如下: package bid.zhazhapan.fims.tag; import java.io.IOException; import java.util.Col ...

  4. FCS省选模拟赛 Day3

    Description  Solution T1 game 咕咕咕 T2 string fail树各个节点的深度之和怎么求? 我们考虑每个前缀的深度是什么 发现这个值就相当于有多少个前缀等于它的后缀 ...

  5. 我为什么选择Vim

    总看到一些飞快敲击键盘而不用鼠标的时候你可以很羡慕和佩服,其实这完全没有必要.就像一个吉他手熟练地弹吉他有必要羡慕吗?一个瓦匠熟练地砌砖有必要羡慕吗?这些都是他们赖以生存的工具而已,熟练地运用工具是理 ...

  6. 批量更新表注释 mysql

    -- 生成更新语句 SELECT CONCAT( 'ALTER TABLE ', T2.table_name, ' COMMENT ''', T1.TABLE_COMMENT, ''';' ) sql ...

  7. 利用ssh 删除远程服务器文件

    ssh 登录,利用命令 rm 登录ssh ssh name@10.202.9.11 rm 命令删除文件 rm file rm -rf 删除文件夹 rm -rf fold 删除当前文件夹内容,保留当前文 ...

  8. Mybatis异常-java.lang.IllegalArgumentException: invalid comparison:java.util.Date and java.lang.String

    原因:在Mapper.xml中对非字符串类型的数据进行了是否为空判断,如date类型的数据Create_Date != '',decimal类型的数据price != ''都会报这个错误 只有字符串才 ...

  9. spark学习记录-1

    mapreduce的限制 适合“一趟”计算操作 很难组合和嵌套操作符号 无法表示迭代操作 ======== 由于复制.序列化和磁盘IO导致mapreduce慢 复杂的应用.流计算.内部查询都因为map ...

  10. layer.msg如何让按钮的回调执行完毕后弹框不自动关闭

    问题出现:我点击“确定”时会验证“新手机号码”,如果验证不通过则不给该弹框关掉,但是实际操作时,不管验证怎么样,点击“确定”之后该弹框都会关掉. 之前的写法: layer.open({         ...