Stefano Sambruna
Stefano Sambruna

Reputation: 797

Should @ComponentScan stay in the class containing the main method?

I know that @ComponentScan with @Configuration tell Spring where to look for beans.

@ComponentScan
@Configuration
public class MyApp{
     ...
}

What I do not understand is on which class I have to put these two annotations. Should they stay on the class containing the main method? Like this

@ComponentScan
@Configuration
public class MyApp{

     public static void main(String[] args) {
          ...
     }

}

Or they can stay on whatever class of the application? The question comes from the fact that Spring has to know the location of @ComponentScan... or is there an automatic way of detection of the @ComponentScan annotation which Spring is performing under the hood? Hope to have explained myself!

Upvotes: 1

Views: 1976

Answers (3)

quintin
quintin

Reputation: 837

Using simple example. You can place @ComponentScan with @Configuration in any class which main method can scan.

Main class scans MyScan class which then scan for bean class.

package com.boot.spring;

@SpringBootApplication
@ComponentScan(basePackages = "com.boot.scan")
public class BootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(BootApplication.class, args);
        System.out.println(ctx.getBean("demoBean"));
    }
}

Bean class is in different package

package com.boot.bean;
@Service
public class DemoBean {
}

Now, bean class is discovered through DemoScan class

package com.boot.scan;
@ComponentScan(basePackages = "com.boot.bean")
@Configuration
public class DemoScan {
}

Upvotes: 0

Ayush28
Ayush28

Reputation: 379

@Configuration annotation tells the Spring container that the class contains Spring bean configuration.

@ComponentScan annotation tells the Spring container that the annotated class to scan/searches for other annotations and components. You can also define package name to scan with the annotation like @ComponentScan("your.package.name") or you can give package/class names that need not be scanned.

Hence, you can put these annotations on any class that defines your bean configuration and could be required by spring container to parse and create objects for your entities/POJOs, services and DAOs.

To conclude, I would like to add @ComponentScan and other annotations are there for automatic detection. Else, you would need to define XMLs (that's what happens under the hood with annotations) for spring to read and perform these actions.

Upvotes: 1

asgarov1
asgarov1

Reputation: 4171

You can put it wherever you want (I usually put mine in com.domain.project-name.config) and just specify the directories it should scan, for example if you want it to scan everything in project use

@ComponentScan("com.domain.project-name")
@Configuration
public class Config {
...

By default, ComponentScan scans all the annotated classes at the current directory level and below.

Upvotes: 1

Related Questions