Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 703 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Configuration using annotation @SpringBootApplication

#1
I have problem with Spring Boot configuration.

I have created base Spring Boot project using

[To see links please register here]


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

[![enter image description here][1]][1]

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

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



[1]:
Reply

#2
The [Spring Boot documentation for `@SpringBootApplication`][1] 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`][2] 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.



[1]:

[To see links please register here]

[2]:

[To see links please register here]


Reply

#3
When setting up a `Spring boot` project, have your Application class (the one that contains the `@SpringBootApplication` annotation in the base package.

One of the things the `@SpringBootApplication` does is a component scan. But, it only scans on **sub-packages**. i.e. if you put that class in *com.mypackage*, then it will scan for all classes in sub-packages i.e. com.mypackage.*.

If you do not want to do it this way, you can also add a `@ComponentScan` to a class specifying the root package i.e `@ComponentScan("com.mypackage")`

I would recommend you have a base package i.e *com.mypackage*. And within those packages, have your sub-packages. Have you class containing the `@SpringBootApplication` in that base package.
Reply

#4
Checking the Spring documentation:

[To see links please register here]


You can override, with the @SpringBootApplication, the default values of component scan. You just need to include it as a parameters:

`@SpringBootApplication(scanBasePackages = "entertainment")`

or String array:

`@SpringBootApplication(scanBasePackages = {"entertainment", "readinglist"})`
Reply

#5
I was having the same problem and to solve it I renamed my packages like this.

> "com.project"

there you can place your SpringBootAplication main class, then just create the others packages beginning with "com.project"

> "com.project.dao"
>
> "com.project.controller"

Creating this sub project structure you have no need to use scanBasePackages in @SpringBootApplication annotation, doing this your main class will be able to find every component in your project.

And in case you chose to use scanBasePackages remember that you need to set **all your components packages** like this.

> @SpringBootApplication(scanBasePackages = {"com.project.dao",
> "com.project.controller"})
Reply

#6
For the scanning of packages to really work, you must do as follows.

@SpringBootApplication(scanBasePackages = {"com.your.package.test.*.*"})

The first asterisk tells you to scan all packages within the main path (**com.your.package.test**) and the second asterisk tells you to scan all files in each package.

For example:

com.your.package.test
|_ config
|_ business
|_ controller
|_ domain
|_ repository
Reply

#7
The Spring Boot team wanted to make you life easy and by default all the packages located under the package where the annotation `@SpringBootApplication` is found are scanned. In your example it means under the package of the class `ReadingListApplication` and the package `readinglist` (and below). <br/><br/>
Following the example, as you did you can create a controller, a repository and a bean Book (based on the name we know it is from the domain).
<br/><br/>
Doing so there is some extra. You can define beans into the class `ReadingListApplication`, and these beans will be scanned. You can define Java configuration under the package `readinglist` and these beans will be scanned. <br/><br/>
Nothing to be configured (only `@SpringBootApplication` to be used).<br/><br/>
If you want to define a class outside the `readinglist` package then you need some configuration. <br/><br/>
From the IDE or from the Java doc, look what is inside the annotation `@SpringBootApplication` and you will find `scanBasePackages`.<br/><br/>
The parameter `scanBasePackages` does configure the packages to be scanned.
If you want to add some extra packages like you did into your example, you have to use this annotation. <br/>

@SpringBootApplication(scanBasePackages = {"readinglist","entertainment"})

Of course, you have to add the package "readinglist" back because well, you configure the scanning explicitely and add all the extra packages you want, and in your example, only one, the package `entertainment`.<br/><br/>
This way, both packages `readinglist` and `entertainment` (and of course below) will be scanned. You can for example, put some Java config into `entertainment` and these beans will be scanned.

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through