Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Can’t use Spring component scan inside an OSGi module
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.
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
We didn't find a good solution and turn to use 'classic' Spring XML application context.
Greetings.
Powered by Liferay™