Posts

Showing posts from April, 2017

Executing Code on Startup of Spring Boot Application

Image
In this tutorial, we will find how to execute a block of code on application startup. Developers frequently need some startup code that needs to be executed once the application starts. This can be reading a set of configuration values from the database which will remain constant or execute some notification logic. Spring boot provides ApplicationRunner and CommandLineRunner interfaces to accomplish this task.  Spring boot also provides ApplicationArguments class which can be injected into any class that needs program argument values. Find this project on Github https://github.com/javacodenet/spring-boot-application-startup-code pom.xml Application.java MyCommandLineRunner.java MyApplicationRunner.java Program arguments added Output from MyCommandLineRunner Output from MyApplicationRunner ...

Injecting Properies in Spring boot

Image
In this tutorial, we will find how to bind configuration properties to Structured Object. Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. If these configurations are all related then we can bound these to java object using spring's @ConfigurationProperties annotation. Ways to make configuration object injected into application Annotate Application class with @EnableConfigurationProperties(value=MyProperties.class) . In this case, there is no need to mark your configuration object with @Component annotation. Or mark your configuration object with @Component annotation. This will make your class to be picked up by Spring's component scanner. Find this project on Github https://github.com/javacodenet/spring-boot-injecting-properties pom.xml Application.java ...

Spring Boot Profiles

Image
In this tutorial, we will deep dive into the features that spring profile going to offer Spring Profiles: Spring profiles help you to conditionally select various beans and configuration that should be included into the project. This will be very helpful if you work with multiple environments and configurations. Any @Component or @Configuration can be marked with @Profile. spring.profiles.active property is used to define your active profile. This can be mentioned either in the configuration file(application.properties or application.yml) or through program arguments. If an active profile is specified through program arguments it will replace the profile defined in configuration file. Sometimes it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally add active profiles. You can also programmatically set your profiles us...

Generate Random values in Spring boot

Image
In this tutorial we will look into the ways of generating a random string or integer values in Spring boot: Find this project on Github https://github.com/javacodenet/random-generator/ pom.xml Application.java RandomValues.java Bean with random values injected from the configuration file. application.properties A configuration file containing random value definitions. Output

Customize Spring Boot's auto configurations

Image
In this tutorial, we will customize the spring boot's auto configurations. This can be done in following ways: Through program arguments application.properties application.yml YAML is a serialization format that follows the hierarchical structure, which is in ways similar to JSON. It will contain key value pairs in a hierarchical format. And it is really popular in ruby. Spring dependencies have snake yaml jar which is responsible for parsing yaml. Spring boot gives developers choices, often there are multiple methods to achieve the same thing in spring boot. This allows developers to pick their favorite approach. Find this project on Github https://github.com/javacodenet/springBootApplicationPropertiesDemo pom.xml Application.java Through program arguments application.properties application.yml Output

Build Spring Boot application in just 5 minutes

Image
Spring Boot Spring Boot is Spring's convention-over-configuration solution for creating stand-alone, production-grade Spring-based Applications that you can "just run". You can use Spring Boot to create Java applications that can be started using java -jar or more traditional war deployments. Features: Create stand-alone Spring applications Embed Tomcat or Jetty directly (no need to deploy WAR files) Provide opinionated 'starter' POMs to simplify your Maven configuration Automatically configure Spring whenever possible Provide production-ready features such as metrics, health checks and externalized configuration Absolutely no code generation and no requirement for XML configuration In this example we will see that just using two files Application.java and pom.xml we can create a sprint boot application. The advantage of spring boot is that it takes care of all the configurations and you can focus on your application...