<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>Can’t use Spring component scan inside an OSGi module</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=120849212" />
  <subtitle>Can’t use Spring component scan inside an OSGi module</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=120849212</id>
  <updated>2026-04-07T07:03:38Z</updated>
  <dc:date>2026-04-07T07:03:38Z</dc:date>
  <entry>
    <title>RE: Can’t use Spring component scan inside an OSGi module</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121237858" />
    <author>
      <name>Antonio Javier Pérez</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121237858</id>
    <updated>2021-11-08T18:14:10Z</updated>
    <published>2021-11-08T17:35:01Z</published>
    <summary type="html">&lt;p&gt;We didn't find a good solution and turn to use 'classic' Spring XML
  application context.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Greetings.&lt;/p&gt;</summary>
    <dc:creator>Antonio Javier Pérez</dc:creator>
    <dc:date>2021-11-08T17:35:01Z</dc:date>
  </entry>
  <entry>
    <title>RE: Can’t use Spring component scan inside an OSGi module</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121237908" />
    <author>
      <name>Prathibha hallur</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121237908</id>
    <updated>2021-11-08T17:05:30Z</updated>
    <published>2021-11-08T17:05:30Z</published>
    <summary type="html">&lt;p&gt;Hello Antonio,&lt;/p&gt;
&lt;p&gt;Did you find a solution for this?  I want to add a spring project as
  client library in Liferay 7.3 and invoke a method but my portlet is
  erroring out.&lt;/p&gt;
&lt;p&gt;Please let me know if you had work around and you got this issue fixed?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Prathi&lt;/p&gt;</summary>
    <dc:creator>Prathibha hallur</dc:creator>
    <dc:date>2021-11-08T17:05:30Z</dc:date>
  </entry>
  <entry>
    <title>Can’t use Spring component scan inside an OSGi module</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120849211" />
    <author>
      <name>Antonio Javier Ortega Pérez</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120849211</id>
    <updated>2021-08-17T16:16:17Z</updated>
    <published>2021-06-25T08:32:13Z</published>
    <summary type="html">&lt;p style="margin-bottom: 11.0px;"&gt;
  &lt;span style="font-size: 11.0pt;"&gt;
    &lt;span style="line-height: 107.0%;"&gt;
      &lt;span style="font-family: Calibri , sans-serif;"&gt;Hi. We are trying
        to use an existing backend library inside various Liferay
        modules. This backend uses Spring and Hibernate to access
        database. The Spring context is loaded programmatically by using
        AnnotationConfigApplicationContext, configuration is made
        through @Component annotations and these configurations are
        found using AnnotationConfigApplicationContext.scan method.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 11.0px;"&gt;
  &lt;span style="font-size: 11.0pt;"&gt;
    &lt;span style="line-height: 107.0%;"&gt;
      &lt;span style="font-family: Calibri , sans-serif;"&gt;This library
        backend works fine in Liferay 6 and in a sample standalone app,
        but not in a Liferay 7 module. The core problem is that Spring’s
        AnnotationConfigApplicationContext.scan method can’t load
        annotated classes when readed inside an OSGi module.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 11.0px;"&gt;
  &lt;span style="font-size: 11.0pt;"&gt;
    &lt;span style="line-height: 107.0%;"&gt;
      &lt;span style="font-family: Calibri , sans-serif;"&gt;Inside the
        AnnotationConfigApplicationContext.scan method the
        scanCandidateComponents method is called, and inside this, it
        uses the getResources method of the ResourcePatternResolver
        interface, implemented by PathMatchingResourcePatternResolver.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 11.0px;"&gt;
  &lt;span style="font-size: 11.0pt;"&gt;
    &lt;span style="line-height: 107.0%;"&gt;
      &lt;span style="font-family: Calibri , sans-serif;"&gt;Inside the
        getResources method of PathMatchingResourcePatternResolver
        class, classpath resources are resolved into an URL and then
        added into a Set&amp;lt;Resource&amp;gt;. When outside OSGi this
        intermediate URL has the following format ‘file:/…’ meanwhile
        when in OSGi environment ‘bundleresource://’&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 11.0px;"&gt;
  &lt;span style="font-size: 11.0pt;"&gt;
    &lt;span style="line-height: 107.0%;"&gt;
      &lt;span style="font-family: Calibri , sans-serif;"&gt;It seems that
        Spring can’t resolve bundle resources out of the box but
        PathMatchingResourcePatternResolver seems to have an optional
        component called org.eclipse.core.runtime.FileLocator wich can
        resolve budle resources, but, when trying to add this OSGi
        component to the bundle we have errors that it’s incompatible
        with the current environment.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 11.0px;"&gt;
  &lt;span style="font-size: 11.0pt;"&gt;
    &lt;span style="line-height: 107.0%;"&gt;
      &lt;span style="font-family: Calibri , sans-serif;"&gt;We are using
        Liferay 7.3 and Spring 5.2.5. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 11.0px;"&gt;
  &lt;span style="font-size: 11.0pt;"&gt;
    &lt;span style="line-height: 107.0%;"&gt;
      &lt;span style="font-family: Calibri , sans-serif;"&gt;¿How can we use
        Spring AnnotationConfigApplicationContext.scan inside an OSGi bundle?&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: 11.0px;"&gt;
  &lt;span style="font-size: 11.0pt;"&gt;
    &lt;span style="line-height: 107.0%;"&gt;
      &lt;span style="font-family: Calibri , sans-serif;"&gt;Thanks.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;</summary>
    <dc:creator>Antonio Javier Ortega Pérez</dc:creator>
    <dc:date>2021-06-25T08:32:13Z</dc:date>
  </entry>
</feed>
