Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 不要在春季使用@autowire_Spring_Spring Mvc_Autowired_Spring Annotations - Fatal编程技术网

Spring 不要在春季使用@autowire

Spring 不要在春季使用@autowire,spring,spring-mvc,autowired,spring-annotations,Spring,Spring Mvc,Autowired,Spring Annotations,我想自动连接我的用户服务,但我得到一个错误。我有: web.xml 上下文配置位置 /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener 调度员 org.springframework.web.servlet.DispatcherServlet 2. 调度员 *.htm springSecurityFilterChain org.springframework.web.filte

我想自动连接我的用户服务,但我得到一个错误。我有:

web.xml

上下文配置位置 /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener 调度员 org.springframework.web.servlet.DispatcherServlet 2. 调度员 *.htm springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* 30 redirect.jsp dispatcher-servlet.xml

application-context.xml

com.andrylat.rgz.ocean.domains org.hibernate.dialen.oracle10galent 符合事实的 符合事实的 UserService.java

@ServiceuserService公共类UserService实现UserDetailsService{ @自动连线 私有UserDAO UserDAO; 公共UserDAO getUserDAO{ 返回userDAO; } 公共无效setUserDAOUserDAO userDAO userDAO{ this.userDAO=userDAO; } @TransactionalreadOnly=true @凌驾 public UserDetails loadUserByUserName最终字符串用户名 抛出UsernameNotFoundException{ com.andrylat.rgz.ocean.domains.User User=userDAO.findbyusernameuname; 名单管理机构 =新阵列列表; authorities.addnew SimpleGrantedAuthorityuser.getRole; 返回buildUserForAuthenticationuser、Authority; } 私人用户buildUserForAuthenticationcom.andrylat.rgz.ocean.domains.User用户, 名单管理机构{ 返回新的Useruser.getLogin、user.getPassword、, user.getEnabled,true,true,true,authorities; }} IndexController.java

@控制器公共类IndexController{ //@自动连线 //@限定符用户服务 私人用户服务; 公共用户服务getUserService{ 返回用户服务; } public void setUserServiceUserService用户服务{ this.userService=userService; } @自动连线 私人会话工厂会话工厂; @RequestMapping值=/index,方法=RequestMethod.GET 私有模型和视图getIndexPage{ 如果sessionFactory==null{ System.err.printlnsessionFactory为空!; }否则{ System.err.PrintlSessionFactory正常!; } 返回新的ModelAndView索引; } @RequestMapping值=/login,方法=RequestMethod.GET 私有模型和查看getLoginPage{ 返回新的ModelAndView登录; } } 问题:IndexController.java中的我的UserService没有自动连线!我加入了sessionFactory,它也不会自动连线。SessionFactory仅为null,当我将注释写入UserService时,我将从jsp获得下一个stacktrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.andrylat.rgz.ocean.services.UserService com.andrylat.rgz.ocean.controllers.IndexController.userService; nested exception is java.lang.IllegalArgumentException: **Can not set com.andrylat.rgz.ocean.services.UserService field com.andrylat.rgz.ocean.controllers.IndexController.userService to com.sun.proxy.$Proxy158**
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:602)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:521)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:462)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
我不明白为什么我无法获取此UserService,并且我的sessionFactory为空。

似乎是application-context.xml命名的问题

因此,请确保在WEB-INF文件夹中存在具有正确名称的applicationContext.xml文件,而不是application-context.xml,以便加载SessionFactorybean。否则,sessionFactory bean对象将为null。

中出现错误

private UserService userService;
UserService是类而不是接口。我创建接口userserviceiuserservice是实现UserServiceI并写入IndexController

@Autowire
private UserServiceI userService;

一切都好

当我尝试登录时,Spring安全性工作得很好,它有sessionFactory。当我想在我的控制器中使用autowire sessionFactory时,我会得到null。这是一大堆代码。我建议你阅读并编辑你的问题,以提高有人试图帮助你的可能性。很高兴知道。该错误清楚地解释了您自动连线的用户服务存在差异。很好,你找到了自我。