Sling Models - @Named annotation not working in AEM 6.4 | Community
Skip to main content
srikanthp689160
Level 4
Nov 13, 2019 12:03 PM UTC
Solved

Sling Models - @Named annotation not working in AEM 6.4

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.

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
Nov 21, 2019 12:42 PM UTC

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

9 replies

srikanthp689160
Level 4
Nov 22, 2019 6:52 AM UTC

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.

Translate
srikanthp689160
Level 4
Nov 22, 2019 6:39 AM UTC

Thanks everyone for the inputs.

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

Thanks

Srikanth Pogula.

Translate
BrijeshYadav
Level 5
Nov 21, 2019 3:27 PM UTC

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>

Translate
Shashi_Mulugu
Shashi_MuluguAccepted solution
New Participant
Nov 21, 2019 12:42 PM UTC

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

Translate
joerghoh
Employee
Nov 20, 2019 6:05 PM UTC

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

Translate
srikanthp689160
Level 4
Nov 20, 2019 10:47 AM UTC

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

Translate
arunpatidar
New Participant
Nov 16, 2019 3:18 PM UTC

Hi,

Try with inject

@Inject

@Named("pId")
private String productid;

Translate
Arun Patidar
srikanthp689160
Level 4
Nov 15, 2019 5:40 AM UTC

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

Translate
arunpatidar
New Participant
Nov 14, 2019 8:57 PM UTC

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?

Translate
Arun Patidar