Skip to main content

Posts

Showing posts from May, 2018

Use Service objects along with your controllers in Spring boot to cleanup code and testing

If you have Spring controller that uses repository objects, it can become a bit messy to write tests with mocks that have to mock various repository object methods like save() etc. Instead, you can use Here is an example controller that uses repository objects: @RestController public class StateManagerController { public StateManagerController(XRepository aRepository, YRespository yRepo) {   } public void aMethod() { // use xRepo, yRepo : e.g., xRepo.save() , yRepo.delete() etc. } ... } To test methods of this controller that use the repository objects, you will likely have to mock each repository collaborator object. Then you can get into lots of mocking details that make your tests ugly. Instead, I find that using the following pattern greatly simplifies both your code and tests. It's very simple but works great. 1. In your controller, only inject/depend on @Service objects. Don't use repository objects directly 2. Test your con

Using actuator in Spring boot to analyze and monitor your application

Intro Spring boot offers a wonderful module called the actuator. It is somewhat similar to the tools offered by Rails or other frameworks to get a better view of your application. Basically when you enable it, you can browse to a url which by default is at /actuator (but can be configured), where you will find very interesting pieces of information about your Spring boot application. For example, if you built a MVC webapp where you wrote a number of @Controller or @RestController classes, then going to /actuator/mappings will list your endpoints along with which controller method serve which endpoint etc. But there is a whole lot more in actuator you will find useful. In this article, I will show you how to use it in your application, and also how to only enable it in your development profile. I would assume that you don't want soething like this enabled in your production deployment. We will use Spring profiles for this. Setup I am assuming you have a working Spri

Using custom conditional logic to enable/disable Spring components

If you have a Spring component and you don't want it to load, you can use Spring's predefined conditionals as much as possible. For example, @Component   @ConditionalOnNotWebApplication   public class SchedulerEntryPoint implements ApplicationRunner { ...  } This will not load your component when running in non web application mode. Such as you may want to start the application but without any of the web framework using SpringApplicationBuilder. But sometimes you want to use custom conditions. It's pretty easy to do so, just use something like this @Component @Conditional (SchedulerCheck. class ) public class SchedulerEntryPoint implements ApplicationRunner { public static class SchedulerCheck implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { return System. getProperty ( "scheduler" ) !=