春天不允许我自动上课

时间:2014-09-04 12:22:56

标签: java spring hibernate spring-mvc

我已经搜索了类似的问题,但无法找到有关此问题的任何内容。

这是我的Controller类

 @Controller
 @RequestMapping(value = "/admin")
 public class AdminController {


 @Autowired
 private AuthenticationServicesImpls check;

 public AuthenticationServicesImpls getCheck() {
    return check;
 }

public void setCheck(AuthenticationServicesImpls check) {
    this.check = check;
}

@RequestMapping(value = "/login")
public ModelAndView adminLogin(HttpServletResponse response)
        throws IOException {
    return new ModelAndView("adminlogin");

}

@RequestMapping(value = "/authentication")
public ModelAndView validateAdmin(HttpServletResponse response,
        @RequestParam(value = "userid", required = false) String username,
        @RequestParam(value = "password", required = false) String password){
    System.out.print(username); 
    System.out.print(password); 



      if(check.validateAdminUser(username, password)){
            return new ModelAndView("adminlogin");
      } else
      {
            return new ModelAndView("error/errorlogin");
      }



     }

   }

这是我的服务界面

package online.services;
 import org.springframework.stereotype.Repository;



@Repository
   public interface AuthenticationServices {


public boolean isLoggedIn();
public int login();
public int logout();
public boolean validateUser();

}

这是我的DOE类实现我刚刚在方法上用于测试

 package online.deo;

   import java.util.List;

   import org.hibernate.Query;
   import org.hibernate.Session;
   import org.hibernate.SessionFactory;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.stereotype.Component;
   import org.springframework.stereotype.Repository;
   import org.springframework.transaction.annotation.Transactional;

    import online.model.UserDetail;
    import online.services.AuthenticationServices;

    @Transactional
    @Repository
    @Component
    public class AuthenticationServicesImpls implements AuthenticationServices {

       AuthenticationServicesImpls(){}
      @Autowired
      private SessionFactory sessionFactory;




     public boolean validateUser() {

          return false;
     }
     @SuppressWarnings("unchecked")
     @Transactional(readOnly = true)
    public boolean validateAdminUser(String userID, String password) {
        System.out.print("this is session facoty class"+ sessionFactory.toString());
        System.out.print(userID);   
        System.out.print(password);

        Session session = sessionFactory.openSession();
        System.out.println(session);
        Query query = session.createQuery("from user_detail");

        System.out.println(query.list());
        List<UserDetail> user = query.list();

        if (user != null) {
            for (UserDetail us : user) {

                System.out.println(us.getUser_id());

                 if (us.getUser_username().equals(userID)) {
                    if (us.getUser_password().equals(password)) {
                        return true;
                    }
                     return false;
                }
            }

        }
           return false;
         }

         }

这是我的POJO班级

package online.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user_detail")
public class UserDetail {
@Id
private int user_id;

@Column(name = "user_first_name")
private String user_first_name;

@Column(name = "user_last_name")
private String user_last_name;

@Column(name = "user_emailid")
private String user_emailid;

@Column(name = "user_mobileno")
private long user_mobileno;

@Column(name = "user_username")
private String user_username;

 @Column(name = "user_password")
private String user_password;

@Enumerated(EnumType.STRING)
private Role user_role;

 public int getUser_id() {
     return user_id;
}

public void setUser_id(int user_id) {
    this.user_id = user_id;
}

public String getUser_first_name() {
    return user_first_name;
}

public void setUser_first_name(String user_first_name) {
    this.user_first_name = user_first_name;
}

public String getUser_last_name() {
    return user_last_name;
}

public void setUser_last_name(String user_last_name) {
    this.user_last_name = user_last_name;
}

public String getUser_emailid() {
    return user_emailid;
}

public void setUser_emailid(String user_emailid) {
    this.user_emailid = user_emailid;
}

public long getUser_mobileno() {
    return user_mobileno;
}

public void setUser_mobileno(long user_mobileno) {
    this.user_mobileno = user_mobileno;
}

public String getUser_username() {
    return user_username;
}

public void setUser_username(String user_username) {
    this.user_username = user_username;
}

public String getUser_password() {
    return user_password;
}

public void setUser_password(String user_password) {
    this.user_password = user_password;
}

public Role getUser_role() {
    return user_role;
}

public void setUser_role(Role user_role) {
    this.user_role = user_role;
}

}

这是我的servlet-servlet.xml文件

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans     

              enter code here`http://www.springframework.org/schema/beans/spring-beans- 3.1.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema  /context/spring-context-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/sche/mvc/spring-mvc-3.1.xsd"> 

   <context:component-scan base-package="online" />
   <mvc:resources mapping="/resources/**" location="/resources/" />

   <context:annotation-config />
   <mvc:annotation-driven/> 
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
     <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

        <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/Shopping" p:username="root"
        p:password="123456" />

     <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

   <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="check" class="online.deo.AuthenticationServicesImpls" />
    </beans>

这是我的错误堆栈

 by: org.springframework.beans.factory.BeanCreationException: Could not autowire field:    private online.deo.AuthenticationServicesImpls online.controller.AdminController.check; nested exception is java.lang.IllegalArgumentException: Can not set online.deo.AuthenticationServicesImpls field online.controller.AdminController.check to com.sun.proxy.$Proxy17
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 35 more
Caused by: java.lang.IllegalArgumentException: Can not set online.deo.AuthenticationServicesImpls field online.controller.AdminController.check to com.sun.proxy.$Proxy17
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:741)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    ... 37 more

Sep 04, 2014 5:43:05 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet SpringDispatcher
java.lang.IllegalArgumentException: Can not set online.deo.AuthenticationServicesImpls field online.controller.AdminController.check to com.sun.proxy.$Proxy17
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:741)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

我无法理解为什么我的应用程序没有自动装配。

3 个答案:

答案 0 :(得分:5)

更改

 @Autowired
 private AuthenticationServicesImpls check;

 public AuthenticationServicesImpls getCheck() {
    return check;
 }

public void setCheck(AuthenticationServicesImpls check) {
    this.check = check;
}

要:

@Autowired
 private AuthenticationServices check;

 public AuthenticationServices getCheck() {
    return check;
 }

public void setCheck(AuthenticationServices check) {
    this.check = check;
}

这应该有用。

答案 1 :(得分:3)

默认情况下,Spring使用标准的基于java接口的代理。这意味着您的Spring托管bean必须实现Spring正在管理的接口。

如果要基于类进行代理,可以在Application Config中更新以下注释(我相信这也需要CGLIB jar):

请注意'proxyTargetClass = true':

@EnableAspectJAutoProxy(proxyTargetClass = true)

答案 2 :(得分:0)

在xml文件中添加此行并尝试再次运行。

    <jpa:repositories base-package="your.repository.base.package.path"/>

我认为这应该有用。