Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

@Reference in sightly

Avatar

Former Community Member

Hello All,

I wrote onc java class which extends to WCMUse,i am trying to use ResourceResolverFactory using @Reference but factory object is getting null even i made this as commponent @Component;

I know it is depricated.

 

Public class Test extends WCMUse{

@Reference

 ResourceResolverFactory factory;

void    activate(){

}

ResourceResolver getResourceResolver(){

 

 return factory.getAdminstrativResourceResolver(null);

}

 

 @Reference can wont work in sightly ?

I appreciate some one respond asap.

 

Thanks,

Nani.

4 Replies

Avatar

Level 10

You cannot use @Reference in a Sightly Java class. Best way is to use sling model with Sightly and use @osgiservice for injection. See:  https://sling.apache.org/documentation/bundles/models.html

Avatar

Level 8

You can also use the Sling Script Helper

private MyService myService; @Override public void activate() throws Exception { myService = this.getSlingScriptHelper().getService(MyService.class); }

Avatar

Community Advisor

Adding to original post for reference of developers who is using osgi service call in sightly

a) you need to add below dependency in maven bundle/pom.xml for WCMUse class

<dependency>

                <groupId>com.adobe.aem</groupId>

                <artifactId>aem-api</artifactId>

                <version>6.0.0.1</version>

                <scope>provided</scope>

            </dependency>

b) Next  important one to be considered is need to mention the package of Bundle in bundle/pom.xml under plugin section as shown below else you will get class not found exception when you try to reference the bundle in WCMUse  extension class.

  <plugin>

                <groupId>org.apache.felix</groupId>

                <artifactId>maven-bundle-plugin</artifactId>

                <extensions>true</extensions>

                <configuration>

                    <instructions>

                        <Bundle-SymbolicName>com.aem.community.mybundle</Bundle-SymbolicName>

                        <Export-Package>

               <!-- Let's export our primary package below is important else you will get class not found exception for youre bundle -->

               com.aem.community,

               com.aem.community.impl

                <!-- By default, don't export any other packages -->

            </Export-Package>

                       

                    </instructions>

                </configuration>

            </plugin>