Spring Boot Actuators – Customizing Actuator Endpoint

Spring Boot Actuators – Customizing Actuator Endpoint

SpringBoot – Actuators (Example here)
Spring Boot Actuator brings in several production grade services to your application. Spring Boot Actuators has various number of built-in endpoints.

Actuators helps us monitor and interact with your application. Spring Boot also lets us to create our own endpoints too. These end points can be both enabled or disabled.

We have already seen in-build actuator endpoints here – Spring Boot Actuators – in-built EndPoint.

Let us see how to customize the already existing endpoint. Let us override the /actuator/health endpoint.

To override the health endpoint we will be extending a class – AbstractHealthIndicator

AbstractHealthIndicator:
Class which has base health indicator implementations and encapsulates creation of health and error handling. This will override the method – protected abstract void doHealthCheck(Health.Builder builder) throws Exception.

Let us see an example,

pom.xml

application.properties

ControllerSample.java

App.java

Now without customizing endpoint, if we run the application

Now let us override the health endpoint,

Output after overriding the healthIndicator,


Download this example here

 

 

 

Read More

Spring Boot Actuators – How to use it in application

SpringBoot – Actuators
Spring Boot Actuator brings in several production grade services to your application. Spring Boot Actuators has various number of built-in endpoints.

Actuators helps us monitor and interact with your application. Spring Boot also lets us to create our own endpoints too. These end points can be both enabled or disabled.

Below are some of the Spring Boot Actuator endpoints,

EndPoint Description
/actuator/health Basic health information of the application
/actuator/configprops list of all @ConfigurationProperties
/actuator/info Arbitrary Information
/actuator/auditevents audit events of the application
/actuator/httptrace HTTP trace information
/actuator/metrics Metrics information of the application
/actuator/mappings @RequestMappings in the application
/actuator/scheduledtasks Scheduled tasks in the application
/actuator/shutdown Shutdowns the application

Let us write a sample application to test the spring boot actuator end points.

pom.xml

add this property in application.properties,

application.properties

SampleController.java

App.java

Let us start the application,

 

Once the application is started, we can see the actuators are getting initialized. when we hit localhost:8080/actuator/{endpoint} we can see the information

Here are some sample endpoint outputs,

/actuator/health

/actuator/mappings

/actuator/httptrace

Download Example here

Read More

@Scope – How to get Scope of Bean from Code

@Scope – How to get Scope of Bean from Code

When we create a Bean we are creating  actual instances of the class defined by that bean definition. We can also control the scope of the objects created from a particular bean definition.

There are 5 types of scopes in bean,

  • singleton (default scope)
  • prototype
  • request
  • session
  • global-session

Singleton:
Single instance per spring IoC container

Prototype:
Single bean definition to any number of object instances.

Request:
Bean definition for each request. Only valid web-aware Spring ApplicationContext.

Session:
Bean definition for a session. Only valid web-aware Spring ApplicationContext.

Global-Session:
Similar to session but the only makes sense in the context of portlet-based web applications. Only valid web-aware Spring ApplicationContext.

In this example, we are going to see how can we get the Scope of bean through java code. We are going to create two bean classes – Employee and Student

Employee is Singleton and Student is Prototype.

Now let us see how to get these scope of bean through Java code.

 

pom.xml

Employee.java

Student.java

ScopeService.java

ScopeController.java

App.java

application.properties

home.jsp

Output:

From output we can see,
@org.springframework.context.annotation.Scope(proxyMode=DEFAULT, value=singleton, scopeName=) 

scopeName and value are aliases, we cant mention both together.

If we try to mention both, we will get the exception

Example:
@Scope(value=”singleton”, scopeName=”ScopeName”)

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.javainfinite.App]; nested exception is org.springframework.core.annotation.AnnotationConfigurationException: In AnnotationAttributes for annotation [org.springframework.context.annotation.Scope] declared on class ‘com.javainfinite.model.Employee’, attribute ‘value’ and its alias ‘scopeName’ are declared with values of [singleton] and [ScopeName], but only one is permitted.
But we can use either one @Scope(value=”singleton”) or @Scope(scopeName=”singleton”)

Download code here

 

 

 

Read More

SpringBoot – Custom Banner / Disable Banner

SpringBoot – Custom Banner / Disable Banner

Whenever we deploy the springboot application, it comes with default banner

We have an option to change this banner or even to disable this banner.

In order to disable this banner, we have to include a property in application.properties file

spring.main.banner-mode=off

If we include this property in application.properties file, SpringBoot banner will be disabled.

 

We can also customize the springboot banner as per our requirement. All we have to do is, Put the design required in a file and save it as banner.txt and paste it in resources folder along with application.properties.

Lets paste this design in banner.txt file,

Paste the banner.txt with application.properties in resources folder

Let us see the output,

Read More

Springboot MVC – Passing Values from JSP to Controller

Springboot MVC – Passing Values from JSP to Controller

In this article let us see how to pass values from JSP page to Spring Controller. We are going to implement MVC with SpringBoot.

For understanding Springboot with MVC – Basic example – Please refer here

In this example, We are going to have a Employee Bean, Controller and a main class. We are going to have 2 JSP pages, home.jsp and welcome.jsp.

home.jsp page loads when the springboot application is deployed, we will enter the name there and the name will be passed to controller and from controller it will go to welcome.jsp and will display the name.

In order to use spring form in the JSP pages, we require to include this taglib

<%@ taglib uri=”http://www.springframework.org/tags/form” prefix=”form”%>

Here is the Project Structure,

 

pom.xml

Employee.java

SpringController.java

In the above code, We have declared model.addAttribute(“Employee”, employee); where employee is a bean and is autowired. So the bean is made available.

What will happen if we don’t declare Bean?
Well, if we dont make the bean available, we will be hitting an exception – BindingResult nor plain target object for bean name available as request attribute.

So when creating an mvc project, make sure bean is available.

home.jsp

welcome.jsp

App.java

Output:

 

 

 

Download code here

Read More