0

This sounds like a generic question, but I'm trying to be very specific here.

I have a SpringBoot app. I have a need to be able to run it normally most of the time, but during the build process I want to be able to run it in a "verification mode", which will do some analysis to ensure that it's likely to start up successfully. There are mistakes developers in our teams can make (and often make) that can't easily be detected on their desktops, but which can be detected in a build process, and are definitely detected when the app is deployed (which is later than I'd like).

So I built a prototype such that in the Application class (subclass of SpringBootServletInitializer), I look at the args list, and if it's not empty I assume we're in "verification mode", so I try to call the class that does the analysis, and will then exit after that.

It doesn't really matter what this analysis class does, but it does need to access Spring framework components, and application components. For instance, it uses a ClassPathScanningCandidateComponentProvider, and looks at all components in a specific base package.

For what it's doing, this class is working fine. However, what I don't like about it is that it's not easily testable, because I have to manually create the CPSCCP, even though I'd prefer to Autowire it. I'd also like for the class itself to have a Spring Component annotation, so I don't have to create it, and it would be autowired into the Application class.

The problem here is that I can't very well Autowire into the Application class, as it's not a Spring component.

What I would think would be possible would be for me to make an API call that would obtain the Spring component with the autowired CPSCCP. However, I can't figure out how to do that. I initially thought that the solution provided here would work, but it appears that component scanning and bean wiring hasn't happened by this point.

The best I can come up with is for my class to be a static utility class, and to manually instantiate the CPSCCP, which makes it awkward to test.

David M. Karr
  • 14,317
  • 20
  • 94
  • 199
  • Instead of the modes, have you considered using [Spring Profiles](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-profiles)? – Boris Oct 19 '18 at 17:08
  • Why don't you write an integration test that would startup your app and exercise it's functionality (or do some verification) through a Rest API that's only available in the test profile? – Mustafa Oct 19 '18 at 17:16

0 Answers0