Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Boot and Application Context Hierarchy

DZone's Guide to

Spring Boot and Application Context Hierarchy

Creating your own application context hierarchy can allow you, should you need to, provide different ports with different endpoints to give you some customization.

· Java Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

Spring Boot supports a simple way of specifying a Spring application context hierarchy

This post is simply demonstrating this feature — I have yet to find a good use of it in the projects I have worked on. Spring Cloud uses this feature for creating a bootstrap context where properties are loaded up, if required, from an external configuration server, which is made available to the main application context later on.

To quickly take a step back — a Spring Application Context manages the lifecycle of all the beans registered with it. Application Context hierarchies provide a way to reuse beans — beans defined in the parent context are accessible in the child contexts.

Consider a contrived use-case of using multiple application contexts and the application context hierarchy — this is to provide two different ports with a different set of endpoints at each of these ports. 

Child1 and Child2 are typical Spring Boot Applications, along these lines:

package child1;   
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.PropertySource; 
import root.RootBean;   

@SpringBootApplication 
@PropertySource("classpath:/child1.properties") 
public class ChildContext1 {       
    @Bean     
    public ChildBean1 childBean(RootBean rootBean, 
    @Value("${root.property}") String someProperty) 
    {                        
        return new ChildBean1(rootBean, someProperty);     
    } 
}


Each of the applications resides in its own root package to avoid collisions when scanning for beans. Note that the bean in the child contexts depends on a bean that is expected to come from the root context. 

The port to listen on is provided as properties, since the two contexts are expected to listen on different ports I have explicitly specified the property file to load with a content along these lines:


server.port=8080
spring.application.name=child1



Given this setup, Spring Boot provides a fluid interface to load up the root context and the two child contexts:

SpringApplicationBuilder appBuilder =
       new
SpringApplicationBuilder()
               .parent(RootContext.class)
               .child(ChildContext1.class)
               .sibling(ChildContext2.class);


ConfigurableApplicationContext applicationContext  = appBuilder.run();


The application context returned by the SpringBootApplicationBuilder appears to be the final one in the chain, defined via ChildContext2 above.

If the application is now started up, there would be a root context with two different child contexts, each exposing an endpoint via a different port. A visualization via the /beans actuator endpoint shows this:

Not everything is clean, though. There are errors displayed in the console related to exporting JMX endpoints, however, these are informational and don't appear to affect the start-up.

Samples are available in my GitHub repo.

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.

Topics:
spring architecture ,java ,spring boot ,tutorial ,application context hierarchy

Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}