wood burning stoves*
The moose likes Spring and the fly likes Spring autowired Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring autowired" Watch "Spring autowired" New topic
Author

Spring autowired

karibasappa Guttur Chikkaveerappa
Greenhorn

Joined: Jan 26, 2013
Posts: 11
Hi All,

I have one question on spring beans dependency injection.

I have one bean which needs to be loaded during startup and inside this bean i have one dependency bean which i want to load during the first request.

so i made the lazy-init true for that bean. but because of @autowired annotation when first bean is loaded it tries to find the dependency bean which is not available in the contained(lazy loading is set true).

so how can i inject this dependency to load during first request ?

suggest me except Application context /bean factory loading

Thank you in advance
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

By definition an eagerly loaded bean can not have dependencies on a lazy one or it will nullify the lazy load. My first question is can you describe why the need for lazy loading? Lazy loading is often misused and I would take a close look at if that is the best solution, it is even more suspicious since you have a bean that you apparently think needs to be eager yet has a dependency on the lazy. Most likely something is wrong with your dependencies or that bean should not really be lazy in the first place.

Assuming lazy loading does make sense, you can look here for one work around.
http://forum.springsource.org/showthread.php?124290-Spring-Annotations-Lazy-Autowired


[How To Ask Questions][Read before you PM me]
karibasappa Guttur Chikkaveerappa
Greenhorn

Joined: Jan 26, 2013
Posts: 11
Hi Bill,

I know about what you explained, but my requirement is like that.

i am loading all the beans through package using context:component-scan and in that one bean has dependency on lazy loading bean. so i want that dependency to lazy load it.

because lazy loading bean is calling rmi, but i dont want this bean to load during application start-up, it should load during call to rmi in the code...
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

lazy load should be reserved for very expensive initialization scenarios which make sense to put off. If you think about it, usually it makes more sense to pay the piper at startup then at runtime when a request comes in, unless of course it is unlikely that request will ever come. If start-up performance is a huge concern you may want to consider lazy loaded beans. Another disadvantage of them, is that if something goes wrong during initialization you will not know at start-up but rather it may pop up hours, days or weeks later. I can count the number of times I have used lazy initialized beans on one hand.

That said with RMI (I assume you are using Spring for this) have a look at the lookupStubOnStartup property of the RmiProxyFactoryBean. Try setting it to false.

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Here is a link (copy and paste it into the browser rather than click it. Seems to leave out the last ) for me otherwise)
http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/remoting/rmi/RmiClientInterceptor.html#setLookupStubOnStartup(boolean)
karibasappa Guttur Chikkaveerappa
Greenhorn

Joined: Jan 26, 2013
Posts: 11
ya you are correct, i set it false and its working fine as expected.

Thanks.
Jay Lim
Greenhorn

Joined: Jul 04, 2012
Posts: 5
Hi,

I am having the same issue. I tired setting lookup-home-on-startup="false", it did solves the initialization issue.
However, when my client tries to call the remote server, it encounters an error: No EJB receiver available for handling combination for invocation context.
By setting the above to true, it will work.
Any idea how to resolve this?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

This sounds JBOSS related more than Spring. Try creating a topic clearly specifying your configuration and what you are trying to do in our JBOSS forums.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring autowired
 
Similar Threads
spring - autowire="constructor"
How to create NON SINGLETON beans in Spring ?
AbstractTransactionalSpringContextTests lazy loading beans
XML Error in the .war file
@Autowired for properies in 3.1