How to find entry point of a Spring Boot application? - java

What is the entry point of a spring boot application?
While going through a Spring Boot application code, all that it says is there is a code
public static void main having - SpringApplication.run(Application.class, args)
Example - SpringBoot2RestServiceApplication.java .
But how to get to know what is the entry point, just by going through the code. Earlier, if we go through applicationContext.xml - example - applicationContext.xml, we could understand the flow.
Is there any way, or maybe a standard to follow to make this understanding self-explanatory?
My question was more of understanding the flow of the application than finding the main class. One option could be separating configurations(#Configuration) to a separate class having multiple #Bean annotations, this would help in finding all bean wirings at one place. Is there a standard that large projects use to make code flow understandable?

The easiest thing to do would probably be to search for #SpringBootApplication in your code.
But, a guaranteed way to get this information would be to build the Spring Boot JAR. From there, you can open the resulting JAR and find the main class defined in the manifest, under META-INF/MANIFEST.MF. You'll see it under the Start-Class attribute:
Start-Class: com.example.foo.Application

I think the OP is studying an existing Spring Boot application, and is asking how to locate any runner, such as Application Runners, Command Line Runners, MVC controllers, Rest controllers, etc.
I don't know if there is an easy way to locate those, unless they are grouped together in the original design.
It's a difficult problem to do programmatically, because threads can be launched outside of Spring, for example in a constructor or a #PostConstruct.
It would be nice though if there were IDE support to easily locate anything that gets launched by Spring Boot

search #SpringBootApplication annotation in your project, the class with #SpringBootApplication annotation will automatically do the component-scan for the sub packages.
if no #SpringBootApplication annotation found, search the class extending "SpringBootServletInitializer" which is also a starting point for the spring boot application

The Entry of any spring boot application has an annotation of #SpringBootApplication

Related

Internal Implementation of IOC and DI in Spring Boot

Whenever spring boot application runs it sees the #SpringBootApplication annotation and runs the #ComponentScan which scans the classes with the annotations such as #Component etc and makes the object in the container.I have a little bit of idea that it might be using reflections internally to create the objects but I am not able to connect all the dots .I want to know what exact information does #AutoConfiguration gives to the container that it is able to get all the information of all the component annotated clases of any component ?
P.s. I have edited the wrong question
First of all #AutoConfiguration is responsible for setting up the default configurations for a Spring boot application depending on the dependencies we have added in the pom.xml.
The thing you are looking for is #ComponentScan which is what performs the component class scanning within the default package and all of its sub packages. It does this by going through each class in the package and looking for the #Component,#Service or #Repository annotations. If any of them is present then the container adds this as a bean.
#SpringBootApplication internally has the #ComponentScan annotation added to it along with two others.

DataJpaTest looking for HttpServletRequest

I have a question about the usage of DataJpaTest annotation. I am trying to test a Jpa repository, exactly as shown in the documentation.
I am getting an error that the HttpServletRequest cannot be resolved. It is because a different bean of mine is using it.
Why is the test trying to use the irrelevant bean? I would expect a DataJpaTest to only load Jpa related beans, repositories, etc. It seems it is trying to load all beans, which of course have their own dependencies.
What is the correct way to write a DataJpaTest so that I only focus on my Jpa repositories?
This is on Java 8, Spring Boot 2 and junit 5.
Update 1: thanks for the comments guys. My test class is literally based on the documentation.
My Spring Boot application class is like this:
#SpringBootApplication
#EnableSwagger2
#EnableCorsFilter
#ComponentScan(basePackages = {"com.acme.superapp"})
#SuppressWarnings("HideUtilityClassConstructor")
public class Swagger2SpringBoot {
public static void main(String[] args) {
new SpringApplication(Swagger2SpringBoot.class).run(args);
}
}
This actually helped because after I removed the "ComponentScan" annotation I get a different error, related to Swagger.
So it seems these annotations are affecting my test.
Thanks for your comments guys. The comment from M. Deinum and shinjw showed me to the correct path. I needed to slim down the entrypoint so that it does not have anything extra.
According to the documentation :
If you structure your code in a sensible way, your
#SpringBootApplication class is used by default as the configuration
of your tests.
It then becomes important not to litter the application’s main class
with configuration settings that are specific to a particular area of
its functionality.
Therefore I moved the ComponentScan and EnableSwagger2 annotations elsewhere, in different Configuration classes and this did the trick.
Thanks for your help!

How to restrict autos scanning of src/test/java in Spring Boot?

I have created a simple web app by following the book "Mastering Spring MVC". Everything was working fine, however, during the testing chapter, I have created two beans with #Primary annotation. 1. ProviderSignInController and 2. An Impl of my search service class. Both of these are in package src/test/java.
The problem here is that if I deploy my application, even then, these two beans come into the picture and I am not able to work with my actual authentication and search service.
I am not getting any error or exception. I would like to know what could be the best way to automatically inject my mocks/stubs while testing and actual implementations when I deploy the app in my dev environment.
The source code link is here. Thanks.
Instead of #Primary, I'd suggest using #Profile("PRODUCTION") along side #Bean for your real/production beans/classes & #Profile("!PRODUCTION") with your test beans/classes. Then, specify the active profile(s) at runtime
-Dspring.profiles.active=PRODUCTION, ...
The problem was the way I was executing the application.
When I launched the application directly from eclipse (Run As Spring Boot App), the tests were included in the build as they were present in the classpath.
I modified my approach and now I am using gradle build (gradle bootRun) to launch my app. This solves the problem. Thanks to #DwB for providing the hint.

Spring - separate directory for controllers

I'm a newbie in Spring and have one question about inserting all controllers in another directory than an executable spring boot file.
This tutorial
I have to have GreetingController.java and Application.java in one directory because if not, then I can't run mvn spring:boot-run
Is there a way to have Application.java in /java directory and all controllers in /java/controllers? Can I create some config file to inform Application.java where all the files are?
Thank you
I am little bit confused. But first point - It is good to have all controllers in separate directory, it is best practise, but better is using /rest directory.
Second point - You have to define application context and enable few things (for example via annotation). Try to look here http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html, here you can see annotations and basic structure.
For you, set this annotations to your Application.java:
#Configuration
#EnableAutoConfiguration
#ComponentScan
More information and where to write it you can find in link what I posted.
If you configure application context and you set EnableAutoconfiguration and ComponentScan, Spring will look up for all annotated components and your controllers (if you annotated you controller class by #Controller annotation.

Java based dependency injection in Spring

I'm working in a webapp and this is the first time that I'm using Java based configuration. I have a bunch of class to configure all:
ApplicationContext
PersistenceContext
SecurityContext
WebAppInitializer
WebMvcContext
Now I'm defining Spring Data repositories and the service layer so I need to inject the repositories there. Normally I would use Autowired but I've read that it is preferable to define the injections manually so the question is, where?
Maybe neither of the previous configuration classes is suitable for such task but, do I have to create a single class to define all the injections or is better to have on for each function? What happens if the project grows too much?
I think that the main question would be what is best way to organize dependencies in a Spring project. What do you do?
I add here an image of the structure of the project as a petition. I'm trying to decouple layers and now I need to inject UserRepository to UserService.
No, I would not define a single class to do all the injections. All your classes are coupled that way.
I don't understand what "define the injections manually" means. You have to specify them in either XML or annotations. There's no other way that I know of.
You don't say if you're using XML or annotation configuration. I find myself using the latter more of the time, with only enough XML configuration to tell the Spring app context to scan for annotations.
The Spring idiom would have you specify your configuration in layers if you're using XML. It's a moot point for annotations, because they go into your source code.
Your application will read the Spring context on start up, instantiate all the beans, and wire together the necessary dependencies. You're good to go from then on.
I disagree with the link you provided. Avoid autowiring? No.
The article said that he recommends using XML configuration for large projects. This is a very small project at this point. It seems to me that auto wiring with annotations would be fine even by the article's author's words.

Categories

Resources