Java: Sample Okta application is using Spring+Autowired to initialize variable - How to use/initialize Sling/Osgi instead? | Community
Skip to main content
jayv25585659
Level 8
January 11, 2024
Solved

Java: Sample Okta application is using Spring+Autowired to initialize variable - How to use/initialize Sling/Osgi instead?

  • January 11, 2024
  • 2 replies
  • 698 views

This is the class I'm trying to use => https://github.com/okta/okta-idx-java/blob/master/api/src/main/java/com/okta/idx/sdk/api/client/IDXAuthenticationWrapper.java

 

This is how I've imported the SDK into my maven project

 

<dependency> <groupId>com.okta.idx.sdk</groupId> <artifactId>okta-idx-java-api</artifactId> <version>3.0.7</version> </dependency>

 

 

This is the sample Sprint application => https://github.com/okta/okta-idx-java/blob/master/samples/embedded-auth-with-sdk/src/main/java/com/okta/spring/example/controllers/LoginController.java

 

//from LoginController.java @Autowired private IDXAuthenticationWrapper idxAuthenticationWrapper;

 

 

I'm trying to do the same but on my Sling+OSGI application. I've tried

 

import com.okta.idx.sdk.api.client.IDXAuthenticationWrapper; import javax.inject.Inject; import org.osgi.service.component.annotations.Reference; public class MyClass @586265 private IDXAuthenticationWrapper idxAuthenticationWrapper1; @3214626 private IDXAuthenticationWrapper idxAuthenticationWrapper2; public main() { IDXAuthenticationWrapper idxAuthenticationWrapper3 = new IDXAuthenticationWrapper();

 

and all 3 variables return null.

 

Any ideas on how to get it work? thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by VikasChaudhary_

Hi @jayv25585659 ,

 

As I can see you have imported the SDK into your project, but this dependency is not available under AEM and the same is not an OSGi bundle as well. You have to ensure that this dependency is fulfilled by the container as well, in case of AEM, we should have the OSGi bundle for the 3rd party library available in Felix. Can you check your bundle state in the Felix console? Are all the bundles resolved there. If not, please follow the following article which explains how to embed a 3rd party library in our project: Embedding Third party dependency/OSGi bundle in AEM application hosted in AEMasCS (myaemlearnings.blogspot.com)

-Vikas Chaudhary

2 replies

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
January 11, 2024

The Dependency Injection works in the OSGI context, so you must create OSGI Components (services types) which then could be injected into other services. 
Something like this:

Service A:

@8220494(immediate = true, service = MyServiceA.class) public class MyServiceAImpl implements MyServiceA { //You can inject/reference other OSGI components @3214626 protected ResourceResolverFactory rrf;

ServiceB referencing ServiceA:

@8220494(immediate = true, service = MyServiceB.class) public class MyServiceBImpl implements MyServiceB { //Injecting my custom service @3214626 privtae MyServiceA serviceA;


Please check these links to clarify your doubts:
https://experienceleague.adobe.com/docs/experience-manager-learn/cloud-service/developing/osgi-services/basics.html?lang=en

https://redquark.org/aem/day-08-osgi-components-services/ 

https://medium.com/@toimrank/aem-osgi-services-81796524bb64 

 

Hope this helps

Esteban Bustamante
VikasChaudhary_
VikasChaudhary_Accepted solution
Level 3
January 11, 2024

Hi @jayv25585659 ,

 

As I can see you have imported the SDK into your project, but this dependency is not available under AEM and the same is not an OSGi bundle as well. You have to ensure that this dependency is fulfilled by the container as well, in case of AEM, we should have the OSGi bundle for the 3rd party library available in Felix. Can you check your bundle state in the Felix console? Are all the bundles resolved there. If not, please follow the following article which explains how to embed a 3rd party library in our project: Embedding Third party dependency/OSGi bundle in AEM application hosted in AEMasCS (myaemlearnings.blogspot.com)

-Vikas Chaudhary