Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

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

Avatar

Level 7

hi folks,

Can you annotate a class extended from WcmUsePojo with @component

and call a service using @reference?

 

I.e. using felix scr annotations.

 

I tried it and got  errors messages about ScriptEvaluationExceptions.

 

The service works fine with bundlecontext.

 

thanks

Fiona

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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>

 

View solution in original post

6 Replies

Avatar

Community Advisor

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

Avatar

Level 7

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

Avatar

Correct answer by
Community Advisor

@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>

 

Avatar

Level 7

thanks Santosh,

Is TextComponentModel  annotated with @Model so that I can use annotations in it?

thanks

Fiona

Avatar

Community Advisor

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