Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Level 2

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.

View solution in original post

5 Replies

Avatar

Level 5

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>

Avatar

Level 7

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

Avatar

Correct answer by
Level 2

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.

Avatar

Level 9

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

Avatar

Level 9

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

Thanks,

Kishore