Expand my Community achievements bar.

SOLVED

Sling Model with method having @PostConstruct - how to debug

Avatar

Level 4

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();
     

    }

}

1 Accepted Solution

Avatar

Correct answer by
Level 9

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

View solution in original post

6 Replies

Avatar

Correct answer by
Level 9

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

Avatar

Level 10

Here is one article which shows usage of postconstruct

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

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.

Avatar

Level 4

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/...

Avatar

Level 2

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

Avatar

Level 2

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 ?

Avatar

Employee

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