Search code examples
spring-bootspring-mvcspring-securityspring-data-jpathymeleaf

How to Register beans from all sub packages in Spring boot app?


I am creating new spring boot + jpa + thymeleaf application, I am using many modules which are placed in sub packages. I have structure like ge.my.project where is placed my main class with @SpringBootApplication and under this package I have sub packages ge.my.project.dao,ge.my.project.service,ge.my.project.controller and so on where are placed my beans.@SpringBootApplication scans only beans under base(ge.my.project) package but I want to scan beans from all sub packages.

I tried many variants to scan sub packages :

@ComponentScan(basePackages = {"ge.my.project.controller","ge.my.project.service","ge.my.project.configuration"})

and

@ComponentScan({"ge.my.project.controller","ge.my.project.service","ge.my.proj ect.configuration"})

and

@ComponentScan("ge.my.project.*")

but nothing works , When I am trying to inject beans using @Autowired I am getting error like this Consider defining a bean of type 'ge.my.project.service.ServiceTypeService' in your configuration.

Here is my main class

package ge.my.project;

import ge.ufc.inhouseProjects.controller.ServiceTypeController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


 @SpringBootApplication
//@ComponentScan(basePackages = {"ge.my.project.controller","ge.my.project.service","ge.my.project.configuration"})
@ComponentScan({"ge.my.project.*"})
@EntityScan({"ge.my.project.entity"})
@EnableJpaRepositories("ge.my.project.dao")
public class InhouseProjectsApplication {

public static void main(String[] args) {
    /*ApplicationContext applicationContext = */SpringApplication.run(InhouseProjectsApplication.class, args);
}

}

Here is my full project https://github.com/JavaGeoGroup/inhouseProjects.git

Which is the clearest way to scan all project beans in spring boot application?


Solution

  • You need to annotate the implementation and not the interface. So in your case the @Service annotation needs to be set on ServiceTypeDaoImpl-class instead of ServiceTypeDao-interface.


    You need to annotate your beans (your implementation of interfaces, not spring data repo interfaces) with the @Component annotation or one of its sub-annotations like @Repository, @Service, @Controller, @Configuration.

    From the javadoc of @Component:

     * Indicates that an annotated class is a "component".
     * Such classes are considered as candidates for auto-detection
     * when using annotation-based configuration and classpath scanning.
     *
     * <p>Other class-level annotations may be considered as identifying
     * a component as well, typically a special kind of component:
     * e.g. the {@link Repository @Repository} annotation or AspectJ's
     * {@link org.aspectj.lang.annotation.Aspect @Aspect} annotation.
    

    Specific extensions to your code-example: Spring beans start with a small case, so your interface "ServiceTypeDao"will be registred as "serviceTypeDao" as long as you dont set a specific name in the "@Service" annotation.