springmvc 注解失败 No qualifying bean of type

胡小素 2014-11-25 03:52:23
这是代码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>
...全文
206676 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
何俊清 2018-11-24
  • 打赏
  • 举报
回复
今天我使用spring+mybatis框架练习是也也到类似的问题。我理解的是注解不成功应该是xml中的context:component-scan 配置不对。比如包名之间应该是.号,而使用了/号。但我的错误完全是因为COPY XML文件配置后没有及时按照项目布局修改context:component-scan。比如使用@Service注解的代码如下:
package service.imp;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import mapper.Personmapper;
import pojo.Person;
import service.PersonService;


@Service("personService")
public class PersonServiceImp implements PersonService {
@Autowired
private Personmapper personMapper;

public Person getPersonByID(int id) {
// TODO Auto-generated method stub
Person person = new Person();
try {
person = personMapper.selectPersonById(id);

}catch(Exception e)
{
e.printStackTrace();
}
return person;
}

}

PersonService接口是在包service下,此时我把xml的中的
<context:component-scan base-package="fhsj.com"></context:component-scan>
修改成
<context:component-scan base-package="service"></context:component-scan>
<context:component-scan base-package="service.imp"></context:component-scan>
,此时问题解决。同时在把配置更改成
<context:component-scan base-package="service"></context:component-scan>
也能解决问题。说明@Service注解在使用spring自动的扫描功能时,仅仅扫描接口所在的包即可。
黑胖黑胖的 2017-07-31
  • 打赏
  • 举报
回复
竟然不是很懂
suixin804180426 2017-07-24
  • 打赏
  • 举报
回复
注入失败更常见的原因是在spring-servlet.xml中没有添加扫描目录,导致项目启动时没扫描相应的包,无法创建实例。解决办法是在spring-servlet.xml中添加要扫描的包 来源:http://blog.csdn.net/u011853294/article/details/51113212 本人配置错误:<mongo:repositories base-package="com.suixin.iznaer.repository" /> 应该为:<mongo:repositories base-package="com.suixin.repository" /> 也就是配置路径错了。
qq_32891697 2017-06-30
  • 打赏
  • 举报
回复
应该是 扫描 mapper 的包配置错误导致的问题
qq_28469029 2017-06-16
  • 打赏
  • 举报
回复
老子还等到解决问题 ,结果楼主跑去食屎去咯
qq_38148000 2017-03-31
  • 打赏
  • 举报
回复
请问楼主是怎么解决的
Java码农老王 2016-07-19
  • 打赏
  • 举报
回复
到底如何解决?
qq4990 2016-07-08
  • 打赏
  • 举报
回复
我来说解决方法! 去通用MAPPER 项目看 做大的重要提示 特别强调:不是表中字段的属性必须加@Transient注解​
xss13 2016-06-13
  • 打赏
  • 举报
回复 1
我也遇到这个问题了,SpringMVC和SpringIOC是两个容器,IOC是老大,MVC是老二,配置注解驱动扫描的时候,把Controller层配给MVC,其他层,比如业务层和持久层配给IOC就解决这个问题了。。。也就是说,扫描控制层的交给xx-servlet.xml,扫描业务层和持久层的交给applicationContext.xml。
zdcsoul 2016-05-16
  • 打赏
  • 举报
回复
nai598455803 2016-04-25
  • 打赏
  • 举报
回复
config 建文件夹,别建包
ailaopo415 2015-08-13
  • 打赏
  • 举报
回复
今天也遇到了这个问题,impl在注入的时候,注入的是class类,而不是interface,从而引起错误。可以参考http://www.iteye.com/topic/1131444
胡小素 2015-02-02
  • 打赏
  • 举报
回复
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>
eagle199012 2015-01-19
  • 打赏
  • 举报
回复
差评 都不说怎么解决的
初露寒秋 2015-01-19
  • 打赏
  • 举报
回复
配置哪里出现了问题,出现了什么问题,具体说一下
胡小素 2014-11-29
  • 打赏
  • 举报
回复
问题已经解决, 配置出现了错误,
qzw1210 2014-11-25
  • 打赏
  • 举报
回复
注解的问题,和我的结构很相似

@Repository     //看看这里
public interface WeatherDao {
    public WeatherDO findByCityname(String cityname);
    public WeatherDO findByCitycode(String citycode);
    public WeatherDO findByPostcode(String postcode);
   }
雪翊寒 2014-11-25
  • 打赏
  • 举报
回复
胡小素 2014-11-25
  • 打赏
  • 举报
回复
引用 7 楼 u011424700 的回复:
不是很明白,感觉你是注解和配制混合着用的吧,但是混着用也没有发现你在xml中配制WeatherServiceImpl和WeatherDao的bean……但是错误很明显,WeatherDao没有注入到类WeatherServiceImpl 1.如果全用注解的话,你可以在WeatherServiceImpl中weatherDao上边再使用@Qualifer注解,把类WeatherDao注入到类WeatherServiceImpl中 2.如果想用在xml中配置bean的方式,建议你再检查一下了……有没有配置bean WeatherServiceImpl ,而且bean WeatherServiceImpl 中有没有配置bean WeatherDao...... 另外,多说一句,你的WeatherAction类好像也有同样的问题
哥们,你说的那种方式(全注解)我试过了,好像专门和我做对使得,还是不对,
qiang_yiqiyihui 2014-11-25
  • 打赏
  • 举报
回复
不是很明白,感觉你是注解和配制混合着用的吧,但是混着用也没有发现你在xml中配制WeatherServiceImpl和WeatherDao的bean……但是错误很明显,WeatherDao没有注入到类WeatherServiceImpl 1.如果全用注解的话,你可以在WeatherServiceImpl中weatherDao上边再使用@Qualifer注解,把类WeatherDao注入到类WeatherServiceImpl中 2.如果想用在xml中配置bean的方式,建议你再检查一下了……有没有配置bean WeatherServiceImpl ,而且bean WeatherServiceImpl 中有没有配置bean WeatherDao...... 另外,多说一句,你的WeatherAction类好像也有同样的问题
加载更多回复(5)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧