Expand my Community achievements bar.

SOLVED

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

Avatar

Level 9

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/IDXA...

 

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/o...

 

//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
    @inject
    private IDXAuthenticationWrapper idxAuthenticationWrapper1;
    
    @reference
    private IDXAuthenticationWrapper idxAuthenticationWrapper2;
    
    public main() {
        IDXAuthenticationWrapper idxAuthenticationWrapper3 =
            new IDXAuthenticationWrapper();

 

and all 3 variables return null.

 

Any ideas on how to get it work? thanks

1 Accepted Solution

Avatar

Correct answer by
Level 3

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.bl...

-Vikas Chaudhary

View solution in original post

2 Replies

Avatar

Community Advisor

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:

@component(immediate = true, service = MyServiceA.class)
public class MyServiceAImpl implements MyServiceA {

    //You can inject/reference other OSGI components
    @reference
    protected ResourceResolverFactory rrf;

ServiceB referencing ServiceA:

@component(immediate = true, service = MyServiceB.class)
public class MyServiceBImpl implements MyServiceB {

    //Injecting my custom service
    @reference
    privtae MyServiceA serviceA;


Please check these links to clarify your doubts:
https://experienceleague.adobe.com/docs/experience-manager-learn/cloud-service/developing/osgi-servi...

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

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

 

Hope this helps



Esteban Bustamante

Avatar

Correct answer by
Level 3

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.bl...

-Vikas Chaudhary