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 ...
随机推荐
- ElasticSearch使用C#操作文档
一.ElasticSearch的.net客户端驱动程序 ElasticSearch官方网站提供了两个.net客户端驱动程序,其中Elasticsearch.Net是一个非常底层且灵活的客户端驱动程序, ...
- 一个有趣的js隐式转换的问题
一个有趣的js隐式转换的问题 在chrome的控制台中打印一下表达式 [] + {} //结果为 [object object] 然后调整顺序打印 {} + [] //结果为 0 然后将两个表达式组合 ...
- 数据结构实验之排序五:归并求逆序数(SDUT 3402)
归并排序详解(戳我). 以下是搬了别人的. #include<stdio.h> #include<stdlib.h> long long sum = 0; int a[1000 ...
- jmeter正则中常见的转义字符-笔记三
背景和目的 接口测试过程中难免会遇到由于有转义符号正则表达式提取不出来的情况,根据小伙伴们的分享和参考自己实践总结了多种情况 参考 首先,感谢如下常见转义字符,感谢提供参考的小伙伴 参考:https ...
- mac webstorm 安装破解
下载: 链接:https://pan.baidu.com/s/1A1afhcpPWMrQtOr1Suqs-g 密码:5r7b 激活码 K6IXATEF43-eyJsaWNlbnNlSWQiOiJLN ...
- Java RabbitMQ配置和使用,基于SpringBoot
package rabbitmq.demo; import com.rabbitmq.client.AMQP; import org.junit.Test; import org.junit.runn ...
- 任意模数FFT
任意模数FFT 这是一个神奇的魔法,但是和往常一样,在这之前,先 \(\texttt{orz}\ \color{orange}{\texttt{matthew99}}\) 问题描述 给定 2 个多项式 ...
- 13.mysql数据库
1.mysql数据库建立 yum install mysql-server mysql -u root mysqladmin ...
- lol英雄时刻
2019.5.30 翻出了当年人生中第一次LOL五杀截图...我用佐伊拿五杀了! 第一次五杀超激动的
- Java8中LocalDate的使用---项目中日期处理
// 获取当前日期 LocalDate now = LocalDate.now(); // 设置日期 LocalDate now2 = LocalDate.of(2099, 2, 28); // 解析 ...