Like the title says, here are my classes (this is just for testing if I can AOP to work):
ApplicationContextConfiguration.kt
package com.fdev.jobby
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.EnableAspectJAutoProxy
@Configuration
@ComponentScan
@EnableAspectJAutoProxy
class ApplicationContextConfiguration {
@Bean
fun dataSource():DataSource{
return DataSource()
}
}
DataSource.kt
package com.fdev.jobby
open class DataSource {
fun getDataFromDB(): String{
// fetching Data
return "data was loaded"
}
fun insertDataToDB(){
// inserting Data
}
}
LoggingDataSource.kt
package com.fdev.jobby
import mu.KotlinLogging
import org.aspectj.lang.annotation.AfterReturning
import org.aspectj.lang.annotation.AfterThrowing
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut
import org.springframework.stereotype.Component
@Aspect
@Component
class LoggingDataSource {
private val logger = KotlinLogging.logger {}
@Pointcut("execution(* com.fdev.jobby.DataSource.*(..))")
fun getAllMethods(){}
@Before("getAllMethods()")
fun startOfAccessToDB(){
logger.error("Starting access to DB")
}
@AfterReturning("getAllMethods()")
fun endOfAccessToDBSuccess(){
println("Successful access to DB..")
}
@AfterThrowing("getAllMethods()")
fun endOfAccessToDBFailure(){
println("Failed access to DB..")
}
}
AOPController.kt
package com.fdev.jobby
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("aop")
class AOPController(@Autowired val dataSource: DataSource) {
@GetMapping("testing")
fun aopTesting(): String{
val data = dataSource.getDataFromDB()
return data
}
}
So when I call localhost:8080/aop/testing it shows the string "data was loaded" but it wont log or print my advices. What am I doing wrong? I have been reading here but the docs seem very unintuitive and not helpful at all. Please help :(
Edit: my gradle dependencies look like this
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-aop")
implementation("io.github.microutils:kotlin-logging-jvm:2.1.20")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}