2017-04-17 128 views
0

我使用Spring版本3和休眠4版本的JDK 1.7。 我无法解决无法自动装配的字段的错误。无法自动装配领域@Autowired注解

Login.java

package com.tanmay.model; 

import javax.persistence.Basic; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.NamedQueries; 
import javax.persistence.NamedQuery; 
import javax.persistence.Table; 
@NamedQueries( 
     { 
      @NamedQuery( 
      name = "validateUserQuery", 
      query = "from Login l where l.userName =:userName AND l.userPassword =:userPassword" 
      ) 
     } 
    ) 

@Entity 
@Table(name="login") 
public class Login { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @Basic 
    private String userName; 

    @Basic 
    private String password; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getUserName() { 
     return userName; 
    } 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 
} 

LoginAPIController.java

package com.tanmay.controller; 

import javax.servlet.http.HttpServletRequest; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 

import com.tanmay.dto.LoginDTO; 
import com.tanmay.dto.ResponseDTO; 
import com.tanmay.model.Login; 
import com.tanmay.service.LoginService; 
/** 
* 
* @author TANMAY 
* 
*/ 
@Controller 
@RequestMapping(value="/user") 
public class LoginAPIController extends BaseController{ 

    @Autowired 
    private LoginService loginService; 

    /** 
    * 
    * @param loginDTO 
    * @param request 
    * @return 
    * @throws Exception 
    */ 
    @RequestMapping(value="/login", method = RequestMethod.POST) 
    @ResponseBody 
    public ResponseDTO login(@RequestBody LoginDTO loginDTO , HttpServletRequest request)throws Exception{ 

     Login login = new Login(); 

     System.out.println(">>>>>>>>>>> userName "+loginDTO.getUsername()); 
     System.out.println(">>>>>>>>>>> userPassword "+loginDTO.getUserpassword()); 

     if(loginDTO.getUsername()!=null && loginDTO.getUserpassword()!=null){ 

      login = loginService.validateUser(loginDTO.getUsername(), loginDTO.getUserpassword()); 

      if(login!=null){ 
       return ResponseDTO.ok(login); 
      } 
      else{ 
       return ResponseDTO.not_found(-1, "UserName/Password invalid"); 
      } 
     } 
     else{ 
      return ResponseDTO.bad_request("Please Enter UserName or Password"); 
     } 
    } 
} 

LoginDaoImpl.java

package com.tanmay.daoimpl; 

import org.springframework.stereotype.Repository; 

import com.tanmay.dao.BaseDao; 
import com.tanmay.dao.LoginDao; 
import com.tanmay.misc.DESEncryption; 
import com.tanmay.model.Login; 

@Repository("loginDao") 
public class LoginDaoImpl extends BaseDao implements LoginDao { 

    @Override 
    public Login validateUser(String userName, String userPassword) throws Exception { 
     DESEncryption desEncryption = new DESEncryption(); 
     return (Login) getSession().getNamedQuery("validateUserQuery") 
       .setString("userName", userName) 
       .setString("userPassword", desEncryption.encrypt(userPassword)).uniqueResult(); 
    } 
} 

个LoginServiceImpl

package com.tanmay.serviceimpl; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 
import com.tanmay.dao.LoginDao; 
import com.tanmay.model.Login; 
import com.tanmay.service.LoginService; 
@Service("loginService") 
@Transactional(readOnly = true) 
public class LoginServiceImpl implements LoginService { 

    @Autowired 
    private LoginDao loginDao; 

    @Override 
    public Login validateUser(String userName, String userPassword) throws Exception { 
     return loginDao.validateUser(userName, userPassword); 
    } 

} 

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="com.tanmay" /> 

    <bean id="apiPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath:db-config.properties</value> 
<!--     <value>classpath:email-config.properties</value> --> 
        <value>classpath:messages_en.properties</value> 
      </list> 
     </property> 
    </bean> 
<!-- <import resource="classpath:email-config.xml"/> --> 
    <import resource="classpath:db-config.xml"/> 
    <bean id="configurationLoader" 
     class="org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader"> 
    </bean>  
    <bean id="validator" class="org.springmodules.validation.bean.BeanValidator" > 
     <property name="configurationLoader" ref="configurationLoader"></property> 
    </bean> 

<!--  Message resource declaration --> 
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="classpath:/messages" /> 
     <property name="defaultEncoding" value="UTF-8"/> 
    </bean> 
</beans> 

错误/控制台登录

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginAPIController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tanmay.service.LoginService com.tanmay.controller.LoginAPIController.loginService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tanmay.dao.LoginDao com.tanmay.serviceimpl.LoginServiceImpl.loginDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected org.hibernate.SessionFactory com.tanmay.dao.BaseDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [db-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Errors in named queries: validateUserQuery 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:652) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:600) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:666) 
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:519) 
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:460) 
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) 
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144) 
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031) 
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4914) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5201) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3746) 
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292) 
    at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5528) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1378) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1382) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1382) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1350) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tanmay.service.LoginService com.tanmay.controller.LoginAPIController.loginService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tanmay.dao.LoginDao com.tanmay.serviceimpl.LoginServiceImpl.loginDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected org.hibernate.SessionFactory com.tanmay.dao.BaseDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [db-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Errors in named queries: validateUserQuery 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) 
    ... 31 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tanmay.dao.LoginDao com.tanmay.serviceimpl.LoginServiceImpl.loginDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected org.hibernate.SessionFactory com.tanmay.dao.BaseDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [db-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Errors in named queries: validateUserQuery 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:891) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:834) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) 
    ... 33 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tanmay.dao.LoginDao com.tanmay.serviceimpl.LoginServiceImpl.loginDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected org.hibernate.SessionFactory com.tanmay.dao.BaseDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [db-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Errors in named queries: validateUserQuery 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) 
    ... 44 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected org.hibernate.SessionFactory com.tanmay.dao.BaseDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [db-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Errors in named queries: validateUserQuery 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:891) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:834) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) 
    ... 46 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected org.hibernate.SessionFactory com.tanmay.dao.BaseDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [db-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Errors in named queries: validateUserQuery 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) 
    ... 57 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [db-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Errors in named queries: validateUserQuery 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:891) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:834) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) 
    ... 59 more 
+0

可以commentout @Service( “login服务”)? –

+0

还显示您的login服务接口 –

+0

@georgesvan我注释掉它,仍然出现问题。 – Tanmay

回答

0

有一个错误在你的命名查询

@NamedQuery( 
     name = "validateUserQuery", 
     query = "from Login l where l.userName =:userName AND l.userPassword =:userPassword" 
     ) 

你是指一个l.userPassword,但该领域被称为password。将l.userPassword更改为password

+0

是啊..谢谢你解决我的问题 – Tanmay

相关问题