Sunshine-jcy

插件里使用@Transactional注解实现事务

https://github.com/atlascommunity/jira-plugins-groovy这款插件的做法 

jira的ao模块,默认是不支持@Transactional注解,如果需要使用事务,可以显式声明事务:

import com.atlassian.jira.transaction.Transaction;
import com.atlassian.jira.transaction.Txn;
 
 
    Transaction txn = Txn.begin();
    try {
        ...
        txn.commit();
        ...
    finally {
        txn.finallyRollbackIfNotCommitted();

但是这样,稍显麻烦,最近查看了一款官方开源插件(https://github.com/atlascommunity/jira-plugins-groovy),这款插件里面是通过注解的方式实现事务的,实现方式如下

需要在pom文件里,增加aop支持,添加位置:

 

代码:

<Import-Package>
                           <!-- imports -->
                           org.springframework.osgi.*;resolution:="optional",
                           org.eclipse.gemini.blueprint.*;resolution:="optional",
                           <!-- spring imports -->
                           org.springframework.aop,
                           org.springframework.core,
                           <!-- jira stuff -->
                           com.atlassian.jira.plugin.webfragment.conditions,
                           com.atlassian.jira.issue.customfields.searchers,
                           com.atlassian.jira.jql.operand,
                           <!-- required for spring lifecycle annotations -->
                           <!--javax.annotation,-->
                           <!-- exclude stuff for querydsl pocketknife -->
                           !net.sf.cglib.proxy,
                           !org.jvnet.hudson.annotation_indexer,
                           <!-- everything else -->
                           *;resolution:=optional
                       </Import-Package>
                       <Require-Bundle>org.apache.felix.framework</Require-Bundle>

 

另外,需要增加几个类:

ProxiedBeanPostProcessor 折叠源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.shdsd.plugin.organizationPro.util.spring;
 
import com.shdsd.plugin.organizationPro.util.AnnotationUtil;
import com.shdsd.plugin.organizationPro.util.PluginInfo;
import org.aopalliance.intercept.Interceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
 
import java.lang.annotation.Annotation;
 
public class ProxiedBeanPostProcessor implements BeanPostProcessor {
    private final Logger logger = LoggerFactory.getLogger(ProxiedBeanPostProcessor.class);
 
    private final Class<? extends Annotation> annotationClass;
    private final Interceptor interceptor;
 
    public ProxiedBeanPostProcessor(Class<? extends Annotation> annotationClass, Interceptor interceptor) {
        this.annotationClass = annotationClass;
        this.interceptor = interceptor;
    }
 
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        return o;
    }
 
    public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
        try {
            Class<?> beanClass = bean.getClass();
 
            //ignore 3rd party beans
            // PluginInfo.PLUGIN_KEY是插件的包的根路径,一般是groupId + arctifactId
            if (beanClass.getCanonicalName().startsWith(PluginInfo.PLUGIN_KEY)) {
                if (AnnotationUtil.isAnnotated(annotationClass, beanClass)) {
                    ProxyFactory proxyFactory = new ProxyFactory(bean);
                    proxyFactory.addAdvice(interceptor);
 
                    return proxyFactory.getProxy(ProxiedBeanPostProcessor.class.getClassLoader());
                }
                else return bean;
            }
        catch (Exception e) {
            logger.warn("Unable to process bean {} - {}", name, bean, e);
        }
        return bean;
    }
 
}
TransactionalInterceptor 折叠源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.shdsd.plugin.organizationPro.util.tx;
 
 
import com.atlassian.activeobjects.tx.Transactional;
import com.atlassian.jira.transaction.Transaction;
import com.atlassian.jira.transaction.Txn;
import com.shdsd.plugin.organizationPro.util.AnnotationUtil;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
 
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
 
public final class TransactionalInterceptor implements MethodInterceptor {
    public static final Class<? extends Annotation> ANNOTATION_CLASS = Transactional.class;
 
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        if (AnnotationUtil.isAnnotated(ANNOTATION_CLASS, invocation.getMethod())) {
            return executeInTransaction(invocation);
        else {
            return invocation.proceed();
        }
    }
 
    private Object executeInTransaction(MethodInvocation invocation) throws Throwable {
        Transaction transaction = Txn.begin();
        try {
            Object result = invocation.proceed();
 
            transaction.commit();
 
            return result;
        catch (InvocationTargetException e) {
            throw e.getTargetException();
        finally {
            transaction.finallyRollbackIfNotCommitted();
        }
    }
}

 

TransactionalAnnotationProcessor 折叠源码
1
2
3
4
5
6
7
8
9
10
11
package com.shdsd.plugin.organizationPro.util.tx;
 
import com.shdsd.plugin.organizationPro.util.spring.ProxiedBeanPostProcessor;
import org.springframework.stereotype.Component;
 
@Component
public final class TransactionalAnnotationProcessor extends ProxiedBeanPostProcessor {
    public TransactionalAnnotationProcessor() {
        super(TransactionalInterceptor.ANNOTATION_CLASS, new TransactionalInterceptor());
    }
}

然后,就可以在service的方法上使用@Transactional注解了

 

 

 

 

react页面js不用加AJS.params.loggedInUser判断了,统一后台加,并且登录后会自动跳转

 

 

 

posted on 2021-02-09 18:50  Sunshine-jcy  阅读(172)  评论(0编辑  收藏  举报

导航