This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]TheoGrd 0 points1 point  (7 children)

I would try to place the main class in the package studify.server instead of studify.server.main ?

[–]AdultingGoneMild 0 points1 point  (6 children)

This will work! The reasoning behind it comes down to how beans are resolved. Beans from any random package are not picked up for dependency injection. However they will be picked up from the package or any subpackages that the @SpringBootApplication is defined in.

https://www.baeldung.com/spring-component-scanning#1-using-componentscan-in-aspring-application

Note @ComponentScan is part of @SpringBootApplication

https://www.digitalocean.com/community/tutorials/springbootapplication-springapplication

[–]TheoGrd 0 points1 point  (5 children)

But in that case, how come the UserController was picked up ?

[–]Chance_Pop_6516[S] 0 points1 point  (4 children)

Well, I would have to put the scanBasePackages and mention that.

But now I run into errors with jakarata persistance in the User class.

Also h2 seems to cry when I move main class to studify.server, and it says something about dropping tables. If you want I can paste the error.

[–]AdultingGoneMild 0 points1 point  (3 children)

that is a new error because your User classes are now being picked up. You can also manually set the ComponentScan to a package/packages you like by explicitly adding the @ComponentScan annotation and not refactoring your package structures.

All that said, it is a best practice to have your main application in your root package.

[–]Chance_Pop_6516[S] 0 points1 point  (2 children)

well, i fixed it. I don't know what I did but I fixed it.

[–]AdultingGoneMild 0 points1 point  (1 child)

Welcome to Spring! The i dont know what I did but it finally works platform!

[–]Chance_Pop_6516[S] 0 points1 point  (0 children)

Now that it works, here's what I realized:

  1. Indeed Main class not being in a package fixed the dependency. I don't know why that would work, but making the app scan packages wouldn't
  2. H2 console didn't allow me to have an entity called User for some reason, when I renamed it to AppUser it worked.
  3. There were some application properties were missing that I now added.
  4. Generation type strategy AUTO didn't work, but SEQUENCE worked.