Spring component-scan can’t find packages

spring

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;

enter image description here

Best Answer

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:-

enter image description here

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"/>
Related Topic