Spring 组件扫描找不到包。我认为我的项目目录有问题。我正在使用 Intellij Idea。
我的 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.xsd">
<context:component-scan base-package="" />
我的目录;
最佳答案
顺便说一句,您的 Maven 项目结构不正确,这就是 IntelliJ 将您的 Java 包显示为常规目录的原因。基于 Maven standard directory layout ,您需要创建 src/main/java
.所以,你的项目结构应该是这样的:-
src
|- main
|- java
|- tr
|- source
|- beans
|- resources
|- webapps
|- ...
现在,当您创建
src/main/java
, IntelliJ 将处理 java
目录作为常规目录(显示为橙色目录图标)。要使其成为源目录(蓝色目录图标),请转到 IntelliJ 的项目结构并添加 java
目录作为源目录。你应该看到这样的东西:-曾经
src/main/java
变为蓝色目录图标,假设您要扫描的基础包是 tr
,转到您的 Spring XML 配置并将此行更改为:- <context:component-scan base-package="tr"/>
关于spring 组件扫描找不到包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19476840/