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
相关推荐
国元点睛-20230825-国元期货-23页
眼睛穴位图-睛明穴-取穴方法 方便经常上网的朋友使用
六爱护眼睛---复式统计图(第1课时).doc
图像与眼睛(2009-04-1.pdf
让消费者眼睛亮起来--稀世宝矿泉水整合营销策划案例(doc 20).doc
包含了谷歌眼睛的全部核心代码,可以用记事本、Dreamweaver、写字板、html等打开
这是一款保护眼睛的小软件,绿色软件,不需安装即可使用,试一下吧。
[最新]八年级上册物理:5.4-眼睛和眼镜-精品教案.pdf
闭着眼睛记单词-英语基础《英语1000高频词汇》
使用OpenCV1.0实现了眼睛状态识别,首先用harr检测眼睛,然后用模板匹配的方法标记眼睛状态。 程序需要使用Eyes45x11.xml Mouth25x15.xml Nose25x15.xml,将其放到C:\Program Files\OpenCV\data\haarcascades目录下...
点睛网论坛整站源码程序+10W+数据(送主动推送工具) 详细安装教程https://www.songzifc.cn/post/5398.html 原点睛网赚论坛整站源码数据泄漏 效果演示https://www.i6wz.cn/ 【全新优化】点睛网论坛整站程序 带全套...
ios开发小案例:从txt文件中读取地图脚本信息.
opencv用于检测带眼镜的眼睛haarcascade_eye_tree_eyeglasses.xml
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
opencv用于检测眼睛。haarcascade_eye.xml文件
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
点睛-文本各种编码方式转变.7z
4.1人的眼睛.pdf
软件名:点睛文本编码查询 功能:本软件用于将文本的字符串转换为 Text、ASCII、Default、Unicode、BigEndianUnicode、UTF-8、UTF-7、GBK、BIG5 之间的互相转换,用于文本查询。转换结果使用十六进制表示。
上网保护眼睛--网页底色修改.2011-11-25最新整理,2011-11-25最新整理...