I have no special talent. I'm only passionately curious - Albert Einstein
March 04, 2011
Grails SpringBeanListingController
Posted by Dave Malone
in Grails,
Spring,
Java
I developed this Grails controller so I can inspect the Spring configuration at any time. It has some basic wildcard support for searching for beans I suspect have been configured in the Spring application context but may not actually be there.
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware;
class SpringBeanListingController implements ApplicationContextAware{
ApplicationContext applicationContext
def index = {
for(String name : applicationContext.getBeanDefinitionNames()){
Object bean = applicationContext.getBean(name)
println "bean name: $name"
println "bean type: " + bean.getClass().getName()
}
}
def beanByName = {
String name = params.id
println "looking for bean with name $name"
if(name.endsWith('*')){
name = name.replaceAll("\\*", "");
println "performing wildcard search for beans with names beginning with $name"
for(String beanName : applicationContext.getBeanDefinitionNames()){
if(beanName.startsWith(name)){
Object bean = applicationContext.getBean(beanName)
println "bean name: $beanName"
println "bean type: " + bean.getClass().getName()
}
}
}else{
println "performing equals search"
Object bean = applicationContext.getBean(name)
if(bean){
println "bean name: $name"
println "bean type: " + bean.getClass().getName()
}else{
println "bean name $name is invalid"
}
}
}
def beansByType = {
String className = params.id
Class type = Class.forName(className)
Map beanMap = applicationContext.getBeansOfType(type)
for(Map.Entry beanMapping : beanMap.entrySet()){
String name = beanMapping.key
Object bean = beanMapping.value
println "bean name: $name"
println "bean type: " + bean.getClass().getName()
}
}
void setApplicationContext(ApplicationContext applicationContext){
this.applicationContext = applicationContext
}
}
Comments are currently disabled