Spring MVC 配置报错: Error creating bean with name ‘MenuController’: Injection of resource dependencies
曾经困扰我一整天的错误,终于解决了。这段报错代码的意思是:创建名为“Menucontroller”的bean时出错:资源依赖项的注入
这是applicationContext.xml里的内容
<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>