I have no special talent. I'm only passionately curious - Albert Einstein
Grails SpringBeanListingController Comment on Grails SpringBeanListingController 0

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
	}
}


0 comments

Comments are currently disabled

About

David Malone is a Java developer residing in the Twin Cities area.  He has been developing enterprise applications since 2004.  This is his personal blog, as well as his design and development workspace.