从cmd运行时,Spring AOP无法正常工作

时间:2018-01-31 18:54:52

标签: java spring gradle spring-aop

我在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中运行jar(不工作)相同

这是我的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进行打包

1 个答案:

答案 0 :(得分:1)

正如你所看到的,我使用kotlin和java。问题是我把我的java文件放在src / main / kotlin目录而不是src / main / java

一旦我将我的java文件复制到正确的目录,它就像魅力一样开始工作。