Spring component-scan can't find packages. I think there is a problem with my project directory. I am using Intellij Idea.
My 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="" />
My directory;
By the way, your Maven project structure is incorrect, which is why IntelliJ shows your Java packages as regular directories. Based on the Maven standard directory layout, you need to create src/main/java
. So, your project structure should look like this:-
src
|- main
|- java
|- tr
|- source
|- beans
|- resources
|- webapps
|- ...
Now, when you create src/main/java
, IntelliJ will treat java
directory as a regular directory (shown as an orange directory icon). To make it a source directory (blue directory icon), go to IntelliJ's Project Structure and add java
directory as source directory. You should see something like this:-
Once src/main/java
becomes a blue directory icon, assuming the base package you want to scan is tr
, go to your Spring XML configuration and change this line to this:-
<context:component-scan base-package="tr"/>
The scanning of classpath packages requires the presence of corresponding directory entries in the classpath. And to autodetect
the beans
and register the corresponding beans requires the inclusion of
<context:component-scan base-package="com.example"/>
where base-package
would be a package for the classes. So in order to find beans defined in package tr.source
and its sub-packages, you need to define base-package
as:
<context:component-scan base-package="main.tr.source"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With