最近想架构一个SSM的项目,注入的Service接口 项目启动却告知有两个相同的service
配置文件:
包结构如下:
异常
D:\soft\tomcat7\bin\catalina.bat run [2017-01-04 03:04:44,144] Artifact farmer:war exploded: Server is not connected. Deploy is not available. Using CATALINA_BASE: "C:\Users\Administrator\.IntelliJIdea15\system\tomcat\Unnamed_farmer" Using CATALINA_HOME: "D:\soft\tomcat7" Using CATALINA_TMPDIR: "D:\soft\tomcat7\temp" Using JRE_HOME: "D:\soft\jdk7" Using CLASSPATH: "D:\soft\tomcat7\bin\bootstrap.jar;D:\soft\tomcat7\bin\tomcat-juli.jar" Connected to the target VM, address: '127.0.0.1:61086', transport: 'socket' 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Server version: Apache Tomcat/7.0.57 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Server built: Nov 3 2014 08:39:16 UTC 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Server number: 7.0.57.0 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: OS Name: Windows 7 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: OS Version: 6.1 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Architecture: x86 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: JAVA_HOME: D:\soft\jdk7\jre 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Version: 1.7.0_15-b03 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Vendor: Oracle Corporation 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_BASE: C:\Users\Administrator\.IntelliJIdea15\system\tomcat\Unnamed_farmer 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_HOME: D:\soft\tomcat7 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:61086,suspend=y,server=n 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcom.sun.management.jmxremote= 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcom.sun.management.jmxremote.port=1091 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcom.sun.management.jmxremote.ssl=false 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcom.sun.management.jmxremote.authenticate=false 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.rmi.server.hostname=127.0.0.1 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.util.logging.config.file=C:\Users\Administrator\.IntelliJIdea15\system\tomcat\Unnamed_farmer\conf\logging.properties 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.endorsed.dirs=D:\soft\tomcat7\endorsed 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.base=C:\Users\Administrator\.IntelliJIdea15\system\tomcat\Unnamed_farmer 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.home=D:\soft\tomcat7 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.io.tmpdir=D:\soft\tomcat7\temp 一月 04, 2017 3:04:44 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\soft\jdk7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\soft\maven\bin;D:\soft\jdk7\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;. 一月 04, 2017 3:04:44 下午 org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8080"] 一月 04, 2017 3:04:44 下午 org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["ajp-bio-8009"] 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.Catalina load INFO: Initialization processed in 294 ms 一月 04, 2017 3:04:44 下午 org.apache.catalina.core.StandardService startInternal INFO: Starting service Catalina 一月 04, 2017 3:04:44 下午 org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.57 一月 04, 2017 3:04:44 下午 org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-bio-8080"] 一月 04, 2017 3:04:44 下午 org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["ajp-bio-8009"] 一月 04, 2017 3:04:44 下午 org.apache.catalina.startup.Catalina start INFO: Server startup in 27 ms Connected to server [2017-01-04 03:04:45,197] Artifact farmer:war exploded: Artifact is being deployed, please wait... 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/core is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined 一月 04, 2017 3:04:48 下午 org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined ----系统初始化开始----- ----系统初始化结束----- [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization started [org.springframework.web.context.support.XmlWebApplicationContext] - Refreshing Root WebApplicationContext: startup date [Wed Jan 04 15:04:49 CST 2017]; root of context hierarchy [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [F:\SVN\demo_shiro_1\target\farmer\WEB-INF\classes\spring\spring-content.xml] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [F:\SVN\demo_shiro_1\target\farmer\WEB-INF\classes\spring\spring-mvc.xml] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [F:\SVN\demo_shiro_1\target\farmer\WEB-INF\classes\spring\spring-mybatis.xml] [org.springframework.context.support.PropertySourcesPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties] [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties] [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/test]}" onto public java.lang.String com.myjava.demo.web.test.controller.TestController.test(org.springframework.ui.Model,javax.servlet.http.HttpServletRequest) [org.hibernate.validator.internal.util.Version] - HV000001: Hibernate Validator 5.2.4.Final [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] - Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Wed Jan 04 15:04:49 CST 2017]; root of context hierarchy [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] - Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Wed Jan 04 15:04:49 CST 2017]; root of context hierarchy [org.springframework.web.context.support.XmlWebApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.myjava.demo.web.test.service.IService com.myjava.demo.web.test.controller.TestController.iService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.myjava.demo.web.test.service.IService] is defined: expected single matching bean but found 2: serviceImpl,IService [org.springframework.web.context.ContextLoader] - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.myjava.demo.web.test.service.IService com.myjava.demo.web.test.controller.TestController.iService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.myjava.demo.web.test.service.IService] is defined: expected single matching bean but found 2: serviceImpl,IService at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:434) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649) at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1760) 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:601) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:792) at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618) at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565) 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:601) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:792) at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1486) at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:96) at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1327) at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1419) at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:847) 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:601) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322) at sun.rmi.transport.Transport$1.run(Transport.java:177) at sun.rmi.transport.Transport$1.run(Transport.java:174) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:173) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:722) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.myjava.demo.web.test.service.IService com.myjava.demo.web.test.controller.TestController.iService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.myjava.demo.web.test.service.IService] is defined: expected single matching bean but found 2: serviceImpl,IService at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ... 56 more Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.myjava.demo.web.test.service.IService] is defined: expected single matching bean but found 2: serviceImpl,IService at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ... 58 more 一月 04, 2017 3:04:50 下午 org.apache.catalina.core.StandardContext startInternal SEVERE: Error listenerStart 一月 04, 2017 3:04:50 下午 org.apache.catalina.core.StandardContext startInternal SEVERE: Context [] startup failed due to previous errors [2017-01-04 03:04:50,790] Artifact farmer:war exploded: Error during artifact deployment. See server log for details.
在service注解上加
@Service("test")
在@Autowired下加
@Qualifier("test")
可以实现按名字注入
单不是我想要的效果,我只想写个
@Autowired 实现接口的注入
求教!!!!
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>demo-shiro</display-name> <!-- Spring和mybatis的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/spring-*.xml</param-value> </context-param> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--项目初始化listener--> <listener> <listener-class>com.myjava.demo.listener.systemInitListener.SystemInitListener</listener-class> </listener> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!--该监听器监听HTTP请求事件,web服务器 接收的每一次请求都会通知该监听器。--> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- Spring MVC servlet --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--欢迎页面--> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!--错误映射页面--> <error-page> <error-code>500</error-code> <location>/WEB-INF/jsp/error/500.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF/jsp/error/404.jsp</location> </error-page> </web-app>
你那两个配置文件是不是扫描了两次啊?
扫描一个和扫两次区别不大 第一次扫描 我是准备只扫描controller的 已经修改了
@LewsKay: ? 第一张图片里的扫描就够了,把画红线的干掉,试试
@让我发会呆: 还是不行
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.myjava.demo.web.test.service.IService] is defined: expected single matching bean but found 2: serviceImpl,IService
想让它自动装配进去的, 之前用的项目跟我自己配的也没差啊 怎么就不行呢 @Autowired 注入接口就失败 ,注入实现类就成功 或者在@Service注解上加上唯一name 在controller 的在@Autowired下加@Qualifier("name") 也能成功,就直接用接口不成功
@LewsKay: @Resource试试吧
@让我发会呆: 不试,我只是想知道为什么只用@Autowired 注入不了
@LewsKay: 说实话,注入两次这种问题,从来没遇到过,要不你把扫描路径直接写到service层,再试试会不会出现这个问题。关于这两种注入的区别,看这个吧:https://www.zhihu.com/question/39356740