Sling Model API update to v1.2.2 (AEM 6.2) Not support @Inject anymore | Community
Skip to main content
John_Ky
New Participant
June 1, 2016
Solved

Sling Model API update to v1.2.2 (AEM 6.2) Not support @Inject anymore

  • June 1, 2016
  • 5 replies
  • 2228 views

AEM 6.2 uses Sling Model API 1.2.2, but this bundle seems not include @Inject annotation. So how to I inject some of sightly object inside Sling Model?

Old version look like: 

@Model(adaptables = Resource.class) public class HelloWorldModel { @Inject private SlingSettingsService settings; @Inject @Named("sling:resourceType") @Default(values = "No resourceType") protected String resourceType; private String message; @PostConstruct protected void init() { message = "\tHello World!\n"; message += "\tThis is instance: " + settings.getSlingId() + "\n"; message += "\tResource type is: " + resourceType + "\n"; } public String getMessage() { return message; } }

Up to new sling model 1.2.2, the @Inject doesnt work anymore.

Please help.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by John_Ky

Thanks for your answer.

Sling Model API version 1.2.2 have removed the package "javax.inject" so that is the reason my code does not work any more.

I just tried to add:

<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> </dependency>

and it works well now.

5 replies

GK-007
New Participant
June 13, 2016

Worked after declaring version in <Import-Package> section.

Thanks,

Kishore

GK-007
New Participant
June 13, 2016

i am seeing similar issue now.

I have below entry in my core pom.xml

<dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
            <scope>provided</scope>
</dependency>

Still i see model injection is not working.

Please help me.

Thanks,

Kishore

John_Ky
John_KyAuthorAccepted solution
New Participant
June 3, 2016

Thanks for your answer.

Sling Model API version 1.2.2 have removed the package "javax.inject" so that is the reason my code does not work any more.

I just tried to add:

<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> </dependency>

and it works well now.

Tuhin_Ghosh
New Participant
June 2, 2016

I am not sure if I am missing something here but I have always used import javax.inject.Inject; for @Inject annotation. Please try this once. Should work.

 

Thanks

Tuhin

New Participant
June 1, 2016

Try declaring the version to be imported in the reactor pom in the <Import-Package> section: 

<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Bundle-Activator>org.uc.news.core.Activator</Bundle-Activator> <Import-Package> javax.inject;version=0.0.0, com.day.cq.commons;version=0.0.0, * </Import-Package> <Export-Package> org.uc.news.core.*</Export-Package> <Sling-Model-Packages>org.uc.news.core.models</Sling-Model-Packages> </instructions> </configuration> </plugin>