aspose file tools*
The moose likes Spring and the fly likes AbstractTransactionalSpringContextTests lazy loading beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "AbstractTransactionalSpringContextTests lazy loading beans " Watch "AbstractTransactionalSpringContextTests lazy loading beans " New topic
Author

AbstractTransactionalSpringContextTests lazy loading beans

Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi ,

I had written class which extends “AbstractTransactionalSpringContextTests” for testing my beans instantiated via spring framework .

I had annotated all java classes eligible for spring to load with @Service,@Autowire,@Qualifier in XML along with component scanning with pkg which has annotated classes .

Issue I face when I tried to inject properties (with autowire ) for classes which is not apart of component scanning ( I purposefully left those packages in scanning, as I am not interested in testing / loading the class)
Spring does because required is set to true by default in Autowired class.
Hence to resolve this issue , I added “default-lazy-init="true" in xml , which resolved my issue .

Even with “AbstractTransactionalSpringContextTests” we have that option using “setDependencyCheck(false) ".it is not working as expected .

I prefer way to do in test class level , rather than changing xml ( FYI going fwd we would like to use same XML for application and testing )


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Why not just put

@Autowired(required=false) in your code.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Since i woudl like to use same pojo for testing too ( i made required=false ) all beans will load on need basis in main app too ...

i got solution from below licnk , which works on manuplating DOm tree before spring intilaises

.Spring Link

Thanks Mark for help .
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Ganesh Gowtham wrote:Since i woudl like to use same pojo for testing too ( i made required=false ) all beans will load on need basis in main app too ...

i got solution from below licnk , which works on manuplating DOm tree before spring intilaises

.Spring Link

Thanks Mark for help .


I still don't think lazy loading is your real true solution to your problem. Yes, it solves it, but isn't what lazy loading is there for to resolve.

In your main application if you put required=false doesn't mean that it won't autowire it. It means if you don't have a bean of that type, that is fine, it is not set and null. But if there is a bean of that type, then it is set to that bean.

Mark
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: AbstractTransactionalSpringContextTests lazy loading beans