Showing posts with label GenericApplicationContext. Show all posts
Showing posts with label GenericApplicationContext. Show all posts

Monday, October 21, 2013

Registering new bean classes in grails application programmatically by registerBeanDefinition

As part of amazing goodies of grails is the ability to extend your applications - registering new classes problematically is a breeze


GenericApplicationContext context = new GenericApplicationContext();

context.setParent(applicationContext);
/* OR */
context.setParent(grailsApplication.mainContext);

// Create class from string
Class clazz = new GrailsAwareClassLoader().parseClass(classString);

// First create a bean definition
def myBeanDef = new GenericBeanDefinition()

// Set bean class
myBeanDef.setBeanClass(clazz)

// Set scope
myBeanDef.setScope(BeanDefinition.SCOPE_SINGLETON)

context.registerBeanDefinition("beandefName", myBeanDef);