Sling Models - @Named annotation not working in AEM 6.4

Avatar

Avatar

srikanthp689160

Avatar

srikanthp689160

srikanthp689160

13-11-2019

Hi,

Until AEM 6.2/6.3 @Named annotation was working as expected but in AEM 6.4 it does not seem to be working i.e. for example if property name is desc in jcr repository but in respective sling model if below is the code then property values are not injected.

@ValueMapValue

@Named("desc")

private String shortDescription;

Am i missing something here? Is there any alternative to @Named annotation in AEM 6.4 and versions above?

Any suggestions on this is highly appreciated.

Thanks

Srikanth Pogula.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Shashi_Mulugu

MVP

Avatar

Shashi_Mulugu

MVP

Shashi_Mulugu
MVP

21-11-2019

Did you tried using " @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL,name = "pId")

Answers (8)

Answers (8)

Avatar

Avatar

srikanthp689160

Avatar

srikanthp689160

srikanthp689160

21-11-2019

Hi,

If you are asking about Apache Sling Models APIorg.apache.sling.models.api) version, it is 1.3.8

Usecase is we are using a sling model for a component where for example if property name is pId but would prefer variable name in respective sling model as productId, we were using below code in AEM 6.2 which was working fine

@Inject

@Named("pId")
private String productid;

We recently upgraded to AEM 6.4 where the above piece of code stopped working.

We have replaced @Inject with @ValueMapValue but could not achieve the functionality with @Named.

Solution proposed by shashi1223 i.e. @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL,name = "pId") worked.

Thanks

Srikanth Pogula.

Avatar

Avatar

srikanthp689160

Avatar

srikanthp689160

srikanthp689160

21-11-2019

Thanks everyone for the inputs.

Using @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL,name = "pId") resolved the issue.

Thanks

Srikanth Pogula.

Avatar

Avatar

BrijeshYadav

Avatar

BrijeshYadav

BrijeshYadav

21-11-2019

Use this for AEM 6.4

<dependency>

        <groupId>com.adobe.aem</groupId>

        <artifactId>uber-jar</artifactId>

        <version>6.4.1</version>

        <scope>provided</scope>

        <classifier>apis</classifier>

</dependency>

<dependency>

        <groupId>javax.inject</groupId>

        <artifactId>javax.inject</artifactId>

        <version>1</version>

        <scope>provided</scope>

</dependency>

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

952

Correct Reply

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

952

Correct Reply

1.0K
Jörg_Hoh
Employee

20-11-2019

Can you please describe your usecase plus the bundle versions you use, and post it to the sling user mailing list [1]?  There should be the right people which can have a good insight into the details.

Jörg

[1] Apache Sling :: Project Information

Avatar

Avatar

srikanthp689160

Avatar

srikanthp689160

srikanthp689160

20-11-2019

Hi Arun,

If we use @Inject annotation, respective property values are not injected, they are null. We are using below maven dependency for sling models in our project.

<dependency>

    <groupId>org.apache.sling</groupId>

    <artifactId>org.apache.sling.models.api</artifactId>

    <version>1.0.0</version>

    <scope>provided</scope>

</dependency>

Since @Inject was not working, we replaced it with @ValueMapValue which works but along with that @Named does not seem to work.

Thanks

Srikanth Pogula

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.1K

Correct Reply

839

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.1K

Correct Reply

839
Arun_Patidar
MVP

16-11-2019

Hi,

Try with inject

@Inject

@Named("pId")
private String productid;

Avatar

Avatar

srikanthp689160

Avatar

srikanthp689160

srikanthp689160

14-11-2019

Hi Arun,

We are adapting the model to Resource. Here is the sample code.

We are on AEM 6.4 SP4

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

/** The product category title. */
@ValueMapValue

@Named("pId")
private String productid;

public String getProductid() {

  return productid;

}

}

@Inject does not inject value hence we are using @ValueMapValue which works.

Thanks,

Srikanth Pogula

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.1K

Correct Reply

839

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.1K

Correct Reply

839
Arun_Patidar
MVP

14-11-2019

Hi,

How are you adapting the model, using Resource or Request?

I am using @Name attribute along with @Inject and adapting as Resource type and it is working.

Can you share a sample code so that we can try to reproduce the issue?