user3216993
user3216993

Reputation: 1

@ComponentScan on Spring Boot

I can't get Spring Boot to find and inject objects. The main method is in the Application.java class, and I need to inject objects that are in another branch and sub-branches.

If you look at the image, I need to inject objects that are in the pablosz.bot.framework, pablosz.bot.framewokr.persistentobjects, z.domain and z.screens packages.

I have tried @ComponentScan, @CompoonentsScan @EntityScan, etc. Could anyone help me please?

Packages of my app (see the image)

Here is the Application.java code.

package z.futbol;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@ComponentScan({"pablosz.bot.framework"
               ,"pablosz.bot.framework.persistentobjects"
               ,"z.futbol"
               ,"z.futbol.domain"})
@Configuration
public class Application  
{
    public static void main(String[] args)
    {
        try
        {
            SpringApplication.run(Application.class,args);          
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

Upvotes: 0

Views: 1736

Answers (1)

sixrandanes
sixrandanes

Reputation: 4407

Spring boot will perform a component scan from the packages (and subpackages) where you are declaring your main class (the class where you declare @SpringBootApplication).

You don't need to declare the packages z.futbol in component scan. You only have to add the packages that are outside of your main package (pablosz.**).

Don't forget to declare the beans you want to put in the spring context with the spring's annotations.

You should remove @Configuration also which is useless.

Upvotes: 1

Related Questions