Spring Context Hierarchy
Spring's context hierarchy provides a way to organize and structure multiple application contexts within a parent-child relationship. This is useful for separating concerns and managing configurations in a modular way. This overview covers the key concepts and usage of context hierarchy in Spring, including creating parent-child contexts and sharing beans between contexts.
Key Concepts of Spring Context Hierarchy
- ApplicationContext: The central interface to provide configuration for an application.
- Parent Context: A context that provides common beans for one or more child contexts.
- Child Context: A context that inherits beans from its parent context but can also define its own beans.
- Bean Inheritance: Mechanism allowing child contexts to access beans defined in parent contexts.
Creating Parent and Child Contexts
To create a parent-child context hierarchy, you need to configure multiple contexts and establish the parent-child relationship. Here is an example:
ParentConfig.java
// ParentConfig.java
package com.example.springcontexthierarchy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ParentConfig {
@Bean
public ParentService parentService() {
return new ParentService();
}
}
ChildConfig.java
// ChildConfig.java
package com.example.springcontexthierarchy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ChildConfig {
@Bean
public ChildService childService() {
return new ChildService();
}
}
ParentService.java
// ParentService.java
package com.example.springcontexthierarchy;
public class ParentService {
public void doSomething() {
System.out.println("Parent service doing something.");
}
}
ChildService.java
// ChildService.java
package com.example.springcontexthierarchy;
import org.springframework.beans.factory.annotation.Autowired;
public class ChildService {
@Autowired
private ParentService parentService;
public void doSomethingElse() {
parentService.doSomething();
System.out.println("Child service doing something else.");
}
}
Main Application Class
To set up the context hierarchy, create an application context for the parent and then create a child context with the parent context set. Here is an example:
Main Application Class
// SpringContextHierarchyApplication.java
package com.example.springcontexthierarchy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringContextHierarchyApplication {
public static void main(String[] args) {
ApplicationContext parentContext = new AnnotationConfigApplicationContext(ParentConfig.class);
AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext();
childContext.setParent(parentContext);
childContext.register(ChildConfig.class);
childContext.refresh();
ChildService childService = childContext.getBean(ChildService.class);
childService.doSomethingElse();
}
}
Sharing Beans Between Parent and Child Contexts
In a context hierarchy, child contexts can access beans defined in parent contexts. Here is an example demonstrating this:
ChildService.java
// ChildService.java
package com.example.springcontexthierarchy;
import org.springframework.beans.factory.annotation.Autowired;
public class ChildService {
@Autowired
private ParentService parentService;
public void doSomethingElse() {
parentService.doSomething();
System.out.println("Child service doing something else.");
}
}
Using Profiles with Context Hierarchy
Profiles can be used in combination with context hierarchy to provide different configurations for parent and child contexts. Here is an example:
ParentConfig.java
// ParentConfig.java
package com.example.springcontexthierarchy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("parent")
public class ParentConfig {
@Bean
public ParentService parentService() {
return new ParentService();
}
}
ChildConfig.java
// ChildConfig.java
package com.example.springcontexthierarchy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("child")
public class ChildConfig {
@Bean
public ChildService childService() {
return new ChildService();
}
}
Key Points
- ApplicationContext: The central interface to provide configuration for an application.
- Parent Context: A context that provides common beans for one or more child contexts.
- Child Context: A context that inherits beans from its parent context but can also define its own beans.
- Bean Inheritance: Mechanism allowing child contexts to access beans defined in parent contexts.
- Create parent and child contexts and establish the parent-child relationship.
- Use child contexts to access beans defined in parent contexts.
- Use profiles to provide different configurations for parent and child contexts.
Conclusion
Spring's context hierarchy provides a powerful way to organize and structure multiple application contexts in a parent-child relationship. By leveraging this hierarchy, developers can manage configurations in a modular and flexible way. Understanding and using context hierarchy effectively enhances the scalability and maintainability of Spring applications. Happy coding!