工程结构图
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.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">
<lang:groovy id="sayHello" script-source="classpath:groovy/SayHello.groovy"
refresh-check-delay="5000">
<!-- customizer-ref="scripletCustomizer" classpath: -->
<lang:property name="user" ref="user3" />
<lang:property name="hello" value="Welcome " />
</lang:groovy>
<bean id="ser1" class="com.org.test.Ser1">
<property name="user" ref="user1" />
</bean>
<bean id="user1" class="groovy.entity.User">
<property name="name" value="Mr X" />
</bean>
<bean id="user2" class="groovy.entity.User">
<property name="name" value="Mr Y" />
</bean>
<bean id="user3" class="groovy.entity.User">
<property name="name" value="Mr Z" />
</bean>
<lang:groovy id="helloWorldService">
<lang:inline-script>
<![CDATA[
import groovy.impl.SayHelloImpl;
class HelloWorldServiceImpl implements SayHelloImpl {
String name
String sayHello()
{
println "Hello $name. Welcome to Scripting in Groovy"
}
}
]]>
</lang:inline-script>
<lang:property name="name" value="meera" />
</lang:groovy>
</beans>
1>
package com.org.test;
import groovy.entity.User;
import groovy.impl.SayHelloImpl;
public class Ser1 implements SayHelloImpl
{
User user;
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
@Override
public Object sayHello()
{
System.out.println("||" + user.getProperty("name"));
return "";
}
}
2>
package com.org.test;
import groovy.entity.User;
import groovy.impl.SayHelloImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestGSpring
{
@Test
public void test() throws InterruptedException
{
MessageSource resources = new ClassPathXmlApplicationContext(
"ApplicationContext.xml");
String message = resources.getMessage("message", null, "Default", null);
System.out.println(message);
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"ApplicationContext.xml");
User user = (User) applicationContext.getBean("user1");
System.out.println(user.getProperty("name"));
System.out.println(applicationContext.getBean("sayHello").getClass());
SayHelloImpl sayHello = null;
while (true)
{
sayHello = (SayHelloImpl) applicationContext.getBean("sayHello");
sayHello.sayHello();
break;
// Thread.currentThread().sleep(2000);
}
System.out.println("##############################");
sayHello = (SayHelloImpl) applicationContext
.getBean("helloWorldService");
sayHello.sayHello();
}
}
3>
package groovy.entity;
public class User{
def name;
}
4>
package groovy.impl
public interface SayHelloImpl{
def Object sayHello();
}
5>
package groovy
import groovy.impl.SayHelloImpl
public class SayHello implements SayHelloImpl {
def hello;
def user;
def sayHello(){
println hello+','+user.name+'@';
}
}
https://172.19.49.185:3690/svn/AS_BOSS_SVN/JFZW/trunk/docs/开发文档/培训资料/BME/敏捷开发/zhao/资料