Enabling Non-Spring Libraries to Access Spring Boot Components: Difference between revisions
Line 28: | Line 28: | ||
Note that the dependency project should be configured with "compile only" access to Spring Framework API packages "org.springframework:spring-beans" and "org.springframework:spring-context". A way to do this that does not involve Spring Boot dependency management is described here: [[Gradle_Spring_dependency-management_Plugin#Overview|Spring dependency-management Plugin for Gradle]]. | Note that the dependency project should be configured with "compile only" access to Spring Framework API packages "org.springframework:spring-beans" and "org.springframework:spring-context". A way to do this that does not involve Spring Boot dependency management is described here: [[Gradle_Spring_dependency-management_Plugin#Overview|Spring dependency-management Plugin for Gradle]]. | ||
The Spring Boot runtime is supposed to configured SpringApplicationContextAccess for the dependency, via a SpringApplicationContextConfiguratorForDependencies component., early in its life cycle. The simplest way to do this is when the component is initialized so it has access to the application context: | |||
<syntaxhighlight lang='java'> | |||
@Component | |||
public class SpringApplicationContextConfiguratorForDependencies { | |||
@Autowired | |||
public SpringApplicationContextConfiguratorForDependencies(ApplicationContext applicationContext) { | |||
SpringApplicationContextAccess.APPLICATION_CONTEXT = applicationContext; | |||
} | |||
} | |||
</syntaxhighlight> |
Revision as of 17:57, 2 November 2018
Internal
Overview
This article describes a possible approach to give non-Spring libraries access to Spring Boot runtime components.
Playground Example
Approach
The Spring Boot runtime should implement a Spring component whose job is to configure a dedicated class in the dependency package. This solution assumes that the dependency package "cooperates" and we can write code in it. The dedicated class in the dependency package is conventionally named SpringApplicationContextAccess:
package playground.springboot.dependency;
import org.springframework.context.ApplicationContext;
public class SpringApplicationContextAccess {
public static ApplicationContext APPLICATION_CONTEXT;
}
Note that the dependency project should be configured with "compile only" access to Spring Framework API packages "org.springframework:spring-beans" and "org.springframework:spring-context". A way to do this that does not involve Spring Boot dependency management is described here: Spring dependency-management Plugin for Gradle.
The Spring Boot runtime is supposed to configured SpringApplicationContextAccess for the dependency, via a SpringApplicationContextConfiguratorForDependencies component., early in its life cycle. The simplest way to do this is when the component is initialized so it has access to the application context:
@Component
public class SpringApplicationContextConfiguratorForDependencies {
@Autowired
public SpringApplicationContextConfiguratorForDependencies(ApplicationContext applicationContext) {
SpringApplicationContextAccess.APPLICATION_CONTEXT = applicationContext;
}
}