spring报错Error creating bean with name 'com.baobaotao.service.TestUserService'

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.baobaotao.service.TestUserService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.baobaotao.service.UserService com.baobaotao.service.TestUserService.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.baobaotao.service.UserService] 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)}

这是报错信息 下面贴出applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd" default-autowire="byName">

<context:annotation-config/>

<!-- 扫描类包,将标注spring注解的类自动转化为bean,同时完成bean的注入 -->
<context:component-scan base-package="com.baobaotao.dao" />

<!-- 扫描本地service类包,应用Spring注解配置 -->
<context:component-scan base-package="com.baobaotao.service" />

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />

<!-- 通过aop配置提供事务增强 , 让service 包下所有的bean的所有方法拥有事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(*com.baobaotao.service..*(..))" id="serviceMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

<!-- 定义一个使用dbcp实现的数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/sampledb"
p:username="root"
p:password="1234" />

<!-- 定义jdbc模板的bean -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource" />

</beans>

Error creating bean with name 'com.baobaotao.service.TestUserService'的错误原因如下:

1、在使用@Autowired注解时,找不到相应的类。

具体的解决办法:设置如下命令:

@Service("XXXXX")

public class XXXXXServiceImpl implements XXXXXService{

......

}

2、没有扫描到@Service标注的类。

具体的解决办法:设置如下命令:

@Service

public classUserServiceImpl implements UserService{

....

}

扩展资料:

组成Spring框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下:

1、核心容器:核心容器提供 Spring 框架的基本功能(Spring Core)。核心容器的主要组件是 BeanFactory,它是工厂模式的实现。BeanFactory 使用控制反转(IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。

2、Spring 上下文:Spring 上下文是一个配置文件,向 Spring框架提供上下文信息。Spring 上下文包括企业服务,例如JNDI、EJB、电子邮件、国际化、校验和调度功能。

3、Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向切面的编程功能集成到了 Spring 框架中。

所以,可以很容易地使 Spring 框架管理的任何对象支持AOP。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖 EJB 组件,就可以将声明性事务管理集成到应用程序中。

4、Spring DAO:JDBCDAO抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。

5、Spring ORM:负责框架中对象关系映射,提供相关ORM 接入框架的关系对象管理工具 。Spring 框架插入了若干个ORM框架,从而提供了 ORM 的对象关系工具,其中包括JDO、Hibernate和iBatisSQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。

参考资料来源:百度百科-spring

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-04-15

错误原因:

1、在使用@Autowired注解时,找不到相应的类。

2、因为XXXXX的实现类中没有加相应的注解,例如DAO层 @Repository ,Service层  @Service。

3、没有扫描到@Service标注的类

解决方法:

在service的实现类XXXXXServiceImpl.java的开始,追加@Service("XXXXX"),如下:

@Service("XXXXX")

public class XXXXXServiceImpl implements XXXXXService{

......

}

步骤如下:

1.在service的实现类XXXXXServiceImpl.java的开始,追加@Service("XXXXX"),如下:

2.在DAO层:

扩展资料:

项目配置上下文参数:

1、使用xml(build时从profile写值到key-value):启动服务时初始化方法(web.xml定义servlet)初始化。

2、使用properties(build时从profile写值到key-value):在xxx-servlet.xml或applicationcontext.xml中<context:property-placeholder ignore-unresolvable="true" location="classpath:my.properties" />

本回答被网友采纳
第2个回答  2014-10-28
1找到com.baobaotao.service.UserService实现类型 @Service这个扔到实现类上
2 TestUserService的控制器使用接口声明要注入的变量
@Autowired
private UserService userService;
不知道具体怎么错误,你测试下。追问

你说的这两点我都做了 ~ 可是还是报错 ~~

追答

No qualifying bean of type [com.baobaotao.service.UserService] found for dependency
怎么说没有实现类 你有没有
@Service

  public classUserServiceImpl implements UserService{
....

}
implements UserService这个有没有?或者没有写他实现类?

它提示至少要有一个实现类来支持这个依赖注入
expected at least 1 bean which qualifies as autowire candidate for this dependency.

本回答被网友采纳
第3个回答  2014-10-28
TestUserService
你这个bean定义在哪了?

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网