org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'usersService' is defined
严重: Exception starting filter AuthFilter
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'usersService' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
at com.wanyu.frame.base.AuthFilter.init(AuthFilter.java:79)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4700)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5340)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
@Override public void init(FilterConfig filterConfig) throws ServletException { // 初始化方法 ServletContext sc = filterConfig.getServletContext(); XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc); if(cxt != null && cxt.getBean("usersService") != null && logManager == null) logManager = (LogManager) cxt.getBean("logManager"); }
项目使用 springframework ,发生这个错误是因为没有找到名称为'usersService' 的 bean;
1、名称写错了,你想获取的根本不是它;
2、名称写错了,你想获取它但不知道它的确切名称;
3、名称没有错,但是对应的 bean ,java 类上忘记了添加注解,如:@Component,@Service,@Controller,@Repository
嗯嗯,知道了错误的原因,解决方法其实已经很明确了。
我这里发现这个错误就是第一个原因,我想获取的根本不是它。
另外:
代码中的 cxt.getBean("usersService") != null 判断是没有意义的;
如果不存在会引发异常;
要么就加上 异常处理;
======================文档信息======================
版权声明:非商用自由转载-保持署名-注明出处
署名(BY) :testcs_dn(微wx笑)
文章出处:[无知人生,记录点滴](http://blog.csdn.NET/testcs_dn)
==============欢迎关注我的个人微信订阅号(微wx笑)==========