Injecting cacheManager not working (setter)

I’m building a plugin for Jira. I want to add a caching-layer so I wanted to use the

com.atlassian.cache.CacheManager
I have to inject this via an argument / setter.

Since I’m extending an other class I wanted to inject this via a setter, but for some reason it returns null all the time. It does not go past the setter.

import com.atlassian.cache.Cache;
import com.atlassian.cache.CacheLoader;
import com.atlassian.cache.CacheManager;
import com.atlassian.cache.CacheSettingsBuilder;

public class Foo extends AbstractJiraContextProvider
{
    private CacheManager cacheManager;

    public void setCacheManager(CacheManager cacheManager) {
        //It does not get past this function..
        this.cacheManager = cacheManager;
    }

    @Override
    public Map getContextMap(ApplicationUser user, JiraHelper jiraHelper) {

        cache = this.cacheManager.getCache("bar");

    }
}

I also tried this by doing the following:

public Foo(CacheManager cacheManager) {
    this.cacheManager = cacheManager;
}

After that the plugin does nothing anymore. I do not get errors, but it just gives 0 output nor logs

I used this for documentation: https://developer.atlassian.com/confdev/confluence-plugin-guide/writing-confluence-plugins/accessing-confluence-components-from-plugin-modules

And How do I cache data in a plugin?

Constructor injection is the right way for that. You aren’t using it for anything so there will be no output. If you debug your addon after deployment, you’ll be able to break on the constructor and confirm its been injected. Assuming you have declared Foo as a Component in your atlassian-plugin.xml you’ll be able to inject it into any simple REST service or Webwork action to drive the component to be provided and hook your debugger.

Regarding your links, they are for confluence. This post is for JIRA Development. JIRA uses constructor injection.

2 Likes

Remember, you’ll need to component-import the CacheManager in your atlassian-plugin.xml file.

<component-import key="cacheManager"
                  interface="com.atlassian.cache.CacheManager" />
2 Likes

Well it gives the following error when I do that:

atlassian-plugin.xml contains a definition of component-import. This is not allowed when Atlassian-Plugin-Key is set.

1 Like

I now believe the latest and greatest is to use the @ComponentImport annotation, as described in this StackOverflow post.

Ye, the only issue is that I get this error on using @ComponentImport

[ERROR] cannot find symbol: class ComponentImport

If you’ve got Atlassian-Plugin-Key in the pom.xml - then you’re using the new style Component importing using @ComponentImport. The one thing to note is that you may have to declare the class to be imported in the pom.xml:

<Import-Package>
                            com.atlassian.cache.*;resolution:="optional",
                            *;version="0";resolution:=optional
                        </Import-Package>

You will also need to make sure you have the right annotations in the dependencies:


        <dependency>
            <groupId>com.atlassian.plugin</groupId>
            <artifactId>atlassian-spring-scanner-annotation</artifactId>
            <version>${atlassian.spring.scanner.version}</version>
            <scope>compile</scope>
        </dependency>
1 Like