IntelliJ runs wrong Spring Boot class - spring-boot

I'm working on a Spring Boot project with IntelliJ IDEA (2021.3.1, community edition). I have two classes annotated with #SpringBootApplication. One's the main application, and the other is a utility for helping build up the database I'm using.
I've been trying to run the utility from IntelliJ with an Application configuration. I have the utility class specified in that configuration as the one to run. But IntelliJ runs the main application class instead.
I tried taking the #SpringBootApplication off my main class, and then the run configuration would run the utility. I put the annotation back on my main class, and the run configuration ran the utility and the main class.
I am absolutely stumped. Is "Application" the wrong kind of run configuration? Is there something else I need to be setting up to get it to run the right class?

Related

Spring Tools Suite stopped to exclude test classpath from RUN AS

I am using STS to develop a Spring Boot based application. I have multiple maven projects and a patent one to include them all.
I used to run/debug the application by calling RUN AS/DEBUG AS Spring Boot Application to the project that contains the Spring Boot main entry class.
Something changed and the STS loads to the application running classpath the test classes as well. That causes conflicts to spring bean initialization.
Is there any configuration in the workspace that causes that behaviour?
I also created a new workspace, but No luck :-(
I know it's a 6 month old question, but for those still having the issue:
You should open your "Run Configurations"
under "Classpath" tab
verify that "exclude test classes" is checked
It seems that new STS' Spring Boot App "run configurations" are created without this option (while it's checked by default for standard Eclipse's Java main class)

Debugging not working for Spring Boot 2.2 and IntelliJ Idea

By running the app in debugging mode (spring-boot:run) with IntelliJ Idea CE, no breakpoints work at all when using Spring Boot 2.2. Instead, when rolling back to Spring Boot 2.1.1, everything works fine. What am I doing wrong? Is there an option/flag to add for 2.2 to make the debugger work?
I would recommend running the app directly from the IDE rather than using mvn spring-boot:run. Doing so will give you the best experience in the IDE.
The release notes indicate that the Spring Boot Maven Plugin now forks the process by default which is the reason why you can't debug the application as you used to with 2.1. If you really have to run the app using the maven plugin, you can disable forking the process (e.g. using -Dspring-boot.run.fork=false)
Edit Configuration -> Application -> in Main Class: select your Main class (the one that contain your main method(public static void main) ) and run

How to selectively make one class to be the code that should be run in Spring (Spring Boot 2.x)

We have several classes annotated with the #Component notation in our Spring Boot 2.x project - but we would like to selectively pick only one of these class at runtime.
To further elaborate we created a Uber Jar - which will run on several machines - but each jar should be running a different logic and this logic is dictated by one of this class.
What is the cleanest way to achieve this in Spring boot 2.x? I read something about profiles etc. any cleaner solutions are very much appreciated.
Use the Spring Boot Maven plugin, and configure it to use the PropertiesLauncher, then set the loader.main via the command line. Or, you can specify the whole thing via the command line. Assuming you're using the Maven (or Gradle) Spring Boot plugin to build the jar/war file:
java -cp bootApp.jar -Dloader.main=org.your.package.DemoApplication org.springframework.boot.loader.PropertiesLauncher
You can group and move those #Component classes in multiple #Configuration classes. In this case you have to manually declare them as #Beans (methods). You will define as many #Configuration classes as your machines count.
Further on, you can enable or disable configuration classes by means of Spring Profiles. Profiles are enabled using spring.profiles.active system property (-Dspring.profiles.active=profile1,profile2).
If you want to have it clean then you have to do it in 'Dependency Inversion Principle' way: You should define interfaces for your #Components and each #Configuration will declare #Beans of a concrete type. In this way you can use #Autowired dependency injection w/o knowing the concrete implementation.

In intelliJ Run/Debug Configuration, how to skip spring boot tests

We have written tests using annotations like below,
#RunWith(SpringRunner.class)
#Slf4j
#SpringBootTest
in IntelliJ 2017 we run spring boot program via Run/Debug Configuration, and it automatically execute these tests. But some tests really takes a lot of time, is there a way to disable or skip these tests? Thanks a lot.
You can write a junit category of runners or right click on package name or test-class and choose Run "yourClassName", which will run only this class or all classes from selected package.

Prevent SpringBoot from creating an EmbeddedServletContainer

I'm trying to convert an existing spring application into a spring boot application. this application is not a web application but somewhere deep in it's maven dependencies hierarchy it includes a spring-web jar and therefore spring-boot tries to autoconfigure a WebEnvironment and in turn complains about not finding an EmbeddedServletContainerFactory. But this is intended as I'm using spring-boot-starter instead of spring-boot-starter-web.
My questing is: how can I prevent spring-boot from autodiscovering web specific items in the classpath? I already tried something like
#EnableAutoConfiguration(exclude = { EmbeddedServletContainerAutoConfiguration.class })
but it doesn't seem to work. Debugging the startup process I see that it runs into a method called deduceWebEnvironment() which is called unconditionally. This method returns true as soon as the following classes are on the classpath:
javax.servlet.Servlet, org.springframework.web.context.ConfigurableWebApplicationContext
But again, even this classes exist in the cp, I don't want to startup a web-application.
Try new SpringApplicationBuilder(YourApp.class).setWebEnvironment(false).run(args) You can also disable the web mode via configuration in application.properties
spring.main.web-environment=false
See the documentation

Resources