Sling Model with method having @PostConstruct - how to debug | Community
Skip to main content
jydps87387977
Level 3
March 9, 2016
Solved

Sling Model with method having @PostConstruct - how to debug

  • March 9, 2016
  • 6 replies
  • 12156 views

Hi,

1. Can you please recommend the steps to debug the sling model.

2. If I have the method with @PostConstruct, when will it get executed ?

3. If there is a constructor, when will it get executed ?

 

++++ EXAMPLE ++++

@Model(adaptables=Resource.class)
public class TestModel {

    @Inject @Named("path")
    private String id;

    @Inject @Named("jcr:title")
    private String title;

    @PostConstruct
    protected void init() {
        LOG.info("Inside init() ");

    }

    @Inject
    public TestModel(Resource resource) {

        final String id = resource.getPath();
     

    }

}

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 Jitendra_S_Toma

Hi,

Here is a very good Sling model document. I guess you might have gone through if not, please do. Let me know if you have some other question which is not covered in the document. Regarding debugging, Logging could be a good option. I don't know about Eclipse way of debugging. But, people do use eclipse as well.

https://sling.apache.org/documentation/bundles/models.html#postconstruct-methods

----Jitendra

6 replies

Jitendra_S_Toma
Jitendra_S_TomaAccepted solution
Level 10
March 10, 2016

Hi,

Here is a very good Sling model document. I guess you might have gone through if not, please do. Let me know if you have some other question which is not covered in the document. Regarding debugging, Logging could be a good option. I don't know about Eclipse way of debugging. But, people do use eclipse as well.

https://sling.apache.org/documentation/bundles/models.html#postconstruct-methods

----Jitendra

edubey
Level 10
March 10, 2016

Here is one article which shows usage of postconstruct

http://adobeaemclub.com/sightly-pagination-component-using-sling-model-and-javascript-use-api-in-aem-6-1/

Sling model works on injection, once all injections are completed this will method (@postconstruct) gets called.

In order to debug you can simply configure slf4j logger in your class and logs can be seen in error.log of your project.

jydps87387977
Level 3
March 10, 2016

Thank you for sharing the links. They helped.

Also, I found one more good link with many Model examples:

MODEL EXAMPLES:

http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/

vyan024
Level 2
August 10, 2016

Did you find any better way to debug sling model via eclipse? 

chanchalesj6077
Level 2
August 29, 2017

what if you have WCMUSEPOJO working but not the sling models as sling models-->@postconstruct method not at all being called form the HTL component in AEM 6.3 .

Any clue ?

Feike_Visser1
Adobe Employee
Adobe Employee
August 29, 2017

PostConstruct is only used by sling-models. With WCMUsePojo you have the activate method.