14点睛Spring4.1-脚本编程
转发: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-脚本编程的更多相关文章
- Redis 实战 —— 14. Redis 的 Lua 脚本编程
简介 Redis 从 2.6 版本开始引入使用 Lua 编程语言进行的服务器端脚本编程功能,这个功能可以让用户直接在 Redis 内部执行各种操作,从而达到简化代码并提高性能的作用. P248 在不编 ...
- SHELL脚本编程的常识和VI常用技巧
来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc3751808 ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---14
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- Shell脚本编程30分钟入门
Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- 【Linux】Shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- 脚本命令高级Bash脚本编程指南(31):数学计算命令
题记:写这篇博客要主是加深自己对脚本命令的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 高等Bash脚本编程指南(31):数学盘算命令 成于坚持,败于止步 操作数字 factor ...
- 高级Bash脚本编程指南(27):文本处理命令(三)
高级Bash脚本编程指南(27):文本处理命令(三) 成于坚持,败于止步 处理文本和文本文件的命令 tr 字符转换过滤器. 必须使用引用或中括号, 这样做才是合理的. 引用可以阻止shell重新解释出 ...
- 30分钟快速学习Shell脚本编程
什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch ...
随机推荐
- mysql模糊查询多个字段
SELECT * FROM nst_t_conferencehis WHERE ConferName REGEXP '军工|军资';
- Kubernetes 学习7 Pod控制器应用进阶2
一.容器探测器 1.所谓的容器探测无非就是我们在里面设置了一些探针,或者称之为传感器来获取相应的数据作为判定其存活与否或就绪与否的标准,目前k8s所支持的存活性和就绪性探测方式都是一样的. 2.k8s ...
- windows下命令行切换目录
1.切换目录 C:\Users\MACHENIKE> cd H:/www C:\Users\MACHENIKE>H: H:\www> 2.查看目录文件 H:\www>dir
- declare/typeset
用来生命变量的,作用完全一样. 不像C语言那样严谨的语法,变量在使用前必须声明. 但是在shell中对变量的声明要求并不高,因为shell弱化了变量的类概念,所以shell被称为弱类型语言, 声明变量 ...
- Linux 检测出口IP地址
找了几个 linux 下检测出口地址的(前提是能访问外网). curl ifconfig.me curl ipinfo.io/ip curl ipecho.net/plain
- YII框架的事件机制
一.什么是事件机制 解释:发生了一件事情,然后某些东西对这件事作出反应. 例子:假设发生了A同学结婚事件,然后B同学给份子钱反应,那么,B是怎么知道(监听)A事件的发生了呢,有两种办法. 扫描式:B不 ...
- P5025 [SNOI2017]炸弹
原题链接 https://www.luogu.org/problem/P5025 闲话时刻: 第一道 AC 的黑题,虽然众人皆说水... 其实思路不是很难,代码也不是很难打,是一些我们已经学过的东西 ...
- [ZJOI2007][BZOJ1060]时态同步
Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数 字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路 ...
- switchcase的用法
<script> var level = prompt("请输入员工评级"); var salary = 5000; switch (level) { case &qu ...
- windows命令行操作
一.打开方式 - 开始菜单 --> 运行 --> 输入cmd --> 回车 二.常用的指令 dir - 列出当前目录 ...