In this article, will explore how to use sling context aware configurations.
Step 1: Create a Context aware configuration. Create an interface with @Configuration annotation. The @Configuration annotation is mandatory. All properties on the @Configuration annotation and the @Property annotations are optional – they provide additional metadata for tooling e.g. configuration editors.
import org.apache.sling.caconfig.annotation.Configuration;
import org.apache.sling.caconfig.annotation.Property;
@Configuration(label="CA Configuration", description="CA Description")
public @interface MyCAConfig {
@Property(label="site name", description="Describe Site Name")
String siteName();
@Property(label="content type", description="Describe Content Type")
String contentType() default "general";
}
To call/refer this configuration in backend logic, get the Resource object from the page path. Then, adapt the resource object to ConfigurationBuilder. Refer the below code snippet to get the sling context aware configuration details.
Resource resource = resourceResolver.getResource(currentPagePath);
if(resource !=null) {
ConfigurationBuilder configBuilder = resource.adaptTo(ConfigurationBuilder.class);
if(configBuilder !=null) {
MyCAConfig caConfig = configBuilder.as(MyCAConfig.class);
logger.debug(" Content Type: {}", caConfig.contentType());
logger.debug(" Site Name: {}", caConfig.siteName());
}
}
Step 2: Modification under /conf folder
Here, create a node of type: sling:Folder under /conf/chetasmind
Name: sling:configs

Here, create a node of type: nt:unstructured
Name: com.chetasmind.tutor.core.config.MyCAConfig

Add the properties contentType, siteName as shown below

Now, under /conf/chetasmind I have created a node of type sling:Folder name: us Here, under us again I have created a node of type sling:Folder named en. Then under here, one more node named: science. Under science folder I have created a node of type sling:Folder Name is sling:configs Here create a node of type: nt:unstructured Name: com.chetasmind.tutor.core.config.MyCAConfig Add the properties contentType, siteName as shown below

Step 3: Adding the property sling:configRef in content node.
Go to the content node: /content/chetasmind/jcr:content Add the property sling:configRef value: /conf/chetasmind
In this case, if you try to get the Context Aware Configuration value under this content hierarchy, then you will get the values: contentType as general, siteName as base. Since, this is present under: /conf/chetasmind/sling:configs/com.chetasmind.tutor.core.config.MyCAConfig

Now, go to the path: /content/chetasmind/us/en/home/science/jcr:content add the property sling:configRef value as: /conf/chetasmind/us/en/science Now for all the sub nodes present under science, different context aware configurations will be applied.

For testing purpose, I have added the Hello World Component in /content/chetasmind/us/en.html page. Here is the logger statement

Now, I have added the Hello World Component in /content/chetasmind/us/en/home/science/article1.html page. Here is the logger statement
