Integration created

How to scan Spring components in OSGi


Spring cannot scan components in OSGi environment. This problem come from the fact that Spring uses classpath scanning to find components. In OSGi environment, classes are loaded from bundles. So, Spring cannot find them.


To solve this problem we cannot use @ComponentScan annotation. Instead, we need to use @Import annotation to import all the components we need.


It’s not a good solution, but it works. However, it is not a good practice to import all the components manually. So, we need to find a better solution.

We need to:

  • find a way to scan components in OSGi environment
  • add found components to the Spring context


Value of @Import annotation can be injected with helper method changeAnnotationValue:

    public static void changeAnnotationValue(Annotation annotation, String attributeName, Object newValue) {
      try {
          Field f = annotation.getClass().getDeclaredField("memberValues");
          Map<String, Object> memberValues = (Map<String, Object>) f.get(annotation);
          memberValues.put(attributeName, newValue);
      } catch (Exception e) {
          throw new RuntimeException(e);

Not forget to add @Import annotation to the configuration class with empty value:

    public class PortalConfig {


Hope that this solution will help you to solve the same problem when integrating Spring Framework with OSGi environment!