关于component-scan中base-package包含通配符的问题探究

今天在配置Spring的component-scan时,发现了一个有趣的问题。就是在指定base-package时,如果使用了星号通配符*,有时会出现类扫描不到的情况。下面研究一下这个问题。

先介绍一下项目结构:
为了演示,我在java文件夹下创建名为controller的包,并在该包下创建了一个名为IndexController的类。如图所示:
[外链图片转存中…(img-56dXRqQM-1650983954077)]
[外链图片转存中…(img-UFjBWHGv-1650983954078)]

先来看正常情况:
在Spring配置文件中配置Component-Scan:
<context:component-scan base-package="controller" />
启动项目,访问localhost:8080/index.do,结果正常。
[外链图片转存中…(img-ox3xZcfr-1650983954078)]

但,当我把component-scan配置成这样时:
<context:component-scan base-package="controller.*" />
出现了404,说明Spring没有扫描到我的Controller,所以无法处理我们的请求。
[外链图片转存中…(img-iJRN4iuc-1650983954079)]

但,当我把component-scan配置成这样时:
<context:component-scan base-package="controller.**" />
又一切正常了。

这是为啥呢,我们打个断点看一下:
base-package="controller"时,可见packageSearchPath为"classpath*:controller/**/*.class"
[外链图片转存中…(img-Mpx9q4OL-1650983954080)]

base-package="controller.*"时,可见packageSearchPath为"classpath*:controller/*/**/*.class"
[外链图片转存中…(img-7jxpIlmY-1650983954082)]

base-package="controller.**"时,可见packageSearchPath为"classpath*:controller/**/**/*.class"
[外链图片转存中…(img-SiAu5AgZ-1650983954084)]

综上, 可以分析出,**匹配任意class文件和包,而*只能匹配包,因此无法扫描到包下的类,因此也就无法被Spring管理。

在这里插入图片描述

<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 明确指定只扫描Controller --> <context:component-scan base-package="com.itheihei" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/images/**" location="/images/"/> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:annotation-driven/> </beans><?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 https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 排除Controller,只扫描Service/Repository等 --> <context:component-scan base-package="com.itheihei"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>@Controller public class AccountController { @Autowired AccountService accountService; @RequestMapping("findAllAccounts") public String findAllAccounts() { System.out.println("方法成功执行"); return "list"; } } <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <a href="${pageContext.request.contextPath}/findAllAccounts">查看所有用户信息</a> </body> </html><?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> <!-- 将所有请求映射到 dispatcherServlet --> </servlet-mapping> </web-app>为什么会404
最新发布
05-14
在uni-app中实现小程序上传大文件的方法可以使用文件切片上传的方式。首先,需要明确需求和方案,然后可以开始动手了。大文件上传的难点之一是实现断点续传。具体的实现方法是使用uni-app官方内部方法uni.chooseFile来实现H5端的上传,而在小程序端,由于不能使用本地HTML,可以使用uni-app官方内部方法wx.chooseMessageFile。在移动端,可以使用web-view组件,并在该组件内使用input元素的type="file"来实现上传。目前,该方法支持上传各种类型的文件,如图片、视频、文件等。如果只想上传单个类型的文件,比如只上传图片或者视频或者某个特定类型的文件,可以参考input的accept属性。切片上传大文件是最复杂的部分之一,需要将文件切割成多个片段并进行上传。关于具体的切片上传实现方式,可以参考uniapp文档以及微信官方文档中关于FileSystemManager的部分。在实现断点续传时,需要注意一些细节。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [uniapp 微信小程序 分片 断点续传 大文件上传](https://blog.csdn.net/qq_34157798/article/details/119324994)[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: 50%"] - *2* [uniApp移动端-H5-微信小程序上传文件(图片,文档和视频等)](https://blog.csdn.net/weixin_45145119/article/details/130581411)[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: 50%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值