Skip to content

Commit 0000027

Browse files
committed
[FIX] #26 SpringBootAop\
1 parent eaaa6d9 commit 0000027

File tree

8 files changed

+114
-19
lines changed

8 files changed

+114
-19
lines changed

SpringBootAOP/src/main/java/com/example/java/component/TestAspect.java renamed to SpringBootAOP/src/main/java/com/example/java/component/SimpleAspect.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
@Aspect
1414
@Component
15-
public class TestAspect {
15+
public class SimpleAspect {
1616

17-
private static final Logger logger = LoggerFactory.getLogger(TestAspect.class);
17+
private static final Logger logger = LoggerFactory.getLogger(SimpleAspect.class);
1818

1919
@Before("execution(* com.example.java.service.*.*Aop(..))")
2020
public void onBeforeHandler(JoinPoint joinPoint) {

SpringBootAOP/src/main/java/com/example/java/service/TestServiceImpl.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
@Service
88
public class TestServiceImpl implements TestService {
99

10-
private static final Logger logger = LoggerFactory.getLogger(TestServiceImpl.class);
10+
private static final Logger logger = LoggerFactory.getLogger(TestServiceImpl.class);
1111

12-
@Override
13-
public String test() {
14-
String msg = "Hello, Spring Boot No AOP";
15-
logger.info(msg);
16-
return msg;
17-
}
12+
@Override
13+
public String test() {
14+
String msg = "Hello, Spring Boot No AOP";
15+
logger.info(msg);
16+
return msg;
17+
}
1818

19-
@Override
20-
public String testAop() {
21-
String msg = "Hello, Spring Boot AOP";
22-
logger.info(msg);
23-
return msg;
24-
}
19+
@Override
20+
public String testAop() {
21+
String msg = "Hello, Spring Boot AOP";
22+
logger.info(msg);
23+
return msg;
24+
}
2525
}

SpringBootAOP/src/main/kotlin/com/example/kotlin/SpringBootAopApplication.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication
44
import org.springframework.boot.runApplication
55

66
@SpringBootApplication
7-
class SpringBootAopApplication {
8-
fun main(args: Array<String>) {
9-
runApplication<SpringBootAopApplication>(*args)
10-
}
7+
class SpringBootAopApplication
8+
9+
fun main(args: Array<String>) {
10+
runApplication<SpringBootAopApplication>(*args)
1111
}
1212

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.kotlin.component
2+
3+
import org.aspectj.lang.JoinPoint
4+
import org.aspectj.lang.annotation.*
5+
import org.slf4j.Logger
6+
import org.slf4j.LoggerFactory
7+
import org.springframework.stereotype.Component
8+
9+
@Aspect
10+
@Component
11+
class SimpleAspect {
12+
13+
private val logger = LoggerFactory.getLogger(SimpleAspect::class.java)
14+
15+
@Before("execution(* com.example.kotlin.service.*.*Aop(..))")
16+
fun onBeforeHandler(joinPoint: JoinPoint) {
17+
logger.info("=============== onBeforeThing")
18+
}
19+
20+
@After("execution(* com.example.kotlin.service.*.*Aop(..))")
21+
fun onAfterHandler(joinPoint: JoinPoint) {
22+
logger.info("=============== onAfterHandler")
23+
}
24+
25+
@AfterReturning(pointcut = "execution(* com.example.kotlin.service.*.*Aop(..))", returning = "str")
26+
fun onAfterReturningHandler(joinPoint: JoinPoint, str: Any) {
27+
logger.info("@AfterReturning : $str")
28+
logger.info("=============== onAfterReturningHandler")
29+
}
30+
31+
@Pointcut("execution(* com.example.kotlin.service.*.*Aop(..))")
32+
fun onPointcut(joinPoint: JoinPoint) {
33+
logger.info("=============== onPointcut")
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.kotlin.config
2+
3+
import org.springframework.context.annotation.Configuration
4+
import org.springframework.context.annotation.EnableAspectJAutoProxy
5+
6+
@Configuration
7+
@EnableAspectJAutoProxy
8+
class AspectJConfig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.kotlin.service
2+
3+
interface TestService {
4+
5+
fun testAop(): String
6+
fun test(): String
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.kotlin.service
2+
3+
import org.slf4j.LoggerFactory
4+
import org.springframework.stereotype.Service
5+
6+
@Service
7+
class TestServiceImpl : TestService {
8+
9+
private val logger = LoggerFactory.getLogger(TestServiceImpl::class.java)
10+
11+
override fun test(): String {
12+
val msg = "Hello, Spring Boot With Kotlin No AOP"
13+
logger.info(msg)
14+
return msg
15+
}
16+
17+
override fun testAop(): String {
18+
val msg = "Hello, Spring Boot With Kotlin AOP"
19+
logger.info(msg)
20+
return msg
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.kotlin.web
2+
3+
import com.example.kotlin.service.TestService
4+
import org.springframework.beans.factory.annotation.Autowired
5+
import org.springframework.web.bind.annotation.GetMapping
6+
import org.springframework.web.bind.annotation.RestController
7+
8+
@RestController
9+
class TestController {
10+
11+
@Autowired
12+
private lateinit var service: TestService
13+
14+
@GetMapping(value = ["/noAop"])
15+
fun noAop(): String {
16+
return service.test()
17+
}
18+
19+
@GetMapping(value = ["/aop"])
20+
fun aop(): String {
21+
return service.testAop()
22+
}
23+
}

0 commit comments

Comments
 (0)