• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

org.springframework.beans.factory.BeanCreationException

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm trying to incorporate hibernate in my project but I'm having some difficulties. I'm getting the following error:

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.sys.power.dao.hibernate.UserDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.sys.power.dao.hibernate.UserDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


I'm not sure what the problem is as I'm implementing all the interfaces needed. I don't know where is the mapping that I need to make the autowired work. I don't want this post that long but here are my files.

root-context.xml


servlet-context.xml


hibernate.cfg.xml



I have my User.java


UserDAO.java


UserDAOImpl.java


UserService.java


UserServiceImpl.java


UserController.java



are my packages ok? What am I missing? any ideas? Thanks. Any help will be greatly appreciate it.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is more of a spring issue than an ORM issue and should probably be moved to the Spring forum.

You are probably having context scope issues. Some of the things are scoped to the dispatcher servlet and some to the root context . You did not post your web.xml so I am not sure what you have going. I suggest in this case searching the spring reference documentation for WebApplicationContext for further explanation.

In brief though you need to have <context:annotation-config/> defined in each context. This annotation is responsible for registering your support for general annotations such as @Required, @Autowired, @PostConstruct, and so on. You need to add this to your servlet-context.xml as well.

Bill


 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your DataSource, TransactionManager, and SessionFactory are in the wrong config file. They should be in the root-config.xml to be injected into your DAOs.

Basically Spring is creating two application contexts. one root the other for the servlet. The root is the parent application context to the servlet's application context which is the child. The child can see parent beans, but the parent cannot see the child beans. Since the SessionFactory is in the child, the parent beans like your DAOs cannot see the SessionFactory bean.

Mark
 
Gabriela Rios
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added <context:annotation-config/> but that didn't fix the problem.

This is my web.xml



 
Gabriela Rios
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark! I moved my DataSource, TransactionManager and SessionFactory to my root-context.xml. When I try to run it, I get:

Jun 11, 2012 9:43:07 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/MPower] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here] with root cause
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

Any ideas?
 
Gabriela Rios
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works now. I added this filter:



Thanks!
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gabriela Rios wrote:It works now. I added this filter:



Thanks!



OK, Glad that that works for you. But to me that is a red flag, or code smell that you have to use OpenSessionInView.

OpenSessionInView to me is an ANTI-PATTERN. It means that you aren't familiar with your use case and wrote code that did not get all the data that it needs and left things lazy loaded.

You should know exactly which data your jsp page will need and to make sure that your Service/DAO class returns all that data. Nothing more, nothing less. It just requires you to think up front about what you are actually doing.

By doing OpenInSessionInView you are actually slowing down your application because now Hibernate has to go to the database more times causing more network traffic.

Queries and the data you fetch are always use case specific.

Hope that helps.

Mark
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gabriela Rios wrote:I added <context:annotation-config/> but that didn't fix the problem.



You would have also had to remove the component scans from the root context that were scanning packages that had session factory dependencies, then it would have worked. It is important to understand what Spring is doing for you here. I gave you something to search in the reference documentation but Mark also explained it above. The component scan will create spring managed beans from any class with one of the stereo type annotations component, service, repository etc. It will only create one of these regardless of how many times you scan the same package. Because your servlet configuration is not visible to the root (parent) those component scans you left in your root were creating beans without having the dependencies defined.

So you had one of 2 options

1. define everything in the root like you did (this probably makes more sense unless there is a specific reason you wanted it scoped to the servlet)
2. leave it as you had it add the context-annotation-config to the servlet.xml and remove the component scans from the root.

Just note that context-annotation-config is not inherited from the parent root if you need it, it must be defined in each config which is what I was trying bring your attention to.

Glad you got it resolved though

Thanks,
Bill
 
Gabriela Rios
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for letting me know all of that. I'm in the process of learning spring 3 and hibernate but at the same time I have to make some progress in my project so I'm using things that I don't fully understand.

I have removed the filter as I really didn't need it. Initially, when I added it, it let me see other errors that I had with my mappings. I had errors with my OneToMany and ManyToMany annotations which essentially was that I didn't have the table in my hibernate.cfg.xml.



The first example that I saw didn't really use that file. It was something like this.



Is that file really necessary? I get a warning because the DTD is deprecated but I changed it to the new one supposedly and it didn't work for me. Any thoughts on this? Thanks for your help on this. I'm learning a lot with you guys.
 
Gabriela Rios
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Turns out I still had the error so I removed <context:annotation-config /> and it works now. Thanks


web.xml




root-context.xml


servlet-context.xml

 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Tiles is a Web Layer thing and should be defined in the servlet's Spring config file.

Remeber there are two ApplicationContexts,

1) the one for the middle tier beans. Those are services, repositories/dao, transactionManager, datasource. This is from your root-context.xml config
2) The one for the dispatcher servlet. Those are your web layer components, Tiles, Controllers, ViewResolvers, etc. Anything about web. This one can see all the beans in the root application context. so I can inject a Service into a Controller. But not the other way around, meaning you cannot inject a Controller into a Service, which you don't ever want to do anyway. You call down the stack architecture, never up.

Mark
 
Don't MAKE me come back there with this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic