springmvc 注解失败 No qualifying bean of type

J2EE 码拜 8年前 (2016-03-12) 1058次浏览
这是代码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>
解决方案

5

看起来好高大上的样子,有这方面问题的可以借鉴一下,个人感觉不错。springmvc 注解失败 No qualifying bean of type

5

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

15

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

15

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

@Repository     //看看这里
public interface WeatherDao {
    public WeatherDO findByCityname(String cityname);
    public WeatherDO findByCitycode(String citycode);
    public WeatherDO findByPostcode(String postcode);
   }

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