【已解决】A component required a bean of type ‘XXService‘ that could not found

一、问题

使用了Mybatis generator自动生成的bean和mapper
写完service层和controller层后运行报错A component required a bean of type 'XXService' that could not found

二、解决方式

在springBoot主入口函数Application里添加注释@MapperScan("com.SF.data.dao")
括号里为自己的mapper路径

@SpringBootApplication
@MapperScan("com.SF.data.dao")
public class Shoppingweb01Application {
    public static void main(String[] args) {
        SpringApplication.run(Shoppingweb01Application.class, args);
    }

}

三、产生原因

mapper文件位置和service文件位置不是父子层关系(同级)导致扫描时没有将sercive注入

四、一些试错

使用过@ComponentScan来进行包扫描、运行时确实不会报错了但数据出不来
可能是springboot在容器里找到了此组件但不知道交给谁去做解析

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <description>Spring公共配置文件</description> <!-- mes 的數據庫 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@10.142.252.132:1521:mestest"/> <property name="maxPoolSize" value="10"></property> <property name="maxIdleTime" value="1800"></property> <property name="minPoolSize" value="1"></property> <property name="initialPoolSize" value="1"></property> <property name="properties"> <ref bean="mesDatasourcePropertiesFactory" /> </property> </bean> <!-- c3p0数据源的一个专有属性,只可以存放密码和用户名 --> <bean id="mesDatasourcePropertiesFactory" class="com.ccc.db.impl.DatasourcePropertiesFactory" factory-method="getProperties"> <!-- userName--> <constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU=</value> </constructor-arg> <!-- password --> <constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU=</value> </constructor-arg> <!-- 生产环境模式 ,才特殊处理加密密码--> <constructor-arg type="java.lang.String"> <value>true</value> </constructor-arg> </bean> <!-- ptc windchill的數據庫 --> <bean id="dataSourcePdm" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@10.142.252.132:1521:mesdev"/> <property name="maxPoolSize" value="10"></property> <property name="maxIdleTime" value="1800"></property> <property name="minPoolSize" value="1"></property> <property name="initialPoolSize" value="1"></property> <property name="properties"> <ref bean="ptcDatasourcePropertiesFactory" /> </property> </bean> <!-- c3p0数据源的一个专有属性,只可以存放密码和用户名 --> <bean id="ptcDatasourcePropertiesFactory" class="com.ccc.db.impl.DatasourcePropertiesFactory" factory-method="getProperties"> <!-- userName--> <constructor-arg type="java.lang.String"> <value>WgDH/SDIJfs=</value> </constructor-arg> <!-- password --> <constructor-arg type="java.lang.String"> <value>WgDH/SDIJfs=</value> </constructor-arg> <!-- 生产环境模式 ,才特殊处理加密密码--> <constructor-arg type="java.lang.String"> <value>true</value> </constructor-arg> </bean> <!-- mes數據源代理 --> <bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy" p:targetDataSource-ref="dataSource"/> <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能--> <context:component-scan base-package="com.ccc"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" /> <!-- 配置事务管理器 針對MES數據庫--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourceProxy"/> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.ccc..*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/> </aop:config> <!-- 配置事务管理器,這個事務性是爭對pdm數據庫的 --> <bean id="transactionManagerPdm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourcePdm"/> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvicePdm" transaction-manager="transactionManagerPdm"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethodPdm" expression="execution(* com.ccc.pdm..*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethodPdm" advice-ref="txAdvicePdm"/> </aop:config> <!-- ibatis插件 --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" p:dataSource-ref="dataSourceProxy"> <property name="configLocation"> <value>classpath:SqlMapConfig.xml</value> </property> </bean> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <!-- 配置要拦截的url,防止2次提交或做其他數據統計用 <bean id="doubleSubmitInterceptor" class="com.ccc.filter.DoubleSubmitInterceptor"> <property name="mappingURL" value=".html" /> <property name="viewURL" value=".html" /> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"> <property name="interceptors"> <list> <ref bean="doubleSubmitInterceptor"/> </list> </property> </bean> --> <!-- JDBC template注入及事務配置 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"><ref bean="dataSourceProxy"/></property> </bean> </beans>
<!--配置连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///maven"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!--配置生产SqlSession对象的工厂--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--扫描pojo包,给包下所有pojo对象起别名--> <property name="typeAliasesPackage" value="com.itheima.domain"/> </bean> <!--扫描接口包路径,生成包下所有接口的代理对象,并且放入spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itheima.dao"/> </bean> <!--dao层配置文件结束--> <!--service层配置文件开始--> <!--组件扫描配置--> <context:component-scan base-package="com.itheima.service"/> <!--aop面向切面编程,切面就是切入点和通知的组合--> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置事务的通知--> <tx:advice id="advice"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--配置切面--> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.itheima.service.impl.*.*(..))"/> <aop:advisor advice-ref="advice" pointcut-ref="
文件夹 PATH 列表 卷序列号为 4E8D-6931 C:. │ .txt │ assess.zip │ └─assess │ assess.sql │ pom.xml │ ├─.idea │ │ assess.eml │ │ assess.iml │ │ compiler.xml │ │ encodings.xml │ │ misc.xml │ │ modules.xml │ │ uiDesigner.xml │ │ webContexts.xml │ │ workspace.xml │ │ │ ├─artifacts │ │ assess_war.xml │ │ assess_war_exploded.xml │ │ │ └─libraries │ Maven__javax_servlet_javax_servlet_api_4_0_1.xml │ Maven__javax_servlet_jstl_1_2.xml │ Maven__mysql_mysql_connector_java_5_1_38.xml │ ├─src │ └─main │ ├─java │ │ └─com │ │ └─demo │ │ ├─bean │ │ │ UserBean.java │ │ │ │ │ ├─dao │ │ │ JdbcUtils.java │ │ │ │ │ ├─service │ │ │ UserService.java │ │ │ │ │ └─web │ │ └─controller │ │ LoginController.java │ │ UserController.java │ │ │ ├─resources │ └─webapp │ │ add.jsp │ │ index.jsp │ │ login.jsp │ │ modify.jsp │ │ │ ├─assets │ │ └─bootstrap-3.3.7 │ │ ├─css │ │ │ bootstrap-theme.css │ │ │ bootstrap-theme.css.map │ │ │ bootstrap-theme.min.css │ │ │ bootstrap-theme.min.css.map │ │ │ bootstrap.css │ │ │ bootstrap.css.map │ │ │ bootstrap.min.css │ │ │ bootstrap.min.css.map │ │ │ │ │ ├─fonts │ │ │ glyphicons-halflings-regular.eot │ │ │ glyphicons-halflings-regular.svg │ │ │ glyphicons-halflings-regular.ttf │ │ │ glyphicons-halflings-regular.woff │ │ │ glyphicons-halflings-regular.woff2 │ │ │ │ │ └─js │ │ bootstrap.js │ │ bootstrap.min.js │ │ npm.js │ │ │ ├─images │ │ background.png │ │ │ └─WEB-INF │ web.xml │ └─target ├─assess-1.0.0-SNAPSHOT │ │ add.jsp │ │ index.jsp │ │ login.jsp │ │ modify.jsp │ │ │ ├─assets │ │ └─bootstrap-3.3.7 │ │ ├─css │ │ │ bootstrap-theme.css │ │ │ bootstrap-theme.css.map │ │ │ bootstrap-theme.min.css │ │ │ bootstrap-theme.min.css.map │ │ │ bootstrap.css │ │ │ bootstrap.css.map │ │ │ bootstrap.min.css │ │ │ bootstrap.min.css.map │ │ │ │ │ ├─fonts │ │ │ glyphicons-halflings-regular.eot │ │ │ glyphicons-halflings-regular.svg │ │ │ glyphicons-halflings-regular.ttf │ │ │ glyphicons-halflings-regular.woff │ │ │ glyphicons-halflings-regular.woff2 │ │ │ │ │ └─js │ │ bootstrap.js │ │ bootstrap.min.js │ │ npm.js │ │ │ ├─images │ │ background.png │ │ │ ├─META-INF │ │ MANIFEST.MF │ │ │ └─WEB-INF │ │ web.xml │ │ │ ├─classes │ │ └─com │ │ └─demo │ │ ├─bean │ │ │ UserBean.class │ │ │ │ │ ├─dao │ │ │ JdbcUtils.class │ │ │ │ │ ├─service │ │ │ UserService.class │ │ │ │ │ └─web │ │ └─controller │ │ LoginController.class │ │ UserController.class │ │ │ └─lib │ jstl-1.2.jar │ mysql-connector-java-5.1.38.jar │ ├─classes │ └─com │ └─demo │ ├─bean │ │ UserBean.class │ │ │ ├─dao │ │ JdbcUtils.class │ │ │ ├─service │ │ UserService.class │ │ │ └─web │ └─controller │ LoginController.class │ UserController.class │ └─generated-sources └─annotations
"A component required a bean of type that could not be found"这个错误通常是因为在应用程序的配置中找不到所需的bean类型引起的。根据提供的引用内容,有几种可能的解决方法。首先,你可以考虑在应用程序的配置文件中定义所需类型的bean。例如,在引用中,建议在配置中定义一个类型为'xxx.service.xxxService'的bean。同样,在引用中,建议在配置中定义一个类型为'xxx.config.xxxConfig'的bean。这样,应用程序就能找到所需的bean类型,解决了错误。另外,你还可以检查是否正确导入了相关的包,并确保类路径和包名与配置文件中的一致。如果问题仍然存在,可以尝试重新编译或重新构建项目,以确保所有的依赖项都正确配置。总之,解决"A component required a bean of type that could not be found"错误的方法是在配置文件中定义所需的bean类型,并确保相关的包和类路径正确配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [A Component required a bean of typexxx.config.xxx‘ that could not be found问题解决办法](https://blog.csdn.net/qq_39691492/article/details/118085232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值