springmvc搭建遇到的一个小坑,大神自动略过。。。

本文通过实例演示了在Spring MVC框架中,当存在多个实现相同接口的Bean时,如果Bean名称与接口变量名一致,Spring将优先使用同名Bean进行注入的情况。了解此行为有助于避免潜在的注入错误。

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

   最近在学习springmvc,在搭建运行的时候遇到的一个小坑(其实这个坑是spring导致的),记录如下,希望遇到的朋友能知道怎么回事,越过小坑。

  由于spring自动注入的时候,会自动寻找符合条件的bean,进行依赖注入。于是本人添加了两个实现了同样接口的dao层,想看下错误信息。奈何却一直不报错。最后才想到,由于命名的原因,导致spring会优先选用在自动注入时同名的bean进行注入。如有同样的人采坑,以作参考,大神请绕行。

代码如下(环境spring5.0,还一度以为是版本问题,后改为4.3.0):

  1. 使用maven管理jar包,创建基本框架

    

2. resources下添加config/springmvc-config.xml(自定义配置文件),配置开启自动扫包及jsp页面解析器

 

springmvc-config.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">


	<mvc:annotation-driven />
	<context:component-scan
		base-package="com.mint.controller" />


	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>


</beans>

3. 添加了context启动加载的配置文件application.xml,只加入了两个扫包配置


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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<context:component-scan base-package="com.mint.dao" />
	<context:component-scan base-package="com.mint.service" />

</beans>

4. 修改web.xml , 添加过滤器、listener及配置springmvc如下:

  

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>springmvc</display-name>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>



	<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>
		<init-param>
			<param-name>forceRequestEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>forceResponseEncoding</param-name>
			<param-value>true</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>

	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/springmvc-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

4. 创建代码层级结构如下所示:

 

关键代码(以dao层为例,仅有一个方法模拟数据库取数据):

  

@Override
	public List<Student> getStudents() {
		Student s1 = new Student();
		s1.setSage(10);
		s1.setSname("哈士奇");
		s1.setSno("001");
		s1.setSsex("男");
		Student s2 = new Student();
		s2.setSage(10);
		s2.setSname("萨摩");
		s2.setSno("002");
		s2.setSsex("男");
		Student s3 = new Student();
		s3.setSage(10);
		s3.setSname("二黑");
		s3.setSno("003");
		s3.setSsex("女");
		Student s4 = new Student();
		s4.setSage(10);
		s4.setSname("小明");
		s4.setSno("001");
		s4.setSsex("男");

		List<Student> students = new ArrayList<Student>();
		students.add(s1);
		students.add(s2);
		students.add(s3);
		students.add(s4);

		return students;
	}

5. service层自动注入dao层,controller层自动注入service层(代码不再粘贴),并在页面显示dao层返回来的数据。

<h1>欢迎来到首页</h1>
	<table>
		<th>
		<tr>
			<td>学号</td>
			<td>姓名</td>
			<td>性别</td>
			<td>年龄</td>
		</tr>
		</th>
		<tbody>
			<s:forEach items="${students}" var="stu">
				<tr>
					<td>${stu.sno}</td>
					<td>${stu.sname}</td>
					<td>${stu.ssex}</td>
					<td>${stu.sage}</td>
				</tr>
			</s:forEach>
		</tbody>

6. 结果显示如下:

  


以上为代码背景,接下来就是问题了:

 在dao层,又添加一个HomeDao2,返回结果为null:

  

service层自动注入代码如下:


    本以为想着,两个实现了同样接口的dao类,spring应该会报错,说有两个符合条件的bean,不知道注入哪一个(关于这个问题,可以增加注解值进行进一步标识取bean)。然后,并没有。。。。。。,重启浏览器,访问地址,照样显示结果。缓存问题??清理缓存,运行还是一样。

   于是,去掉HomeDao的注解,让spring不再扫到它,注意此时HomeDao已经不再被spring所管理。


   再次重启,刷新


  没错,这次spring注入了的是HomeDao2,再给HomeDao添加注解并重启,即恢复数据显示。说好的报错呢????于是返回去又仔细看了一眼service层注入的代码,不由的菊花一紧。。。。

 

    是的,我这个IHome的变量,正好是HomeDao的BeanName。于是赶紧改了名字一试,果然spring会优先使用同名的bean进行注入。



错误信息报出了(spring:我需要一个实现IHomeDao的实例,你却给了我两个,我该用哪个呢?):

  

后记:关于此问题,还需要在翻阅spring源代码进行验证,但猜测一下,感觉spring应该是优先使用beanName进行反射寻找同名bean进行注入。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值