Hello everybody,
I'm having some troubles understanding the way I can modify and reload/refresh the context at runtime(without re-deploying my app).
I need to add some bean definition to the context, depending from user behaviour (read: when a user submit a form, i have to add some bean to the application context, depending from user submitted data), then I (suppose) have to refresh the context to get my new beans available for any kind of operation (in detail: scheduling with Quartz).
How can I do that?
At the moment I tried to do something like this:
in my ApplicationContextAware implementing class....
and here's the registerBean(Fonte f) definitionCode:XmlWebApplicationContext ctx = (XmlWebApplicationContext) context; System.out.println("bean count before"+ctx.getBeanDefinitionCount()); registerBean(f); //f is my data Object-see below ctx.refresh(); System.out.println("bean count after"+ctx.getBeanDefinitionCount()); CronTriggerBean c= (CronTriggerBean) ctx.getBean(f.getNome()+"Job");
The output is:Code:private void registerBean(Fonte f){ try { XmlWebApplicationContext ctx = (XmlWebApplicationContext) context; DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ctx.getBeanFactory(); BeanDefinitionBuilder simpleTriggerBeanBuilder = BeanDefinitionBuilder.rootBeanDefinition(org.springframework.scheduling.quartz.CronTriggerBean.class); simpleTriggerBeanBuilder.addPropertyValue("cronExpression", "0/10 * * * * ?"); beanFactory.registerBeanDefinition(f.getNome()+"Job",simpleTriggerBeanBuilder.getBeanDefinition()); }catch(Exception e){e.printStackTrace();} }
-bean count is always the same. Should be incremented by 1, doesn't?bean count before43
bean count after43
org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'ffffffffJob' is defined
-'ffffffffJob" is the user submitted data
What's wrong with that?
Is this the right way to do what I'm trying to do(modify and refresh the context) ?
Any suggestion?
Thank you in advance.
Regards.


Reply With Quote
