14点睛Spring4.1-脚本编程

时间:2015-05-21 02:18:54   收藏:0   阅读:363

14.1 Scripting脚本编程

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

<?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

原文:http://wiselyman.iteye.com/blog/2212678

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!