`
wiselyman
  • 浏览: 2083017 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81303
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130228
社区版块
存档分类
最新评论

14点睛Spring4.1-脚本编程

 
阅读更多

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

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


0
1
分享到:
评论
9 楼 readxuxuegang 2017-09-17  
博主你好。如果groovy的代码保存在数据库里,不是文件里,这样怎么搞
8 楼 高桌子矮板凳 2015-11-30  
高桌子矮板凳 写道
博主你好。用你的例子,我测试的结果:打印的都是ok嘛。不知道什么情况
部分输出如下:

信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@dfd3711: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,main,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.scripting.config.scriptFactoryPostProcessor,demoService]; root of factory hierarchy
hello 1234 ok
hello 1234 ok
十一月 30, 2015 2:12:04 下午 org.springframework.context.support.AbstractApplicationContext doClose


好意思,没改脚本
7 楼 高桌子矮板凳 2015-11-30  
博主你好。用你的例子,我测试的结果:打印的都是ok嘛。不知道什么情况
部分输出如下:

信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@dfd3711: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,main,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.scripting.config.scriptFactoryPostProcessor,demoService]; root of factory hierarchy
hello 1234 ok
hello 1234 ok
十一月 30, 2015 2:12:04 下午 org.springframework.context.support.AbstractApplicationContext doClose

6 楼 wiselyman 2015-05-21  
laoniu85 写道
恩。。我的编译器(idea) 编译出来之后
源代码目录中的非文件是不会被拷贝到输出目录(classpath)中的
所以有这问题

你要点一下那个写着"01 10 01"的小图标
5 楼 wiselyman 2015-05-21  
laoniu85 写道
博主:我大量搬运了你的Spring4.1点睛系列到自己的qq空间。


没事,记得注明出处哈!
4 楼 laoniu85 2015-05-21  
博主:我大量搬运了你的Spring4.1点睛系列到自己的qq空间。
3 楼 laoniu85 2015-05-21  
恩。。我的编译器(idea) 编译出来之后
源代码目录中的非文件是不会被拷贝到输出目录(classpath)中的
所以有这问题
2 楼 wiselyman 2015-05-20  
laoniu85 写道
xml和groovy文件需要放到resource的路径中,否则会出现找不到xml和脚本的问题。

代码我是测试运行过的,只要指定正确的classpath路径,放哪都可以
1 楼 laoniu85 2015-05-20  
xml和groovy文件需要放到resource的路径中,否则会出现找不到xml和脚本的问题。

相关推荐

Global site tag (gtag.js) - Google Analytics