aspose 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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Dependency Injection Problem." Watch "Dependency Injection Problem." New topic
Author

Dependency Injection Problem.

pankaj semwal
Ranch Hand

Joined: Oct 07, 2008
Posts: 300
Hi List[],

I am getting null pointer exception in the below line.

login=this.jdbcTemplate.queryForObject("SELECT * FROM USER_TB1 where uname=?",new Object[] {username},mapper);


MyDao is :



and bean.xml is



Pl help me where i am wrong.

Thanks
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

For starters your DAO class does not require this block of code



It just needs the JdbcTemplate which already knows the datasource as you took care of that while wiring up the JdbcTemplate in your bean definitions.

Clearly JdbcTemplate is null in your UserDAO class. This leads me to believe you are probably using the new operator to create this class somewhere. In this case it is not a Spring manged bean and your JdbcTemplate will not be wired in for you. Show us how UserDAO is used.


[How To Ask Questions][Read before you PM me]
pankaj semwal
Ranch Hand

Joined: Oct 07, 2008
Posts: 300
Yes Bill i am creating UserDAO using new operator in MyUserDetailService class.

After removing the new Operator from MyUserDetailService class i am getting null pointer exception due to i think there is injection problem in UserDao class.

MyUserDetailsService code:


and bean.xml look like



Now pl tell me why null pointer excpetion is coming in this line
Login dbUser = userDAO1.searchDatabase(username);


Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Probably for the same reason. If I were you rather than wiring every thing up manually like that I would make use of Springs component scanner.

To use this you would annotate your services with @Service and your DAO with @Repository you could remove your service and dao beans form your configuration and replace it with



Now you can adjust that package to whatever makes sense for you but it will scan for all the stereotype annotations @Service, @Repository, @Controller, @Component etc and automatically create beans for you.

Now when you need to use them you can just autowire them in



The problem you are running into is that for some reason your are newing up these classes, when you do that they are not spring managed. Where is the entry point to your application is this a Spring-mvc application?

pankaj semwal
Ranch Hand

Joined: Oct 07, 2008
Posts: 300

Yes i am using spring MVC and spring security.

My Login.jsp is like this:



Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Ok so presumably you have some Spring controllers annotated with @Controller. These will be picked up by the component scan I mentioned. When the user logs in you are in a Spring managed bean. Any Service you call from the controller should be autowired in as I mentioned earlier, and the service itself should be annotated with @Service. Do the same for your repositories. This should solve the problem . You should never need to use the new operator when dealing with Spring managed beans. This is the purpose of dependency injection, Spring has already created your object and will provide the classes with a reference to the necessary implementations.

pankaj semwal
Ranch Hand

Joined: Oct 07, 2008
Posts: 300
Ok..Thanks Bill.I will do what you told.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependency Injection Problem.
 
Similar Threads
DataSource null in DWR
Spring Struts implementation for Database conncetion problem in Login module
data source defined but not found..
Understand regarding REQUIRED and REQUIRES_NEW
JdbcTemplate and IllegalArgumentException