Message Boards

Can’t use Spring component scan inside an OSGi module

Antonio Javier Ortega Pérez, modified 2 Years ago.

Can’t use Spring component scan inside an OSGi module

New Member Posts: 22 Join Date: 4/20/11 Recent Posts

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.

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.

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.

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://’

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.

We are using Liferay 7.3 and Spring 5.2.5.

¿How can we use Spring AnnotationConfigApplicationContext.scan inside an OSGi bundle?

Thanks.

Prathibha hallur, modified 2 Years ago.

RE: Can’t use Spring component scan inside an OSGi module

New Member Posts: 7 Join Date: 10/21/21 Recent Posts

Hello Antonio,

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.

Please let me know if you had work around and you got this issue fixed?

 

Regards,

Prathi

Antonio Javier Pérez, modified 2 Years ago.

RE: Can’t use Spring component scan inside an OSGi module

New Member Post: 1 Join Date: 1/13/20 Recent Posts

We didn't find a good solution and turn to use 'classic' Spring XML application context.

 

Greetings.