Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@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;
    } }
like image 570
Moolerian Avatar asked Feb 07 '23 01:02

Moolerian


1 Answers

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.

like image 160
dunni Avatar answered Feb 10 '23 11:02

dunni