Spring 当来自 cmd 运行 时 AOP 不工作
Spring AOP not working when running from cmd
我正在 运行在 amazon aws 中安装一个 Spring 引导应用程序,我正在使用 Spring AOP 在调用某些带注释的方法时登录到数据库。
当我 运行 我的服务器在我的本地计算机上使用 IntelliJ Idea Ultimate(而不是 运行 gradle 任务!)一切正常,但是,如果我部署它进入 ElasticBeans(Java 平台)或 运行 gradle,顶部部分不工作。我的功能很好,我看到了结果等。但是我的数据库中没有任何记录。
有人可以帮忙吗?
@Aspect
@Component
public class JAspects {
private final Aspects aspectWorker;
public JAspects(@Autowired Aspects aspects) {
this.aspectWorker = aspects;
}
@Around(value = "@annotation(enableLogging) && args(reqArg, resArg,..)")
public ResponseEntity around(ProceedingJoinPoint joinPoint, EnableLogging enableLogging, HttpServletRequest reqArg, HttpServletResponse resArg) {
long startTime = System.currentTimeMillis();
ResponseEntity result = null;
try {
result = (ResponseEntity) joinPoint.proceed();
long timeTaken = System.currentTimeMillis() - startTime;
aspectWorker.success(reqArg, resArg, result, timeTaken, enableLogging, joinPoint);
} catch (Throwable throwable) {
long timeTaken = System.currentTimeMillis() - startTime;
aspectWorker.exception(reqArg, resArg, result, timeTaken, enableLogging, joinPoint, throwable);
}
return result;
}}
这就是我使用的方式
@EnableLogging(paramNames = ["firstParam", "secondParam"])
@GetMapping("api/v1/app/{mutation}&{number}/generateRedeem")
fun generateRedeemCodesForWebPage(firstParam:String, secondParam:Int){...}
附加信息
它只能在 IntelliJ IDEA 中运行。 运行 来自命令行的 jar 与 运行在 aws 中使用它相同(不工作 aop)
这是我的 gradle 文件
buildscript {
ext {
spekVersion = "1.1.5"
junitVersion = "4.12"
kluentVersion = "1.25"
pegdownVersion = "1.6.0"
kotlinVersion = "1.2.10"
hamkrestVersion = "1.4.2.2"
kotlintestVersion = "2.0.7"
apacheCommonsVersion = "3.7"
mockitoKotlinVersion = "1.5.0"
springBootVersion = "2.0.0.M7"
mysqlConnectorVersion = "6.0.6"
redmineJavaApiVersion = "3.1.0"
junitPlatformVersion = "1.0.2"
mainClass = "hu.click.ServerApplication"
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/milestone/" }
}
dependencies {
classpath "org.junit.platform:junit-platform-gradle-plugin:$junitPlatformVersion"
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion"
} }
plugins {
id "java"
id "io.spring.dependency-management" version "1.0.3.RELEASE"
id "org.jetbrains.kotlin.jvm" version "1.2.10"
id "org.jetbrains.kotlin.plugin.allopen" version "1.2.10"
id "org.jetbrains.kotlin.plugin.jpa" version "1.2.10"
id "org.jetbrains.kotlin.plugin.noarg" version "1.2.10"
id "org.jetbrains.kotlin.plugin.spring" version "1.2.10"
id "org.jetbrains.kotlin.kapt" version "1.2.10"
id "war"
}
apply plugin: "org.junit.platform.gradle.plugin"
apply plugin: "org.springframework.boot"
springBoot {
mainClass = mainClass
}
noArg {
annotation("hu.click.util.NoArg")
}
kapt {
generateStubs = true
}
junitPlatform {
filters {
engines {
include "spek"
}
}
}
test {
useJUnit {
exclude '**/*IT.class'
}
}
task integrationTest(type: Test) {
useJUnit {
include '**/*IT.class'
}
}
check.dependsOn integrationTest
integrationTest.mustRunAfter test
jar {
baseName = "server"
version = "0.0.1-SNAPSHOT"
manifest {
attributes "Main-Class": mainClass
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/milestone/" }
}
dependencies {
//Spring dependencies
runtime "org.springframework.boot:spring-boot-devtools"
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.boot:spring-boot-starter-mail"
implementation "org.springframework.boot:spring-boot-starter-json"
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
// implementation "org.springframework.boot:spring-boot-starter-actuator"
implementation "org.springframework.boot:spring-boot-starter-aop"
//Kotlin dependencies
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion"
//Core dependencies
runtime "mysql:mysql-connector-java:$mysqlConnectorVersion"
implementation "org.apache.commons:commons-lang3:$apacheCommonsVersion"
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
// implementation "org.aspectj:aspectjweaver:1.8.8"
// https://mvnrepository.com/artifact/org.aspectj/aspectjrt
// compile group: 'org.aspectj', name: 'aspectjrt', version: '1.8.13'
//App dependencies
implementation "org.pegdown:pegdown:$pegdownVersion"
implementation "com.taskadapter:redmine-java-api:$redmineJavaApiVersion"
//Test dependencies
testCompile "junit:junit:$junitVersion"
testCompile "com.natpryce:hamkrest:$hamkrestVersion"
testCompile "org.amshove.kluent:kluent:$kluentVersion"
testCompile "io.kotlintest:kotlintest:$kotlintestVersion"
testCompile "com.nhaarman:mockito-kotlin:$mockitoKotlinVersion"
testCompile "org.springframework.boot:spring-boot-starter-test"
//these had been excluded to use the supplied kotlin version
testCompile("org.jetbrains.spek:spek-api:$spekVersion") {
exclude group: 'org.jetbrains.kotlin'
}
testRuntime("org.jetbrains.spek:spek-junit-platform-engine:$spekVersion") {
exclude group: 'org.junit.platform'
exclude group: 'org.jetbrains.kotlin'
}
testCompile "org.junit.platform:junit-platform-runner:$junitPlatformVersion"
}
我正在使用gradlew bootJar、bootWar 进行打包
如您所见,我同时使用了 kotlin 和 java。问题是我将 java 文件放在 src/main/kotlin 目录中,而不是 src/main/java
一旦我将我的 java 文件复制到正确的目录,它就开始像魅力一样工作。
我正在 运行在 amazon aws 中安装一个 Spring 引导应用程序,我正在使用 Spring AOP 在调用某些带注释的方法时登录到数据库。
当我 运行 我的服务器在我的本地计算机上使用 IntelliJ Idea Ultimate(而不是 运行 gradle 任务!)一切正常,但是,如果我部署它进入 ElasticBeans(Java 平台)或 运行 gradle,顶部部分不工作。我的功能很好,我看到了结果等。但是我的数据库中没有任何记录。
有人可以帮忙吗?
@Aspect
@Component
public class JAspects {
private final Aspects aspectWorker;
public JAspects(@Autowired Aspects aspects) {
this.aspectWorker = aspects;
}
@Around(value = "@annotation(enableLogging) && args(reqArg, resArg,..)")
public ResponseEntity around(ProceedingJoinPoint joinPoint, EnableLogging enableLogging, HttpServletRequest reqArg, HttpServletResponse resArg) {
long startTime = System.currentTimeMillis();
ResponseEntity result = null;
try {
result = (ResponseEntity) joinPoint.proceed();
long timeTaken = System.currentTimeMillis() - startTime;
aspectWorker.success(reqArg, resArg, result, timeTaken, enableLogging, joinPoint);
} catch (Throwable throwable) {
long timeTaken = System.currentTimeMillis() - startTime;
aspectWorker.exception(reqArg, resArg, result, timeTaken, enableLogging, joinPoint, throwable);
}
return result;
}}
这就是我使用的方式
@EnableLogging(paramNames = ["firstParam", "secondParam"])
@GetMapping("api/v1/app/{mutation}&{number}/generateRedeem")
fun generateRedeemCodesForWebPage(firstParam:String, secondParam:Int){...}
附加信息
它只能在 IntelliJ IDEA 中运行。 运行 来自命令行的 jar 与 运行在 aws 中使用它相同(不工作 aop)
这是我的 gradle 文件
buildscript {
ext {
spekVersion = "1.1.5"
junitVersion = "4.12"
kluentVersion = "1.25"
pegdownVersion = "1.6.0"
kotlinVersion = "1.2.10"
hamkrestVersion = "1.4.2.2"
kotlintestVersion = "2.0.7"
apacheCommonsVersion = "3.7"
mockitoKotlinVersion = "1.5.0"
springBootVersion = "2.0.0.M7"
mysqlConnectorVersion = "6.0.6"
redmineJavaApiVersion = "3.1.0"
junitPlatformVersion = "1.0.2"
mainClass = "hu.click.ServerApplication"
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/milestone/" }
}
dependencies {
classpath "org.junit.platform:junit-platform-gradle-plugin:$junitPlatformVersion"
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion"
} }
plugins {
id "java"
id "io.spring.dependency-management" version "1.0.3.RELEASE"
id "org.jetbrains.kotlin.jvm" version "1.2.10"
id "org.jetbrains.kotlin.plugin.allopen" version "1.2.10"
id "org.jetbrains.kotlin.plugin.jpa" version "1.2.10"
id "org.jetbrains.kotlin.plugin.noarg" version "1.2.10"
id "org.jetbrains.kotlin.plugin.spring" version "1.2.10"
id "org.jetbrains.kotlin.kapt" version "1.2.10"
id "war"
}
apply plugin: "org.junit.platform.gradle.plugin"
apply plugin: "org.springframework.boot"
springBoot {
mainClass = mainClass
}
noArg {
annotation("hu.click.util.NoArg")
}
kapt {
generateStubs = true
}
junitPlatform {
filters {
engines {
include "spek"
}
}
}
test {
useJUnit {
exclude '**/*IT.class'
}
}
task integrationTest(type: Test) {
useJUnit {
include '**/*IT.class'
}
}
check.dependsOn integrationTest
integrationTest.mustRunAfter test
jar {
baseName = "server"
version = "0.0.1-SNAPSHOT"
manifest {
attributes "Main-Class": mainClass
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/milestone/" }
}
dependencies {
//Spring dependencies
runtime "org.springframework.boot:spring-boot-devtools"
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.boot:spring-boot-starter-mail"
implementation "org.springframework.boot:spring-boot-starter-json"
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
// implementation "org.springframework.boot:spring-boot-starter-actuator"
implementation "org.springframework.boot:spring-boot-starter-aop"
//Kotlin dependencies
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion"
//Core dependencies
runtime "mysql:mysql-connector-java:$mysqlConnectorVersion"
implementation "org.apache.commons:commons-lang3:$apacheCommonsVersion"
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
// implementation "org.aspectj:aspectjweaver:1.8.8"
// https://mvnrepository.com/artifact/org.aspectj/aspectjrt
// compile group: 'org.aspectj', name: 'aspectjrt', version: '1.8.13'
//App dependencies
implementation "org.pegdown:pegdown:$pegdownVersion"
implementation "com.taskadapter:redmine-java-api:$redmineJavaApiVersion"
//Test dependencies
testCompile "junit:junit:$junitVersion"
testCompile "com.natpryce:hamkrest:$hamkrestVersion"
testCompile "org.amshove.kluent:kluent:$kluentVersion"
testCompile "io.kotlintest:kotlintest:$kotlintestVersion"
testCompile "com.nhaarman:mockito-kotlin:$mockitoKotlinVersion"
testCompile "org.springframework.boot:spring-boot-starter-test"
//these had been excluded to use the supplied kotlin version
testCompile("org.jetbrains.spek:spek-api:$spekVersion") {
exclude group: 'org.jetbrains.kotlin'
}
testRuntime("org.jetbrains.spek:spek-junit-platform-engine:$spekVersion") {
exclude group: 'org.junit.platform'
exclude group: 'org.jetbrains.kotlin'
}
testCompile "org.junit.platform:junit-platform-runner:$junitPlatformVersion"
}
我正在使用gradlew bootJar、bootWar 进行打包
如您所见,我同时使用了 kotlin 和 java。问题是我将 java 文件放在 src/main/kotlin 目录中,而不是 src/main/java
一旦我将我的 java 文件复制到正确的目录,它就开始像魅力一样工作。