Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring component-scan can't find packages

Tags:

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

like image 259
hellzone Avatar asked Oct 20 '13 11:10

hellzone


2 Answers

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"/>
like image 161
limc Avatar answered Oct 16 '22 07:10

limc


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"/>
like image 35
Debojit Saikia Avatar answered Oct 16 '22 08:10

Debojit Saikia