In AEM, can you call a service from a WcmUsePojo using annotations ? | Community
Skip to main content
Level 6
July 6, 2022
Solved

In AEM, can you call a service from a WcmUsePojo using annotations ?

  • July 6, 2022
  • 2 replies
  • 1809 views

hi folks,

Can you annotate a class extended from WcmUsePojo with @8220494

and call a service using @3214626?

 

I.e. using felix scr annotations.

 

I tried it and got  errors messages about ScriptEvaluationExceptions.

 

The service works fine with bundlecontext.

 

thanks

Fiona

 

 

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 SantoshSai

@fionas76543059 Usage of annotation where extending WCMUsePojo class not a right approach. Please refer below code snippet you may understand using WCMUsePojo 

Java

import javax.jcr.Node;

import org.demo.core.models.TextComponentModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.sightly.WCMUsePojo;

public class TextComponent extends WCMUsePojo {

private TextComponentModel model;

@Override
public void activate() throws Exception {
try {
log.info("Text Component 2.0 backend logic starts");
/**
* Initializing the model object to set the values
*/
model = new TextComponentModel();
/**
* Getting the current node from the resource object which is available in Use
* API
*/
Node node = getResource().adaptTo(Node.class);
/**
* Check if the node has title property
*/
if(node.hasProperty("title")) {
/**
* Reading the title property from the string
*/
String title = node.getProperty("title").getString();

/**
* Setting the value entered by the user in the model object
*/
model.setTitle(title);

}
} catch (Exception e) {

log.error(e.getMessage(), e);
}
}

/**
* This method is wrapper to return the model object
*
* @return {@link TextComponentModel}
*/
public TextComponent2Model getModel() {
return model;
}
}

  HTL

Text Component 2.0 - Showcases Java Use API
<div data-sly-use.text="org.demo.core.cqcomponents.TextComponent">
	<h1>Title: ${text.model.title}</h1>
</div>

 

2 replies

SantoshSai
Community Advisor
Community Advisor
July 6, 2022

Hi @fionas76543059 ,

What is the use of extending a class from WCMUsePojo?, the purpose of extending WCMUsePojo - 

  • It is a class that enables us to communicate to the front end of our component. It is similar to the JavaScript Use API in usage. 
  • This provide better decoupling of the business logic and so your code will be more easily maintainable and also easier to debug.
  • WCMUsePojo will need to be extend from that class, whereas Sling Models can be standalone class with @Model annotation and no keyword.

Your class treated as a regular POJOs that extend WCMUsePojo (Javadoc) implement the Use interface and are initialized with the scripting bindings, providing convenience methods for accessing commonly used objects (request, resource, properties, page etc). 

IMHO, there is no sense to extend WCMUsePojo in your component/service component.

Hope that helps!

Regards,

Santosh

Santosh Sai
Level 6
July 6, 2022

hi Santosh,

 

Are you saying, if you want to use annotations, don't use classes extended from wcmUsePojo :

 

Convert the Class to be a @Model class instead. ?

 

thanks

Fiona

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
July 6, 2022

@fionas76543059 Usage of annotation where extending WCMUsePojo class not a right approach. Please refer below code snippet you may understand using WCMUsePojo 

Java

import javax.jcr.Node;

import org.demo.core.models.TextComponentModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.sightly.WCMUsePojo;

public class TextComponent extends WCMUsePojo {

private TextComponentModel model;

@Override
public void activate() throws Exception {
try {
log.info("Text Component 2.0 backend logic starts");
/**
* Initializing the model object to set the values
*/
model = new TextComponentModel();
/**
* Getting the current node from the resource object which is available in Use
* API
*/
Node node = getResource().adaptTo(Node.class);
/**
* Check if the node has title property
*/
if(node.hasProperty("title")) {
/**
* Reading the title property from the string
*/
String title = node.getProperty("title").getString();

/**
* Setting the value entered by the user in the model object
*/
model.setTitle(title);

}
} catch (Exception e) {

log.error(e.getMessage(), e);
}
}

/**
* This method is wrapper to return the model object
*
* @return {@link TextComponentModel}
*/
public TextComponent2Model getModel() {
return model;
}
}

  HTL

Text Component 2.0 - Showcases Java Use API
<div data-sly-use.text="org.demo.core.cqcomponents.TextComponent">
	<h1>Title: ${text.model.title}</h1>
</div>

 

Santosh Sai
BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
July 6, 2022

Try this
 MyConfigService service = getSlingScriptHelper().getService(MyConfigService.class);

Level 6
July 7, 2022

thanks Brian, I'll try that.