Sling Model Post Constructor is not calling | Community
Skip to main content
New Participant
Aug 29, 2020 7:24 AM UTC
Solved

Sling Model Post Constructor is not calling

We are in process of AEM 6.5 migration, 
Sling model Post construction is not calling in AEM 6.5, Same thing working fine in AEM 6.4 instance.

Post construction will be called, if I add javax.annotation package in export or import package of maven-bundle-plugin, But nested Sling models are not injecting, Nested sling models will be null.

 

Child class:

/**
*
*/
package com.aem.community.core.models;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.inject.Inject;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;

import org.apache.sling.models.annotations.Model;

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Address {

@586265
private String address;

private String postalcode;

@PostConstruct
protected void init() {
postalcode = "560001";
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPostalcode() {
return postalcode;
}

public void setPostalcode(String postalcode) {
this.postalcode = postalcode;
}
}


Parent class:

package com.aem.community.core.models;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.inject.Inject;

import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Employee {

@586265
private String name;

@586265
private Address address;

private String email;

@PostConstruct
protected void init() {
email = "test@test.com";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Shashi_Mulugu
Shashi_MuluguAccepted solution
New Participant
Aug 29, 2020 9:52 AM UTC

@irannachoukimath1 thank you for posting your question in community.  It is great place to exchange ideas and resolve problems.

 

Can you please post your skeleton model class here so that we can guide you better.

 

Also please below blog post for reference. 

 

https://sourcedcode.com/blog/aem/aem-sling-model-constructor-injection

4 replies

New Participant
Oct 4, 2020 6:26 PM UTC
Hi @irannachoukimath1 I'm also facing the same issue, looks like you were able to solve your's, can you please elaborate what is the steps that need to be taken here
Translate
knitshweta
Level 2
Aug 31, 2020 6:45 AM UTC
Which Java compiler version are you using. In AEM 6.4 and 6.5?
Translate
Nikhil-Kumar
New Participant
Aug 29, 2020 9:29 PM UTC

@irannachoukimath1 

 

By nested sling model you mean, nesting another resource in parent model ?

 

Can you provide code snippet of parent and child model along with class declaration ?

 

Translate
Shashi_Mulugu
Shashi_MuluguAccepted solution
New Participant
Aug 29, 2020 9:52 AM UTC

@irannachoukimath1 thank you for posting your question in community.  It is great place to exchange ideas and resolve problems.

 

Can you please post your skeleton model class here so that we can guide you better.

 

Also please below blog post for reference. 

 

https://sourcedcode.com/blog/aem/aem-sling-model-constructor-injection

Translate