Sling Model with method having @PostConstruct - how to debug | Community
Skip to main content
jydps87387977
Level 3
Mar 9, 2016 9:54 PM UTC
Solved

Sling Model with method having @PostConstruct - how to debug

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.
Jitendra_S_Toma
Jitendra_S_TomaAccepted solution
Level 10
Mar 10, 2016 4:44 AM UTC

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

Feike_Visser1
Employee
Aug 29, 2017 6:55 PM UTC

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

Translate
chanchalesj6077
Level 2
Aug 29, 2017 8:29 AM UTC

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 ?

Translate
vyan024
Level 2
Aug 10, 2016 11:59 AM UTC

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

Translate
jydps87387977
Level 3
Mar 10, 2016 3:18 PM UTC

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/

Translate
edubey
Level 10
Mar 10, 2016 4:52 AM UTC

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.

Translate
Jitendra_S_Toma
Jitendra_S_TomaAccepted solution
Level 10
Mar 10, 2016 4:44 AM UTC

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

Translate