Grails 1.1 运行时Spring与Beans DSL - 使用 Spring命名空间

从Spring 2.0开始,通过XML命名空间可以更方便的使用Spring的各种特性.

如果使用BeanBuilder, 你可以先声明所要使用的Spring命名空间:xmlns context:"http://www.springframework.org/schema/context"然后调用与命名空间名称和属性匹配的方法:context.'component-scan'( 'base-package' :"my.company.domain" )通过Spring的命名空间可以做很多有用的事,比如查找JNDI资源:xmlns jee:"http://www.springframework.org/schema/jee" jee.'jndi-lookup'(id:"dataSource", 'jndi-name':"java:comp/env/myDataSource")上面的例子通过查找JNDI创建一个 dataSourcebean对象. 通过Spring命名空间,你可以在BeanBuilder中直接访问Spring AOP功能比如下面的代码:class Person { int age; String name; void birthday() { ++age; } } class BirthdayCardSender { List peopleSentCards = [] public void onBirthday(Person person) { peopleSentCards << person } }你可以定义一个AOP aspect pointcut来监测对 birthday() 方法的所有调用:xmlns aop:"http://www.springframework.org/schema/aop" fred(Person) { name = "Fred" age = 45 } birthdayCardSenderAspect(BirthdayCardSender) aop { config("proxy-target-class":true) { aspect( id:"sendBirthdayCard",ref:"birthdayCardSenderAspect" ) { after method:"onBirthday", pointcut: "execution(void ..Person.birthday()) and this(person)" } } }