SSM框架:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type

isevenun 发布于 2016/10/19 07:37
阅读 5K+
收藏 0
今晚在整合ssm框架,出现了这个bug,折腾了一晚上,经常说没有找到bean..可是我在配置文件里都填写了扫描的包,应该是没问题的,可是我再也找不出问题,希望大家帮我找下原因,万分感谢。

只要我打开任一网页都会出现下面的错误,比如http://localhost:8080/BeikeBook/hello

http://localhost:8080/BeikeBook/showCity

错误提示:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.databasegroup.fortest.service.ICityService] found for dependency [com.databasegroup.fortest.service.ICityService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1403)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1056)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1452)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
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)

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cityController': Unsatisfied dependency expressed through field 'cityService': No qualifying bean of type [com.databasegroup.fortest.service.ICityService] found for dependency [com.databasegroup.fortest.service.ICityService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.databasegroup.fortest.service.ICityService] found for dependency [com.databasegroup.fortest.service.ICityService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
	org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
	org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
	org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
	org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
	org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
	org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
	javax.servlet.GenericServlet.init(GenericServlet.java:158)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
	org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
	org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1452)
	org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	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)
root cause




web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  version="3.0"  
  metadata-complete="true">
  
  <display-name>Archetype Created Web Application</display-name>
  
  <!-- 根应用上下文配置 -->
  <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
  <!-- 应用上下文路径  -->
  <servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<!-- multipart配置,上传文件的配置  -->
		<multipart-config>
			<location>/tmp/pic</location>
			<max-file-size>2097152</max-file-size>
			<max-request-size>4194304</max-request-size>
		</multipart-config>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app> 


servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="com.databasegroup.fortest.controller" />

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />
    
    <!-- Forwards requests to the "/" resource to the "welcome" view -->
    <!-- <mvc:view-controller path="/" view-name="index"/>    -->
	
	<!-- HandlerMapping -->  
    <bean  
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />  
  
    <!-- HandlerAdapter -->  
    <bean  
        class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />  
    
    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    

</beans> 

root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	<!-- 自动扫描 -->
	<context:component-scan base-package="com.databasegroup.fortest" />
	
	<mvc:annotation-driven />
	
	<!-- 引入配置文件 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="/WEB-INF/jdbc.properties" />
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="${initialSize}"></property>
		<!-- 连接池最大数量 -->
		<property name="maxActive" value="${maxActive}"></property>
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="${maxIdle}"></property>
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="${minIdle}"></property>
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="${maxWait}"></property>
	</bean>

	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描mapping.xml文件 classpath:com/databasegroup/fortest/mapping/ -->
		<property name="mapperLocations" value="/WEB-INF/*.xml"></property>
	</bean>

	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.databasegroup.fortest.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- <bean id="cityService" class="com.databasegroup.fortest.service" -->
	
</beans>  

ICityDao.java

package com.databasegroup.fortest.dao;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.databasegroup.fortest.pojo.City;

@Repository
public interface ICityDao {
	City getCity(City city);
	void addCity(City city);
} 

CityServiceImpl.java

package com.databasegroup.fortest.service;

import javax.annotation.Resource; 
import org.springframework.stereotype.Service;

import com.databasegroup.fortest.dao.ICityDao;
import com.databasegroup.fortest.pojo.City;
import com.databasegroup.fortest.service.ICityService;

@Service("cityService")
public class CityServiceImpl implements ICityService {
	
	@Resource
	private ICityDao cityDao;

	public City getCity(City city) {
		return cityDao.getCity(city);
	}

	public void addCity(City city) {
		cityDao.addCity(city);
	}

}
CityController.java
package com.databasegroup.fortest.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import static org.springframework.web.bind.annotation.RequestMethod.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;

import com.databasegroup.fortest.pojo.City;
import com.databasegroup.fortest.service.ICityService;

@Controller
@RequestMapping("/showCity")
public class CityController {
	
	@Autowired
    private ICityService cityService;
	
	@RequestMapping(method=GET)
	public String getCity(HttpServletRequest request, Model model) {
		City city = new City();
		String cityNum = request.getParameter("cityNum");
		city.setCityNum(cityNum);
		model.addAttribute("cityName", 
				cityService.getCity(city).getCityName());
		
		return "/fortest/showCity";
	}
}

ICityService.java


package com.databasegroup.fortest.service;

import org.springframework.stereotype.Component;

import com.databasegroup.fortest.pojo.City;

public interface ICityService {
	City getCity(City city);
	void addCity(City city);
}




加载中
0
拉娜亚
拉娜亚
教你个 办法 把所有的service impl都删除还有mybatis的配置文件也是 空跑 然后一个一个东西往里加 才可以更好的理解 
拉娜亚
拉娜亚
回复 @isevenun : 对于spring的配置多看看spring怎么加载bean的源代码 就知道这些错误的解决办法了
isevenun
isevenun
我也是慢慢添加上去,然后测试,不知道为什么就过了
0
oldDriverSS
oldDriverSS
你把扫描的包在精确以下看看 比如com.databasegroup.fortest.xxxImpl什么的
oldDriverSS
oldDriverSS
回复 @isevenun : 加上@service(value=‘’)试试 不过都这地步了,就不保证是那个问题了
isevenun
isevenun
请问接口ICityService也要加注解吗
isevenun
isevenun
您好,我把root-context,里面扫描的包精确到CityServiceImpl实现类的包了,但依然还是老问题。 <!-- 自动扫描 --> <context:component-scan base-package="com.databasegroup.fortest.service" />
0
szwx855
szwx855
 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>

    </context-param>


改成:

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/root-context.xml</param-value>
    </context-param>

isevenun
isevenun
您好,我试了,可是还是一样的问题。
0
求是科技
求是科技
https://my.oschina.net/u/2312022/blog/743014
0
青苗
青苗
折腾了一晚上   这么夸张!! 下载 开源的 SSM 参考下咯  点击 下载
0
温安适
温安适

试试:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/*.xml</param-value>
</context-param>



ZhangMinjer
ZhangMinjer
谢谢
返回顶部
顶部