spring+mvc注解问题。启动报错,求教!!!!!!

程序员Joe 发布于 2014/02/11 15:04
阅读 114K+
收藏 0

spring mvc框架

service层调用DAO层时,代码如下: 

@Component
public class LoginService {
 @Autowired
 private LoginDao loginDao;
 
 public String login(User user){
  return  loginDao.login(user);
 }
}

问题:这个 “@Autowired”会引起启动报错,不加这个注解启动正常,但是调用不到loginDao.login方法

死活查不出原因啊

 

奇怪的是,我在controller层

@Autowired
 private LoginService loginService;

加注解引入service

调用service中的方法完全正常

为什么同样的方式在service中调用dao的就不行了?!?!

跪求高手!!!

 

附上启动报错的代码:

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' defined in URL [file:/D:/Program%20Files/tomcatServers/apache-tomcat-6.0.35/webapps/SpringJoe/WEB-INF/classes/shiro/spring-shiro.xml]: Cannot resolve reference to bean 'securityManager' while setting bean property 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in URL [file:/D:/Program%20Files/tomcatServers/apache-tomcat-6.0.35/webapps/SpringJoe/WEB-INF/classes/shiro/spring-shiro.xml]: Cannot resolve reference to bean 'monitorRealm' while setting bean property 'realm'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'monitorRealm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.simple.mvc.service.LoginService com.simple.mvc.service.MonitorRealm.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.simple.dao.LoginDao com.simple.mvc.service.LoginService.loginDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.simple.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
 at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:232)
 at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:618)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
 at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
 at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
 at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
 at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
 at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
 at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
 at org.apache.catalina.core.StandardService.start(StandardService.java:525)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:606)
 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in URL [file:/D:/Program%20Files/tomcatServers/apache-tomcat-6.0.35/webapps/SpringJoe/WEB-INF/classes/shiro/spring-shiro.xml]: Cannot resolve reference to bean 'monitorRealm' while setting bean property 'realm'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'monitorRealm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.simple.mvc.service.LoginService com.simple.mvc.service.MonitorRealm.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.simple.dao.LoginDao com.simple.mvc.service.LoginService.loginDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.simple.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRe
gistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:320)
 ... 39 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'monitorRealm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.simple.mvc.service.LoginService com.simple.mvc.service.MonitorRealm.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.simple.dao.LoginDao com.simple.mvc.service.LoginService.loginDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.simple.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:320)
 ... 49 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.simple.mvc.service.LoginService com.simple.mvc.service.MonitorRealm.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.simple.dao.LoginDao com.simple.mvc.service.LoginService.loginDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.simple.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
 ... 57 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.simple.dao.LoginDao com.simple.mvc.service.LoginService.loginDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.simple.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
 ... 59 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.simple.dao.LoginDao com.simple.mvc.service.LoginService.loginDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.simple.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
 ... 70 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.simple.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
 ... 72 more

 

以下是问题补充:

@程序员Joe:applicationContext.xml中: 就配了一个 <context:component-scan base-package="com.simple.dao,com.simple.mvc.service" /> spring-mvc.xml中 <context:component-scan base-package="com.simple.mvc.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" /> (2014/02/11 15:19)
加载中
1
jackson_open
jackson_open
1、Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at

2、org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) at ,

根据上面的异常信息提示:1、Autowired默认是true,对于不存在的bean,你去引用会报错;2、BeanDefinitionValueResolver异常说明你这个dao的定义有问题,要么没配置,要么引用错误。

我的想法是:1、你这个loginDao如果没配置的话,需要进行配置,id不要重复了。2:、loginDao如果已经配置,建议使用@Qualifier("loginDao")进行标注说明你到底要使用哪个dao。3、建议对dao层进行自动注入配置: <loxia:dao-config base-package="com.manage.dao" /> ,希望对你有所帮助,不对的地方请见谅


程序员Joe
程序员Joe
我解决了。。。但是具体原因我自己也不是很清楚...不知道怎么的就好了。。。差了很久也无果。。。不管了,现在spring+mvc正常了,我加了jpa又有问题了。求教啊大婶 http://www.oschina.net/question/1435284_143221
0
爬树猪
爬树猪
配置文件呢? 
程序员Joe
程序员Joe
回复 @无尽的编码 : 无效...这个在配置文件里面加bean,和在loginDao中加@Repository应该是一个效果吧,还是一样报错
爬树猪
爬树猪
回复 @M_and_7 : <bean id="userDao" class="..."/> <bean id="userService" class="..."> <property name="userDao"> <ref bean="userDao"/> </property> </bean>
程序员Joe
程序员Joe
我附上去
程序员Joe
程序员Joe
配置文件没有什么特别的,
0
double9
double9
LoginDao 需要加@Responsitory注解
石羊场的小混混
石羊场的小混混
回复 @程序员Joe : 1111
程序员Joe
程序员Joe
@Repository 已经加了
0
常计南
常计南
http://java-true.iteye.com/blog/796357到这里看看,你试一下@required设为false。我公司不能上网,那pad没法帮你测试
程序员Joe
程序员Joe
设为false以后。。启动倒是不报错了, 问题是程序走到loginDao.login(user)时调用不到loginDao里面的方法。
0
52iSilence7
52iSilence7
异常中有这个描述NoSuchBeanDefinitionException: No qualifying bean of type [com.simple.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.你加@Qualifier试试行不行
0
0day
0day

<context:component-scan base-package="com.simple.dao,com.simple.mvc.service" />

LoginDao 不在这个包里吧,所以找不到。

0
一身正气
一身正气

Cannot resolve reference to bean 'securityManager' while setting bean property 'securityManager';

其他地方没有问题,是这里出现问题了,你的shiro配置问题。

0
一身正气
一身正气
上面给出的方案都是脚痛医脚,头痛医头。
0
一身正气
一身正气
shiro才是你的病痛根源。
0
Jack001
Jack001
在Dao上加注解 @Repository
返回顶部
顶部