Expand my Community achievements bar.

SOLVED

Sling Models - @Named annotation not working in AEM 6.4

Avatar

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

9 Replies

Avatar

Community Advisor

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?



Arun Patidar

Avatar

Level 4

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

Community Advisor

Hi,

Try with inject

@Inject

@Named("pId")
private String productid;



Arun Patidar

Avatar

Level 4

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

Employee Advisor

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

Correct answer by
Community Advisor

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

Avatar

Community Advisor

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

Level 4

Thanks everyone for the inputs.

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

Thanks

Srikanth Pogula.

Avatar

Level 4

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.