我有一个 spring boot 2 java 应用程序,想使用解释(未编译)的 groovy 代码来注入 aop。从阅读 spring 文档来看,这听起来像是可能的,但我找不到任何示例。AOP - 建议脚本化 Bean https://docs.spring.io/spring/docs/5.0.8.RELEASE/spring-framework-reference/languages.html#dynamic-language-final-notes-aop:
当然,您不仅限于建议脚本化 bean...您还可以使用受支持的动态语言编写方面本身,并使用此类 bean 来建议其他 Spring bean。不过,这确实是动态语言支持的高级使用。
最后,我希望有一个目录,可以在其中将 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>
测试配置.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
{
}
应用程序上下文.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);
}
}
获取文本.java
package de.test;
import org.springframework.stereotype.Component;
@Component
public class GetText
{
public String getText()
{
return "test1";
}
}
命令行.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";
}
}
After
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
- 改变切入点
- 还是不行
Update3:
- 添加了applicationContext.xml
- 现在在 org.springframework.aop.AopInitationException 中运行:通知方法的参数不匹配 [public java.lang.String groovy.TestAspect.doChangeGetText(org.aspectj.lang.ProceedingJoinPoint) 抛出 java.lang.Throwable];切入点表达式 [org.aspectj.weaver.internal.tools.PointcutExpressionImpl@4c4f4365];嵌套异常是 java.lang.IllegalArgumentException:对象不是声明类的实例
- 当将 testAspect.groovy 复制为 java 类时,切入点将按预期工作,并且将附加“;test 2”,但作为 groovy 脚本,将出现上述异常。
- 添加接口 IGetText 和/或 IAspectTest 没有帮助