如何配置Spring为Transactional使用aspectj?

前端之家收集整理的这篇文章主要介绍了如何配置Spring为Transactional使用aspectj?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

根据这个答案
https://stackoverflow.com/a/3429757/2674303
要在执行本地方法时使用@Transactional,我执行了以下步骤:

添加到pom.xml:

    

在上下文中替换

在我看到之后

Could not obtain transaction-synchronized Session for current thread

另外补充:

但是当make mvn clean install我看到(当测试尝试执行时会出现问题.在我的测试中我申请上下文):

    java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    ....
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.weaving.AspectJWeavingEnabler#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)

    ... 31 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)

    ... 45 more
Caused by: java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
    at org.springframework.context.weaving.DefaultContextLoadTimeWeaver.setBeanClassLoader(DefaultContextLoadTimeWeaver.java:91)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1590)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1561)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    ... 54 more

更新

整个pom.xml:

更新2:

如果在构建应用程序时跳过测试阶段 – 构建成功然后运行mvn clean install jetty:run我看到以下stacktrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAspect': Post-processing failed of bean type [class org.springframework.transaction.aspectj.AnnotationTransactionAspect] failed; nested exception is java.lang.IllegalStateException: Failed to introspect annotations: protected abstract void org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$pointcut$$transactionalMethodExecution$e12(java.lang.Object)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:936)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    ...
Caused by: java.lang.IllegalStateException: Failed to introspect annotations: protected abstract void org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$pointcut$$transactionalMethodExecution$e12(java.lang.Object)
    at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:166)
    ... 69 more
Caused by: java.lang.IllegalAccessError: class sun.reflect.GeneratedConstructorAccessor14 cannot access its superclass sun.reflect.ConstructorAccessorImpl
    at sun.misc.Unsafe.defineClass(Native Method)
    at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:63)
    ... 76 more
13.11.2015 04:57:35 ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAspect': Post-processing failed of bean type [class org.springframework.transaction.aspectj.AnnotationTransactionAspect] failed; nested exception is java.lang.IllegalStateException: Failed to introspect annotations: protected abstract void org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$pointcut$$transactionalMethodExecution$e12(java.lang.Object)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:936)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    ...
Caused by: java.lang.IllegalStateException: Failed to introspect annotations: protected abstract void org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$pointcut$$transactionalMethodExecution$e12(java.lang.Object)
    at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:166)
    at org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(AnnotatedElementUtils.java:91) 
    ... 69 more
Caused by: java.lang.IllegalAccessError: class sun.reflect.GeneratedConstructorAccessor14 cannot access its superclass sun.reflect.ConstructorAccessorImpl
    at sun.misc.Unsafe.defineClass(Native Method)   
    ... 76 more

更新3

我也尝试添加

@Configuration
@EnableLoadTimeWeaving(aspectjWeaving= EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class Configiuration {

我看到同样的结果

更新4:

使用Transactional注释的类:

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {
    @PostConstruct
    public void init() {
        this.refreshStopJobs();
    }
    ...
}   

实际上this post对我有帮助

最佳答案
基本上要使AspectJ加载时间编织工作,您首先需要考虑您的应用程序将要运行的环境.正如名称所说的“加载时间”意味着您的类将被类加载器更改.所以从你的问题我明白你需要你的LTW在两个环境中.首先是命令行的“Test”环境,第二个是Servlet容器(Jetty / Tomcat).

因此,让我们从测试环境开始:您的测试失败,因为类加载器没有机会编辑/仪器/编织您的类,Spring无法帮助您,因为它尚未运行且您已选择加载时间超过运行时编织.所以我们需要以某种方式告诉默认的类加载器我们将要破解类.这在Spring Documentation中记录为了这样做,我们需要添加-javaagent,在我们的例子中,而不是instrumenting / profiling运行时只会在加载时更改类.要实现这一点,只需告诉maven添加2个javaaganets:aspectj-weaver-x.jar和特殊的spring-instrument-x.jar,所以只需在你的maven插件部分添加即可.

只需记住将spring-instrument-4.1.6.RELEASE.jar添加为至少在提供的范围内的依赖项,以确保它在repo中;)
添加此配置设置后,测试执行将正常工作.试一试:

mvn清洁包

接下来让我们从Servlet容器的第二个环境开始.
如果您正在运行Tomcat,那么您很高兴,因为您可以通过向项目添加以下META-INF / context.xml来覆盖默认的类加载器.

更多信息here.对于Jetty,您应该手动编辑运行脚本并添加两个-javaagents,就像我们为maven测试环境所做的那样.

猜你在找的Spring相关文章