package com.wisely.dsl;
public class DemoService {
private String msg;
public String sayHello(){
return "hello "+msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
import com.wisely.dsl.DemoService//import要注册为bean的类
//所有的bean的声明放在beans下
beans{
//demoService为bean name,DemoService为类本身,msg = "world"为注入的属性
demoService(DemoService){
msg = "world"
}
}
package com.wisely.dsl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:com/wisely/dsl/DemoConfig.groovy")
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.dsl");
DemoService ds =context.getBean(DemoService.class);
System.out.println(ds.sayHello());
context.close();
}
}
输出结果
Hello World
原文:http://wiselyman.iteye.com/blog/2213387