SpringにGrovyを使用したダイナミックビーム
転載してから
http://blog.chenlb.com/2009/04/spring-use-groovy-dynamic-bean.html
springでgroovyなどの動的言語を使う利点は、サーバー上でgroovyファイルを変更するか、または新たにgroovyファイルを追加すると、新しい機能があります.いくつかの規則的な論理処理、ダイナミックなアプリケーションに対してgroovyを適用することができます.
例として、springではgroovyを使います.サブ環境spring 2.2.5、groovy-1.5.
1、アナログ業務インターフェース:
ハローchenlb@http://blog.chenlb.com
springでgroovyを使うには、依存するjarがあります.antlr-2.7.6.jar、aopalliance.jar、asm-23.2.3.jar、asm-comons-2.23.jar、asm-util-2.23.jar、groovy-1.5.jarはspringのlibの中にあります.もちろん、groovy-1.5.jarをgroovy-1.5.jarに変えてもいいです.
http://blog.chenlb.com/2009/04/spring-use-groovy-dynamic-bean.html
springでgroovyなどの動的言語を使う利点は、サーバー上でgroovyファイルを変更するか、または新たにgroovyファイルを追加すると、新しい機能があります.いくつかの規則的な論理処理、ダイナミックなアプリケーションに対してgroovyを適用することができます.
例として、springではgroovyを使います.サブ環境spring 2.2.5、groovy-1.5.
1、アナログ業務インターフェース:
package com.chenlb.groovy;
/**
*
*
* @author chenlb 2009-4-7 02:04:38
*/
public interface Hello {
String say(String name);
}
2、groovy実現:
package com.chenlb.groovy;
class GroovyHello implements Hello {
def blog;
String say(String name) {
return "Hello ${name}@${blog}"
}
void setUrl(String blog) {
this.blog = blog;
}
}
3、spring配置aplication Contront-groovy.xml:
<?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-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">
<lang:groovy id="groovyHello" script-source="classpath:com/chenlb/groovy/GroovyHello.groovy">
<lang:property name="blog" value="http://blog.chenlb.com"></lang:property>
</lang:groovy>
</beans>
<?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-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">
<lang:groovy id="groovyHello" script-source="classpath:com/chenlb/groovy/GroovyHello.groovy">
<lang:property name="blog" value="http://blog.chenlb.com"></lang:property>
</lang:groovy>
</beans>
4、運転:
package com.chenlb.groovy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext-groovy.xml");
Hello hello = (Hello) context.getBean("groovyHello");
System.out.println(hello.say("chenlb"));
}
}
package com.chenlb.groovy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext-groovy.xml");
Hello hello = (Hello) context.getBean("groovyHello");
System.out.println(hello.say("chenlb"));
}
}
結果:ハローchenlb@http://blog.chenlb.com
springでgroovyを使うには、依存するjarがあります.antlr-2.7.6.jar、aopalliance.jar、asm-23.2.3.jar、asm-comons-2.23.jar、asm-util-2.23.jar、groovy-1.5.jarはspringのlibの中にあります.もちろん、groovy-1.5.jarをgroovy-1.5.jarに変えてもいいです.