2017-05-31 68 views
0

我是spring mvc的新手,我有一个问题,那就是如何知道标签是否正在扫描正确的包? 例如: 我已定义的命名找不到xml中定义的bean

<context:component-scan base-package="spring.mvc"/>

一个标签,而该包的位置如下:

,当我定义用户beans.xml中豆,如

<bean id="userService" class="spring.mvc.service.UserServiceImpl" />

用户的beans.xml:

<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans" 
 
\t xmlns:context="http://www.springframework.org/schema/context" 
 
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t xsi:schemaLocation=" http://www.springframework.org/schema/beans 
 
\t  \t \t \t \t \t \t http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 
\t  \t \t \t \t \t \t http://www.springframework.org/schema/context 
 
\t  \t \t \t \t \t \t http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
 
    
 
    <context:component-scan base-package="spring.mvc"/> 
 
    <context:annotation-config /> 
 
    
 
    <bean id="userService" class="spring.mvc.service.UserServiceImpl" /> 
 
    <bean id="userDao" class="spring.mvc.dao.UserDaoImpl" /> 
 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
 
     <property name="dataSource" ref="datasource" /> 
 
    </bean> 
 
    
 
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
 
     <property name="url" value="jdbc:mysql://localhost:3306/myusers" /> 
 
     <property name="username" value="root" /> 
 
     <property name="password" value="root" /> 
 
    </bean> 
 
</beans>

好像豆不能在我把在类路径中找到= “”

错误如下所示:

19:44:19.399 [localhost-startStop-1] WARN o.s.w.c.s.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [spring.mvc.service.UserServiceImpl] for bean with name 'userService' defined in class path resource [config/user-beans.xml]; nested exception is java.lang.ClassNotFoundException: spring.mvc.service.UserServiceImpl 
 
19:44:19.399 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]65f7a781: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,userService,userDao,jdbcTemplate,datasource,org.springframework.web.servlet.view.InternalResourceViewResolver#0]; root of factory hierarchy 
 
19:44:19.433 [localhost-startStop-1] ERROR o.s.web.servlet.DispatcherServlet - Context initialization failed 
 
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [spring.mvc.service.UserServiceImpl] for bean with name 'userService' defined in class path resource [config/user-beans.xml]; nested exception is java.lang.ClassNotFoundException: spring.mvc.service.UserServiceImpl 
 
\t at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1385) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:641) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:609) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1484) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1007) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:741) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at javax.servlet.GenericServlet.init(GenericServlet.java:158) [servlet-api.jar:3.1.FR] 
 
\t at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183) [catalina.jar:8.5.15] 
 
\t at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099) [catalina.jar:8.5.15] 
 
\t at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989) [catalina.jar:8.5.15] 
 
\t at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4931) [catalina.jar:8.5.15] 
 
\t at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5241) [catalina.jar:8.5.15] 
 
\t at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.5.15] 
 
\t at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) [catalina.jar:8.5.15] 
 
\t at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) [catalina.jar:8.5.15] 
 
\t at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_131] 
 
\t at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_131] 
 
\t at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_131] 
 
\t at java.lang.Thread.run(Unknown Source) [na:1.8.0_131] 
 
Caused by: java.lang.ClassNotFoundException: spring.mvc.service.UserServiceImpl 
 
\t at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285) ~[catalina.jar:8.5.15] 
 
\t at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119) ~[catalina.jar:8.5.15] 
 
\t at org.springframework.util.ClassUtils.forName(ClassUtils.java:250) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:401) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1432) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1377) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
 
\t ... 26 common frames omitted

有人可以解释组件扫描是如何工作的吗?

在此先感谢

+0

看到https://stackoverflow.com/questions/6807230/what-syntax-for-including-sub-packages-in-contextcomponent-scan –

+0

对不起,我还没有得到它 –

+0

如何发布你有错误。它似乎与* context:component-scan * –

回答

0

确认以下两点UserServiceImpl类:
1.检查在你的类包声明。它应该与spring.mvc.service相同。
2.检查类级别注释是否添加到您的班级(@Component/@Service)。