Following is the implementation of scripting of beans setter
injection in spring using groovy script.
Implementation:
Drink:
package com.lnn.groovy;
public interface Drink {
public void drink();
}
Drinker:
package com.lnn.groovy;
public class Drinker {
private Drink drink;
public void setDrink(Drink
drink) {
this.drink = drink;
}
public void drink() {
System.out.println("Drinking
from Java!");
drink.drink();
}
}
MyDrink.groovy:
class MyDrink implements
com.lnn.groovy.Drink {
void drink() {
print content;
}
String content;
}
Groovy.xml:
<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-3.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
<bean id="drinker"
class="com.lnn.groovy.Drinker">
<property name="drink"
ref="drink" />
</bean>
<lang:groovy id="drink"
script-source="classpath:com/lnn/groovy/MyDrink.groovy">
<lang:property name="content"
value="Drinking from Groovy using Setter!"
/>
</lang:groovy>
</beans>
GroovyTest.java:
package com.lnn.groovy;
import
org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class GroovyTest {
public static void main(String[]
args) {
ApplicationContext
context = new ClassPathXmlApplicationContext("groovy.xml");
Drinker
drinker = (Drinker) context.getBean("drinker");
drinker.drink();
}
}
No comments:
Post a Comment