spring mvc 注解成功,DAO失败 No qualifying bean of type

J2EE 码拜 9年前 (2015-04-03) 7440次浏览 0个评论
严重: Servlet /test threw load() exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type

service注解成功,但是DAO 就是失败,下面这是代码Controller

@Controller
public class WeatherAction {
    @Autowired
    private WeatherService weatherService;
    @RequestMapping("/find")
    public @ResponseBody String find(String test){
        WeatherDO weatherDO = weatherService.findWeather(test);
        return weatherDO.getCityname();
    }
}

service

public interface WeatherService {
     public WeatherDO findWeather(String test);
}

service实现

@Service
public class WeatherServiceImpl implements WeatherService {
    @Autowired
    private WeatherDao weatherDao;
    public WeatherDO findWeather(String test) {
        if (Utils.isHowManyNumbers(6, test)) {
            return weatherDao.findByPostcode(test);
        }
        if (Utils.isHowManyNumbers(9, test)) {
            return weatherDao.findByCitycode(test);
        }
        return weatherDao.findByCityname(test);
    }
}

DAO

public interface WeatherDao {
    public WeatherDO findByCityname(String cityname);
    public WeatherDO findByCitycode(String citycode);
    public WeatherDO findByPostcode(String postcode);
   }

接下来就是配置文件web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name></display-name>
	<!-- 中文编码 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<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-class>
    		org.springframework.web.context.ContextLoaderListener
    	</listener-class>
    </listener>
    <context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>classpath*:spring-*.xml</param-value>  
    </context-param>
    
	<servlet>
		<servlet-name>spring-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/spring-dispatcher.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-dispatcher</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
	<!--页面路径 -->
	<filter>
		<filter-name>path-filter</filter-name>
		<filter-class>com.lljqiu.test.PathFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>path-filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

spring-datasource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
		>
    <context:component-scan base-package="com.lljqiu" />
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations"  value="classpath:config/datasource.properties" />
	</bean>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">   
    	<property name="driverClassName"><value>${database.driver}</value></property>   
  		<property name="url"><value>${database.url}</value></property>   
    	<property name="username"><value>${database.username}</value></property>  
        <property name="password"><value>${database.password}</value></property>  
        <property name="maxActive"><value>${database.maxActive}</value></property>  
        <property name="initialSize"><value>${database.initialSize}</value></property>  
        <property name="maxWait"><value>${database.maxWait}</value></property>  
        <property name="maxIdle"><value>${database.maxIdle}</value></property>  
        <property name="minIdle"><value>${database.minIdle}</value></property>  
        <property name="removeAbandoned"><value>${database.removeAbandoned}</value></property>  
        <property name="removeAbandonedTimeout"><value>${database.removeAbandonedTimeout}</value></property>  
        <property name="connectionProperties"><value>${database.connectionProperties}</value></property>
	</bean>
	<!-- 事务 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- MyBatis -->
	<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath:mybatis/*.xml" />
	</bean>
	<bean id="weatherDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
	    <property name="sqlSessionFactory" ref="sessionFactory" />
		<property name="mapperInterface" value="com.lljqiu.test.dao.WeatherDao" />
	</bean>
</beans>

spring-dispatcher.xml

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
	<!-- 开启aspectJ支持, 强制使用cglib实现动态代理 -->
	<aop:aspectj-autoproxy proxy-target-class="true" />
	<context:component-scan base-package="com.lljqiu" />
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:config/ftl.properties</value>
			</list>
		</property>
	</bean>
	<!-- Freemarker Configuration -->
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="configLocation" value="classpath:config/ftl.properties" />
		<property name="templateLoaderPath" value="/WEB-INF/views" />
		<property name="defaultEncoding" value="UTF-8" />
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">10</prop>
				<prop key="locale">zh_CN</prop>
			</props>
		</property>
	</bean>
	<bean id="ftlViewResolver"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="contentType" value="text/html;charset=UTF-8" />
		<!-- <property name="prefix" value="/WEB-INF/views" /> -->
		<property name="suffix" value=".ftl" />
	</bean>
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>
</beans>

[b]spring-service.xml[b]

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/util
		http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	<context:component-scan base-package="com.lljqiu" />
	<import resource="classpath*:spring/spring-*.xml" />
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:config/ftl.properties" />
	</bean>
</beans>
spring mvc 注解成功,DAO失败 No qualifying bean of type

追加datasource.properties

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/lljqiu?useUnicode=true&characterEncoding=utf-8
database.username=root
database.password=root
database.maxActive=20
database.initialSize=1
database.maxWait=60000
database.maxIdle=20
database.minIdle=3
database.removeAbandoned=true
database.removeAbandonedTimeout=180
database.connectionProperties=clientEncoding=UTF-8
database.minPoolSize=1
database.maxPoolSize=1

ftl.properties

classic_compatible=true
#auto_import=ftl/macro/commonMacro.ftl as commonMacro
datetime_format=yyyy-MM-dd HH:mm:ss
date_format=yyyy-MM-dd 
time_format=HH:mm:ss
number_format=#.####


#locale=zh_CN
template_update_delay=5
#number_format=0.######;
#boolean_format=true,false 
#auto_import="/WEB-INF/ftl_lib/ponyjava.com/index.ftl" as p, "/WEB-INF/ftl_lib/jeecms/index.ftl" as cms 
#whitespace_stripping=true
#default_encoding=UTF-8 
#tag_syntax=auto_detect
#url_escaping_charset=UTF-8 
#struts.freemarker.templatesCache=true 
#template_update_delay=60000
spring mvc 注解成功,DAO失败 No qualifying bean of type

这是错误的信息,service注解成功,但是DAO 就是失败,困扰我很久了,希望大家帮我解决下多谢

严重: Servlet /test threw load() exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lljqiu.test.dao.WeatherDao] 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:1103)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
	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:1017)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
	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.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
	at javax.servlet.GenericServlet.init(GenericServlet.java:160)
	at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1274)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1186)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1081)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5033)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5320)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	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:744)
spring mvc 注解成功,DAO失败 No qualifying bean of type
5分
看起来好高大上的样子,有这方面问题的可以借鉴一下,个人感觉不错。
spring mvc 注解成功,DAO失败 No qualifying bean of type
5分
恩,挺好,最近也遇到这种问题,看到楼主的做法,瞬间开窍,问题已解决,谢谢!
spring mvc 注解成功,DAO失败 No qualifying bean of type
引用 5 楼 u013243453 的回复:

恩,挺好,最近也遇到这种问题,看到楼主的做法,瞬间开窍,问题已解决,谢谢!

诶,你怎么解决的,求赐教

spring mvc 注解成功,DAO失败 No qualifying bean of type
15分
不是很明白,感觉你是注解和配制混合着用的吧,但是混着用也没有发现你在xml中配制WeatherServiceImpl和WeatherDao的bean……但是错误很明显,WeatherDao没有注入到类WeatherServiceImpl
1.如果全用注解的话,你可以在WeatherServiceImpl中weatherDao上边再使用@Qualifer注解,把类WeatherDao注入到类WeatherServiceImpl中
2.如果想用在xml中配置bean的方式,建议你再检查一下了……有没有配置bean WeatherServiceImpl ,而且bean WeatherServiceImpl 中有没有配置bean WeatherDao……
另外,多说一句,你的WeatherAction类好像也有同样的问题
spring mvc 注解成功,DAO失败 No qualifying bean of type
引用 7 楼 u011424700 的回复:

不是很明白,感觉你是注解和配制混合着用的吧,但是混着用也没有发现你在xml中配制WeatherServiceImpl和WeatherDao的bean……但是错误很明显,WeatherDao没有注入到类WeatherServiceImpl
1.如果全用注解的话,你可以在WeatherServiceImpl中weatherDao上边再使用@Qualifer注解,把类WeatherDao注入到类WeatherServiceImpl中
2.如果想用在xml中配置bean的方式,建议你再检查一下了……有没有配置bean WeatherServiceImpl ,而且bean WeatherServiceImpl 中有没有配置bean WeatherDao……
另外,多说一句,你的WeatherAction类好像也有同样的问题

哥们,你说的那种方式(全注解)我试过了,好像专门和我做对使得,还是不对,

spring mvc 注解成功,DAO失败 No qualifying bean of type
15分

注解的问题,和我的结构很相似

@Repository     //看看这里
public interface WeatherDao {
    public WeatherDO findByCityname(String cityname);
    public WeatherDO findByCitycode(String citycode);
    public WeatherDO findByPostcode(String postcode);
   }
spring mvc 注解成功,DAO失败 No qualifying bean of type
问题已经解决,  配置出现了错误,
spring mvc 注解成功,DAO失败 No qualifying bean of type
配置哪里出现了问题,出现了什么问题,具体说一下
spring mvc 注解成功,DAO失败 No qualifying bean of type
差评 都不说怎么解决的
spring mvc 注解成功,DAO失败 No qualifying bean of type

spring-service.xml   可以看一下这个配置文件, 修改过后的配置文件

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/util
		http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<context:component-scan base-package="com.lljqiu" />


</beans>

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring mvc 注解成功,DAO失败 No qualifying bean of type
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!