gpt4 book ai didi

java - Spring 不允许我 Autowiring 我的类(class)

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:51 25 4
gpt4 key购买 nike

我搜索过类似的问题,但找不到与此问题相关的任何信息。

这是我的 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)

我不明白为什么我的应用程序没有 Autowiring 。

最佳答案

改变

 @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;
}

这应该有效。

关于java - Spring 不允许我 Autowiring 我的类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25665420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com