-
Feb 25th, 2010, 12:06 AM
#1
@Bean is not read on applicationContext.refresh ?
I need to create a new bean on the fly and decided to use annotation based approach. The way how I found doing is by using @Configuration. So we would be adding a new bean config into this file at runtime (without restarting web server, just by changing classfile and refreshing context)
while working with this I found that @Bean is read on applicationContext refresh.
Here is an example I tried to remove @Bean config on the fly but not worked until I restart server.
Here is the annotation configuration class created using eclipse RAD tool.
@Configuration
public class config {
@Bean("customer")
public Customer createCustomer(){
return new Customer();
}
}
Here is the Customer.java, a normal bean
public class Customer {
Integer id;
// accessor methods.
}
I deployed this on to apache tomcat web server I see that customer bean is created application context on server start up.
I came back to eclipse and modified config.java, I removed the @Bean annotation and copied compiled .class file to tomcat server,
and I din't restart server but refreshed only the applicationContext using following code.
applicationContext.refresh();
I was expecting customer bean will no more exist in applicationContext but I was wrong still customer bean is created.
And then, I restarted the webserver now customer bean is not created.
Any idea on this, why is @Bean annotation not read on applicationcontext refresh ?
This is not same while using stereo type @service, create a class with @service on top of class and refresh application context it will be created on the fly.
Thanks,
-Amar
-
Feb 25th, 2010, 01:11 AM
#2
Those annotations are handled differently. @Configuration is like a normal applicationcontext.xml file and that isn't reloaded, it simple clears the beans and reinstantiates them from the BeanDefinitions.
Now if you add a bean with @Service the classpath is scanned by @Component on a refresh and the bean will appear.
-
Feb 25th, 2010, 01:37 AM
#3
Hi Marten,
Thanks for the reply.
As you mentioned
"@Configuration is like a normal applicationcontext.xml file and that isn't reloaded"..
I am not really agreed on to this, because I have seen applicationcontext file is reloaded on refresh. and the log entries says context file is being reloaded.
Try this, start application context using applicationContext.xml file, then modify applicationContext.xml with adding new bean, refresh context and you will have new bean available in the context.
So with this I was expecting @Bean to be read again.
Thanks,
-Amar
-
Feb 25th, 2010, 02:07 AM
#4
Well it depends where you are, if you are on an appserver, you have a high change of files being cached and you don't see the new version (at least tomcat does this for files in the WEB-INF directory) and you still have the old beans/config.
My guess is due to the nature of handling (proxying of the @Configuration class) the new config isn't detected, so this might be some kind of caching/dynamic creation issue. If you can create a small test case which shows this issue then register a JIRA
-
Feb 25th, 2010, 11:44 PM
#5
Yes you are right. App server does not pick up files with new version. I tried same with standalone application it worked. We need this working over app server. I found other way doing it.
Thanks
Thanks,
-Amar
-
Feb 27th, 2010, 08:05 AM
#6
Well it works if you store your xml files outside of the paths cached by the app server. So if you put them somewhere on your file system and load them with the file: prefix it should work. However with class files this isn't going to be possible.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules