Groovy DSL
发布时间:2020-12-14 16:44:10 所属栏目:大数据 来源:网络整理
导读:Groovy DSL Spring 4.x的一个新特性是使用Groovy的语言来配置Spring的bean; 这意味着我们构造一个spring的bean又多了一种方式,包括如下: xml配置 java config(@Bean) @Component,@Service,@Repository,@Controller系列 Groovy DSL 19.2 示例 19.2.1 演示类 p
Groovy DSL
19.2 示例19.2.1 演示类package com.wisely.dsl;
public class DemoService {
private String msg;
public String sayHello(){
return "hello "+msg;
}
getMsg() {
return msg;
}
void setMsg(String msg) {
this.msg = msg;
}
}
19.2.2 使用groovy配置DemoService为bean
import com.wisely.dsl.DemoService//import要注册为bean的类
//所有的bean的声明放在beans下
beans{
//demoService为bean name,DemoService为类本身,msg = "world"为注入的属性
demoService(DemoService){
msg = "world"
}
}
19.2.3 测试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")
Main {
static 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
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |