Sling Model Post Constructor is not calling

Avatar

Avatar
Level 1
IrannaChoukimath
Level 1

Likes

0 likes

Total Posts

3 posts

Correct reply

0 solutions
View profile

Avatar
Level 1
IrannaChoukimath
Level 1

Likes

0 likes

Total Posts

3 posts

Correct reply

0 solutions
View profile
IrannaChoukimath
Level 1

29-08-2020

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 {

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

@inject
private String name;

@inject
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;
}
}

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Springboard
MVP
Shashi_Mulugu
MVP

Likes

232 likes

Total Posts

294 posts

Correct reply

67 solutions
Top badges earned
Springboard
Bedrock
Validate 1
Applaud 100
Establish
View profile

Avatar
Springboard
MVP
Shashi_Mulugu
MVP

Likes

232 likes

Total Posts

294 posts

Correct reply

67 solutions
Top badges earned
Springboard
Bedrock
Validate 1
Applaud 100
Establish
View profile
Shashi_Mulugu
MVP

29-08-2020

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

Answers (1)

Answers (1)

Avatar

Avatar
Boost 250
MVP
Nikhil-Kumar
MVP

Likes

251 likes

Total Posts

262 posts

Correct reply

38 solutions
Top badges earned
Boost 250
Validate 1
Ignite 3
Ignite 1
Give Back 5
View profile

Avatar
Boost 250
MVP
Nikhil-Kumar
MVP

Likes

251 likes

Total Posts

262 posts

Correct reply

38 solutions
Top badges earned
Boost 250
Validate 1
Ignite 3
Ignite 1
Give Back 5
View profile
Nikhil-Kumar
MVP

29-08-2020

@IrannaChoukimath 

 

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 ?