Can’t use Spring component scan inside an OSGi moduleCan’t use Spring component scan inside an OSGi modulehttps://liferay.dev/en/c/message_boards/find_thread?p_l_id=119785333&threadId=1208492122024-03-29T14:36:31Z2024-03-29T14:36:31ZRE: Can’t use Spring component scan inside an OSGi moduleAntonio Javier Pérezhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1212378582021-11-08T18:14:10Z2021-11-08T17:35:01Z<p>We didn't find a good solution and turn to use 'classic' Spring XML
application context.</p>
<p> </p>
<p>Greetings.</p>Antonio Javier Pérez2021-11-08T17:35:01ZRE: Can’t use Spring component scan inside an OSGi modulePrathibha hallurhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1212379082021-11-08T17:05:30Z2021-11-08T17:05:30Z<p>Hello Antonio,</p>
<p>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.</p>
<p>Please let me know if you had work around and you got this issue fixed?</p>
<p> </p>
<p>Regards,</p>
<p>Prathi</p>Prathibha hallur2021-11-08T17:05:30ZCan’t use Spring component scan inside an OSGi moduleAntonio Javier Ortega Pérezhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1208492112021-08-17T16:16:17Z2021-06-25T08:32:13Z<p style="margin-bottom: 11.0px;">
<span style="font-size: 11.0pt;">
<span style="line-height: 107.0%;">
<span style="font-family: Calibri , sans-serif;">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.</span></span></span></p>
<p style="margin-bottom: 11.0px;">
<span style="font-size: 11.0pt;">
<span style="line-height: 107.0%;">
<span style="font-family: Calibri , sans-serif;">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.</span></span></span></p>
<p style="margin-bottom: 11.0px;">
<span style="font-size: 11.0pt;">
<span style="line-height: 107.0%;">
<span style="font-family: Calibri , sans-serif;">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.</span></span></span></p>
<p style="margin-bottom: 11.0px;">
<span style="font-size: 11.0pt;">
<span style="line-height: 107.0%;">
<span style="font-family: Calibri , sans-serif;">Inside the
getResources method of PathMatchingResourcePatternResolver
class, classpath resources are resolved into an URL and then
added into a Set<Resource>. When outside OSGi this
intermediate URL has the following format ‘file:/…’ meanwhile
when in OSGi environment ‘bundleresource://’</span></span></span></p>
<p style="margin-bottom: 11.0px;">
<span style="font-size: 11.0pt;">
<span style="line-height: 107.0%;">
<span style="font-family: Calibri , sans-serif;">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.</span></span></span></p>
<p style="margin-bottom: 11.0px;">
<span style="font-size: 11.0pt;">
<span style="line-height: 107.0%;">
<span style="font-family: Calibri , sans-serif;">We are using
Liferay 7.3 and Spring 5.2.5. </span></span></span></p>
<p style="margin-bottom: 11.0px;">
<span style="font-size: 11.0pt;">
<span style="line-height: 107.0%;">
<span style="font-family: Calibri , sans-serif;">¿How can we use
Spring AnnotationConfigApplicationContext.scan inside an OSGi bundle?</span></span></span></p>
<p style="margin-bottom: 11.0px;">
<span style="font-size: 11.0pt;">
<span style="line-height: 107.