微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在spring boot 2中使用groovy解释(使用spring-aop注释)2使用maven构建java应用程序?

我有一个spring boot 2 @L_404_1@ app,并希望使用解释(未编译)的groovy代码来注入aop.从阅读弹簧文档这听起来有可能,但我找不到任何例子.
AOP – advising scripted beans

You are of course not just limited to advising scripted beans…​ you can also write aspects themselves in a supported dynamic language and use such beans to advise other Spring beans. This really would be an advanced use of the dynamic language support though.

最后,我想有一个目录,我可以将groovy脚本(用于业务逻辑)添加到应用程序,它将通过spring-aop注入自己.

我不确定的是Spring Boot 2在这种情况下自动执行的操作,还是我必须手动集成基于org.springframework.scripting的代码

所以这是我的小测试项目:

project
|-pom.xml
|src/main/java/de/test
|-Commandline.java
|-MytestApplication.java
|-TestConfig.java
|-GetText.java
|src/main/resources/groovy
|-testAspect.groovy
|src/main/resources
|-application.properties   (empty at the moment)
|-applicationContext.xml

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>de.test</groupId>
  <artifactId>mytest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mytest</name>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/>
  </parent>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy</artifactId>
    </dependency>
  </dependencies>

</project>

TestConfig.java

package de.test;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoproxy;

@Configuration
@EnableAspectJAutoproxy
@ComponentScan({"de.test", "groovy"})
@ImportResource({"classpath*:applicationContext.xml"})
public class TestConfig
 {
 }

applicationContext.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

  <lang:groovy id="testAspect" refresh-check-delay="30000" script-source="classpath:groovy/TestAspect.groovy"/>

</beans>

MytestApplication.java

package de.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan({"de.test", "groovy"})
@SpringBootApplication
public class SpringboottestApplication
 {
  public static void main(final String[] args)
   {
    SpringApplication.run(SpringboottestApplication.class, args);
   }
 }

GetText.java

package de.test;

import org.springframework.stereotype.Component;

@Component
public class GetText
 {
  public String getText()
   {
    return "test1";
   }

 }

Commandline.java

package de.test;

import org.springframework.boot.CommandLineRunner;  
import org.springframework.stereotype.Component;

@Component
public class Commandline implements CommandLineRunner
 {
  @Autowired
  GetText textbean;

  public Commandline()
   {
    super();
   }

  @Override
  public void run(final String... args) throws Exception
   {
    System.out.println((this.textbean.getText());
   }
 }

testAspect.groovy

package groovy

import org.springframework.stereotype.Component

import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect

@Aspect
@Component
class TestAspect
 {
  @Around("execution(String de.test.GetText.getText())")
  public String doChangeGetText(ProceedingJoinPoint pjp) throws Throwable
   {
    String retVal = (String)pjp.proceed();
    return retVal + "; test2";
   }
 }

mvn clean install

我开始吧

java -jar target/mytest-0.0.1-SNAPSHOT.jar

这导致只有“test1”作为输出而不是想要的“test1; test2”.

UPDATE1:

>添加了TestConfig.java
>将src / main / groovy / groovy / testAspect.groovy移至src / main / resources / groovy / testAspect.groovy
>改进了一些东西,但仍然无效.

UPDATE2:

>添加了GetText.java
>修改了Commandline.java以使用GetText bean
>改变了pointcut
>仍然无法正常工作

UPDATE3:

>添加了applicationContext.xml
>现在在org.springframework.aop.AopInvocationException中运行:对advice方法的参数不匹配[public java.lang.String groovy.TestAspect.doChangeGetText(org.aspectj.lang.ProceedingJoinPoint)throws java.lang.Throwable];切入点表达式[org.aspectj.weaver.internal.tools.pointcutExpressionImpl@4c4f4365];嵌套异常是java.lang.IllegalArgumentException:object不是声明类的实例
>当将testAspect.groovy作为java类复制时 – 然后切入点按预期工作并且将附加“; test 2”,但作为groovy脚本,上述异常将是haben.
>添加接口IGetText和/或IAspectTest将无济于事

解决方法:

这是一个经典问题,在这里被问过几十次:

Spring AOP不拦截bean内的自调用,只调用从其他类到公共Spring bean方法调用.这也记录在Spring manual中.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐