Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration using annotation @SpringBootApplication

I have problem with Spring Boot configuration.

I have created base Spring Boot project using https://start.spring.io/

And I have a problem, configuration works only for classes in sub catalog:

enter image description here

I have tried annotation @ComponentScan but it didn't help.

Do You have any idea what can I do with this?

like image 447
Piotr Żak Avatar asked Nov 09 '15 22:11

Piotr Żak


People also ask

What exactly @SpringBootApplication annotation does?

Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. It's same as declaring a class with @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.

Which of the following features are enabled using @SpringBootApplication annotation?

A single @SpringBootApplication annotation can be used to enable those three features, that is: @EnableAutoConfiguration : enable Spring Boot's auto-configuration mechanism. @ComponentScan : enable @Component scan on the package where the application is located (see the best practices)

What is the use of SpringBootApplication?

We use the @SpringBootApplication annotation in our Application or Main class to enable a host of features, e.g. Java-based Spring configuration, component scanning, and in particular for enabling Spring Boot's auto-configuration feature.

What is @configuration @EnableAutoConfiguration and @ComponentScan?

How They Differ. The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.


1 Answers

The Spring Boot documentation for @SpringBootApplication states

Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplication alternative.

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes: [...]

where the @ComponentScan javadoc states

If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

That is, only the types that are in the same package as your ReadingListApplication will be scanned.

If you want a custom configuration, provide your own @Configuration, @EnableAutoConfiguration, and @ComponentScan, as appropriate.

like image 127
Sotirios Delimanolis Avatar answered Oct 05 '22 19:10

Sotirios Delimanolis