Error creating bean with name 'MenuController': Injection of resource dependencies

这篇博客介绍了在Spring MVC项目中遇到的'Error creating bean with name 'MenuController''报错问题,以及解决方法。错误原因是资源依赖项的注入失败。解决方案包括检查default-autowire配置、确保@Service和@Resource注解的正确使用,以及确认注解扫描是否包含Controller Bean所在的包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring MVC 配置报错: Error creating bean with name ‘MenuController’: Injection of resource dependencies
曾经困扰我一整天的错误,终于解决了。这段报错代码的意思是:创建名为“Menucontroller”的bean时出错:资源依赖项的注入
这是我的项目整体结构这是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:aop=“http://www.springframework.org/schema/aop”
xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:context=“http://www.springframework.org/schema/context”
xsi:schemaLocation=“http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd” default-autowire=“default”>

<context:component-scan base-package=“com.bjsxt”></context:component-scan>

<context:property-placeholder location=“classpath:db.properties”/>






















<tx:advice id=“txAdvice” transaction-manager=“txManage”>
tx:attributes
<tx:method name=“ins*”/>
<tx:method name=“del*”/>
<tx:method name=“upd*”/>
<tx:method name="" read-only=“true”/>
</tx:attributes>
</tx:advice>

<aop:config proxy-target-class=“true”>
<aop:pointcut expression="execution(
com.bjsxt.service.impl..(…))" id=“mypoint”/>
<aop:advisor advice-ref=“txAdvice” pointcut-ref=“mypoint”/>
</aop:config>

里面有个默认自动装配的配置叫default-autowire,我当时出现这个错误时,设置的是byName参数,把参数设置成default就可以正常运行项目了。

如果上面的方法不管用,还有两种情况也会导致控制台报这个错误。

第一种情况:没有在实现类写@Service和@Resourse注解
package com.bjsxt.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
@Service
public class UsersServiceImpl implements UsersService{
@Resource
private UsersMapper usersMapper;
@Override
public Users login(Users users) {

	return usersMapper.selById(users);
}

}

第二种情况:在applicationContext.xml和springMVC.xml配置文件中,注解扫描没有扫描到MenuController的Bean
最好的办法就是把两个配置文件的注解扫描配置直接写父包名,这样父包下的所有子包就都能被扫描到了。

<context:component-scan base-package="com.bjsxt"></context:component-scan>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值