Moolerian
Moolerian

Reputation: 562

@SpringBootApplication in same package?

I wrote an app based on Spring Boot, but it works when i put all class (model , contoller that annotated with @restController) in the same package of where SpringBoot exist . My question is why these classes must be in the same package ?

this is the Spring Boot App annotated :

@SpringBootApplication    
public class Application {
   public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
      } }

This is the rest controller :

@RestController
public class PersonController {

    @RequestMapping("/Hello")
    public String syaHello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return "Hello " + name;
    } }

Upvotes: 4

Views: 8862

Answers (1)

dunni
dunni

Reputation: 44535

Because this is the default behaviour of the @SpringBootApplication annotation. More correctly, component scanning detects all configuration and components in the package and all subpackages of the class with the annotation. If you want to have your classes in other packages, then you can specify those packages or classes with the packages as attributes in the annotation:

@SpringBootApplication(scanBasePackageClasses = {OneClass.class, AnotherClass.class})

Spring will then scan the packages and subpackages of the classes OneClass and AnotherClass.

Upvotes: 8

Related Questions