Last active
August 29, 2015 13:57
-
-
Save chbaranowski/9582002 to your computer and use it in GitHub Desktop.
Spring groovy beans context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package beans | |
import org.springframework.core.io.ClassPathResource | |
// load configuration from the classpath | |
def url = new ClassPathResource('beans/simple.config').URL; | |
def config = new ConfigSlurper().parse(url); | |
beans { | |
// import a beans form a xml context configuration | |
importBeans("classpath:/beans/default-context.xml") | |
// use component scan in package beans | |
xmlns([ctx:'http://www.springframework.org/schema/context']) | |
ctx.'component-scan'('base-package': 'beans') | |
// define a spring bean | |
groovyBean(MyBean) { bean -> | |
bean.initMethod = 'initBeanProperties' | |
bean.scope = 'prototype' | |
name = "groovy bean" | |
} | |
// define a bean the smart way | |
smartBean SmartBean, | |
name: "SMART Bean", | |
bean: groovyBean | |
// define a bean with constructor arguments | |
constructorInjectionBean ImmutableBean, | |
"immutable bean", | |
groovyBean, | |
{ bean -> | |
bean.scope = 'singleton' | |
} | |
// bean reference to a service, the name of the | |
// service bean is defined in the configuration. | |
serviceUserBean(BeanRefService) { | |
simpleService = ref(config.service) | |
} | |
// define more then one bean e.g. in a loop | |
config.listeners.each { listener -> | |
"$listener" Listener, name: listener | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment