在自定义Atlassian JIRA插件中注入SoyTemplateRenderer

时间:2015-10-25 00:47:52

标签: spring jira jira-plugin atlassian-plugin-sdk

我目前正在尝试开发一个自定义Jira插件,该插件还使用servlet渲染Soy模板。

这是我的servlet的代码:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package at.fraiss.plugins.custommenuitem.servlets;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.atlassian.soy.renderer.SoyTemplateRenderer;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Mario
 */
public class SoyTemplateServlet extends HttpServlet {

    private SoyTemplateRenderer soyTemplateRenderer;

    public SoyTemplateServlet(SoyTemplateRenderer soyTemplateRenderer)
    {
        this.soyTemplateRenderer = soyTemplateRenderer;
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        response.setContentType("text/html");
        response.getWriter().write(getHtml());
    }

    public String getHtml()
    {
        Map<String, Object> data = new HashMap<String,Object>();
        data.put("isLocal", true);
        data.put("url", "http://www.example.com");
        data.put("title", "example");
        return this.soyTemplateRenderer.render("jira.webresources:soy-templates", "JIRA.Templates.Links.helpLink", data);
    }
}

这就是我在atlassian-plugin.xml描述符中定义大豆模板文件的方式:

<web-resource key="soy-templates">
    <dependency>com.atlassian.soy.soy-template-plugin:soy-deps</dependency>


    <!-- Available to the server-side only -->
    <resource type="soy" name="links" location="links.soy"/>
 
    <!-- Make available to the client-side in JS as well... -->
    <transformation extension="soy">
        <transformer key="soyTransformer"/>
    </transformation>
    <resource type="download" name="Links.soy.js" location="links.soy"/>
</web-resource>

我已将以下部分添加到我的pom.xml中:

<dependency>
        <groupId>com.atlassian.soy</groupId>
        <artifactId>soy-template-renderer-api</artifactId>
        <version>4.0.4</version>
        <scope>provided</scope>
    </dependency>

并在尝试访问servlet时始终出现此错误

[INFO] [talledLocalContainer] Caused by: org.springframework.beans.factory.Unsat isfiedDependencyException: Error creating bean with name 'at.fraiss.plugins.cust ommenuitem.servlets.SoyTemplateServlet': Unsatisfied dependency expressed throug h constructor argument with index 0 of type [com.atlassian.soy.renderer.SoyTempl ateRenderer]: : No qualifying bean of type [com.atlassian.soy.renderer.SoyTempla teRenderer] found for dependency: expected at least 1 bean which qualifies as au towire candidate for this dependency. Dependency annotations: {}; nested excepti on is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualif ying bean of type [com.atlassian.soy.renderer.SoyTemplateRenderer] found for dep endency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} [INFO] [talledLocalContainer] at org.springframework.beans.factory.support.Con structorResolver.createArgumentArray(ConstructorResolver.java:749) [INFO] [talledLocalContainer] at org.springframework.beans.factory.support.Con structorResolver.autowireConstructor(ConstructorResolver.java:185) [INFO] [talledLocalContainer] at org.springframework.beans.factory.support.Abs tractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanF actory.java:1139) [INFO] [talledLocalContainer] at org.springframework.beans.factory.support.Abs tractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFa ctory.java:1042) [INFO] [talledLocalContainer] at org.springframework.beans.factory.support.Abs tractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory. java:504) [INFO] [talledLocalContainer] at org.springframework.beans.factory.support.Abs tractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.ja va:342) [INFO] [talledLocalContainer] ... 2 filtered [INFO] [talledLocalContainer] at java.lang.reflect.Method.invoke(Method.java:4 97) [INFO] [talledLocalContainer] at com.atlassian.plugin.osgi.spring.DefaultSprin gContainerAccessor.createBean(DefaultSpringContainerAccessor.java:112) [INFO] [talledLocalContainer] at com.atlassian.plugin.module.ClassPrefixModule Factory.createModule(ClassPrefixModuleFactory.java:39) [INFO] [talledLocalContainer] at com.atlassian.plugin.module.PrefixDelegatingM oduleFactory.createModule(PrefixDelegatingModuleFactory.java:100) [INFO] [talledLocalContainer] at com.atlassian.plugin.servlet.descriptors.Serv letModuleDescriptor.getModule(ServletModuleDescriptor.java:48) [INFO] [talledLocalContainer] at com.atlassian.plugin.servlet.DelegatingPlugin Servlet.<init>(DelegatingPluginServlet.java:33) [INFO] [talledLocalContainer] at com.atlassian.plugin.servlet.DefaultServletMo duleManager$LazyLoadedServletReference.create(DefaultServletModuleManager.java:5 41) [INFO] [talledLocalContainer] at com.atlassian.plugin.servlet.DefaultServletMo duleManager$LazyLoadedServletReference.create(DefaultServletModuleManager.java:5 21) [INFO] [talledLocalContainer] at com.atlassian.util.concurrent.LazyReference$S ync.run(LazyReference.java:325) [INFO] [talledLocalContainer] at com.atlassian.util.concurrent.LazyReference.g ...

2 个答案:

答案 0 :(得分:7)

我也遇到了这个问题(并且已经丢失了几个小时)。

Spring扫描程序显然只处理标有@Named的类。

这对我有用:

@Named("MyServlet")
public class MyServlet extends HttpServlet{
    @ComponentImport
    private final SoyTemplateRenderer soyTemplateRenderer;

    @Inject
    public MyServlet(@ComponentImport final SoyTemplateRenderer soyTemplateRenderer) {
        this.soyTemplateRenderer = soyTemplateRenderer;
    }
}

答案 1 :(得分:-3)

显然这与jiira无关,但是在你的沮丧中,你似乎在责备它及其文档。使用弹簧实际上是一个问题,而且你显然无法读取堆栈痕迹。

堆栈跟踪中的第一个条目可能以不同的方式提及No qualif ying bean of type [com.atlassian.soy.renderer.SoyTemplateRenderer] found。因此,您似乎没有为SoyTemplateRenderer提供bean定义以供Spring查找。纠正这个问题,这个特殊问题就会消失。