File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Dependency injection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Dependency injection problem" Watch "Dependency injection problem" New topic
Author

Dependency injection problem

Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
I have an application with Spring framework, using AnnotationSessionFactoryBean. I'm also using Hibernate.



The problem is boReporte is not being instantiated by Spring. If before the problem line I add this:

the application continues to run and the DAO class is instantiated normally in my BO. I don't understand why this happens. I also don't understand why I have to set hibernate's session factory via code, I wasn't able to do it via my applicationContext.xml, which contains the sessionFactory mentioned in the code above.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It'd be handy to include the config file.
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
here it is:
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Why there is no boReporte in application context file?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
Why there should be? As far as I know, @Component annotation means the class will be auto-detected when using annotation-based configuration.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Do you need to add the bean names to the Annotations?

Did you check to see what exceptions were thrown, in the logs?

For the Hibernate stuff, why are you using "Annotated Packages" They are not used to find Entities. Most people think that that property is for that, but it isn't.

Someone actually had to extend the class so that you can state what package your Entities reside in so you don't have to list out each and every entity class in your xml.
http://baldercm.blogspot.com/2008/06/howto-setup-spring-for-hibernate.html

they actually plan on added that in Spring 3.x

But for your issue, i know there had to be an exception thrown and we need to see what that it, that will tell us why Spring either missed the class, or had a problem with something.

So it might be in your Tomcat console or Tomcat log.

Mark


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

Joined: Jun 04, 2009
Posts: 155
Well, I had an error in my application. I was instantiating ReporteService with new(), that's why BO was null. If I remove it the application still doesn't run, I get a NullPointerException in my getReporteService method in my pageBean. Again, if I try to use it works. The main problem is, why do I have to use the context to instantiate my beans, at some moment? Wasn't it supposed to be Spring's task?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You have to get a class through Spring if you expect Spring to be able to do anything with/to it, like set properties etc.

If you instantiate a class with "new" then Spring has no way of knowing it was instantiated--Spring's not involved in core Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependency injection problem
 
Similar Threads
Convert type fail in Spring 3.1 and Hibernate 4.1.0
Why is autowiring working in JUnit test but not in bean?
Spring Security 3: Salting password issue
Hard time getting Spring working with WS'.
spring - autowire="constructor"