How to Inject CacheManager

I’m writing a plugin for JIRA server and I want to use Atlassian’s built-in caching functionality. However, I’m having trouble injecting the CacheManager. If I include @Scanned at the top of my class, my plugin loads up fine BUT returns an exception at runtime:

No qualifying bean of type [com.atlassian.cache.CacheManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

When I include @Named on top of the class, the plugin won’t even become enabled.

With either annotation, when I package the plugin in verbose mode, this little tidbit pops up:

[INFO] 	(/) Found annotation 'com.atlassian.plugin.spring.scanner.annotation.component.Scanned' inside class 'com.cdk.alm.releaseui.apiextension.RestReleaseService'
[INFO] 	(/) Found 'com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport' inside class 'com.cdk.alm.releaseui.apiextension.RestReleaseService' method '<init>' parameter 'com.atlassian.cache.CacheManager'
[INFO] 		(X) Class not suitable 'com.cdk.alm.releaseui.apiextension.RestReleaseService$1'.  Skipping...

I’m not sure what I’m doing wrong here. How do you inject CacheManager into JIRA Rest Plugin Modules?

Here’s the class that I’m trying to inject CacheManager into:

import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import javax.inject.Inject;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
// Other imports

@Scanned
@Path("/")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class RestReleaseService
{
    @Inject
    public RestReleaseService(@ComponentImport CacheManager cacheManager)
    {
        // Do stuff with cacheManager here
    }
}

Check your pom.xml. Since you’re using SpringScanner, you’ll need to make the package available for you. In the pom.xml you should see something like:


            <plugin>
                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>bitbucket-maven-plugin</artifactId>
                <!--                <artifactId>maven-jira-plugin</artifactId>-->
                                <version>${amps.version}</version>
                                <extensions>true</extensions>
                                <configuration>
                                    <enableQuickReload>true</enableQuickReload>
                                    <enableFastdev>false</enableFastdev>
                                    <products>
                                        <product>
                                          <id>bitbucket</id>
                                            <instanceId>bitbucket</instanceId>
                                            <version>${bitbucket.version}</version>
                                            <dataVersion>${bitbucket.data.version}</dataVersion>
                                            <enableQuickReload>true</enableQuickReload>
                                            <enableFastdev>false</enableFastdev>

                                            <!--              <id>jira</id>
                                                        <instanceId>jira</instanceId>
                                                        <version>${jira.version}</version>
                                                        <dataVersion>${jira.data.version}</dataVersion>
                -->
                                                    </product>
                                                </products>

                                                <instructions>
                                                    <skipManifestValidation>true</skipManifestValidation>
                                                    <Atlassian-Plugin-Key>${atlassian.plugin.key}</Atlassian-Plugin-Key>
<!--                                                    <Atlassian-Scan-Folders>/descriptors/bitbucket</Atlassian-Scan-Folders> -->



                        <!-- Add package import here -->
                        <Import-Package>
                            org.eclipse.gemini.blueprint.*;resolution:="optional",
                            *;version="0";resolution:=optional
                        </Import-Package>

                        <!-- Ensure plugin is Spring powered -->
                        <Spring-Context>*</Spring-Context>
                    </instructions>
                </configuration>
            </plugin>

On the Import-package, make sure to add:
com.atlassian.cache.*;resolution:="optional",

That should make the bundle available for your add-on.

4 Likes