Skip to content

Commit 6d5623e

Browse files
committed
gray aop conponent
1 parent 91d4ce2 commit 6d5623e

File tree

10 files changed

+155
-4
lines changed

10 files changed

+155
-4
lines changed

study-spring-boot/src/main/java/com/bage/study/springboot/aop/annotation/gray/GrayConfig.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import lombok.extern.slf4j.Slf4j;
44
import org.springframework.stereotype.Component;
55

6+
import java.util.Random;
7+
68
@Component
79
@Slf4j
810
public class GrayConfig {
911

1012
public boolean matchGraySwitch(String key) {
11-
return false;
13+
int nexted = new Random().nextInt(100);
14+
return nexted < 50;
1215
}
1316

1417
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.bage.study.springboot.aop.annotation.gray;
2+
3+
import com.bage.study.springboot.aop.annotation.rest.RestResult;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@SpringBootApplication
10+
@RestController
11+
public class GrayFlowApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(GrayFlowApplication.class, args);
15+
}
16+
17+
@RestResult
18+
@RequestMapping("/hello")
19+
public Object hello(){
20+
return "hello";
21+
}
22+
23+
}

study-spring-boot/src/main/java/com/bage/study/springboot/aop/annotation/gray/OrderGrayFlow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
String keySpEL() default "";
1313

14-
String copyToMethod() default ""; // 到类对应方法,默认是相同方法
14+
String toMethod() default ""; // 到类对应方法,默认是相同方法
1515

1616
}

study-spring-boot/src/main/java/com/bage/study/springboot/aop/annotation/gray/OrderGrayFlowAspect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class OrderGrayFlowAspect {
2121
@Autowired
2222
private OrderGrayFlowLogic orderGrayFlowLogic;
2323

24-
@Around(value = "@annotation(com.ctrip.train.trnorder.core.biz.temp.gray.OrderGrayFlow)")
24+
@Around(value = "@annotation(com.bage.study.springboot.aop.annotation.gray.OrderGrayFlow)")
2525
public Object around(ProceedingJoinPoint pjp) throws Throwable {
2626
Object[] args = null;
2727
Object result = null;

study-spring-boot/src/main/java/com/bage/study/springboot/aop/annotation/gray/OrderGrayFlowLogic.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Object getBean(OrderGrayFlow annotation, Method method) {
4545
public Object invokeSync(Object bean, OrderGrayFlow annotation, Method method, Object[] args) { // 同步,直接执行
4646
Object invoke = null;
4747
try {
48-
String methodName = "".equals(annotation.copyToMethod()) ? method.getName() : annotation.copyToMethod();
48+
String methodName = "".equals(annotation.toMethod()) ? method.getName() : annotation.toMethod();
4949
Class grayToClass = annotation.toClass();
5050
Method toMethod = grayToClass.getMethod(methodName, method.getParameterTypes());
5151
invoke = toMethod.invoke(bean, args);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.bage.study.springboot.aop.annotation.gray;
2+
3+
4+
import com.bage.study.springboot.aop.annotation.flow.copy.FlowCopyApplication;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
@RunWith(SpringRunner.class)
12+
@SpringBootTest(classes = GrayFlowApplication.class)
13+
public class GrayFlowTest {
14+
15+
@Autowired
16+
MyHelloService helloService;
17+
18+
@Test
19+
public void sayHi() {
20+
helloService.sayHi("hihi");
21+
}
22+
23+
@Test
24+
public void sayHiHey() {
25+
helloService.sayHi("hihi");
26+
helloService.sayHey("heihie");
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.bage.study.springboot.aop.annotation.gray;
2+
3+
public interface HelloService {
4+
5+
String sayHi(String param);
6+
7+
String sayHey(String param);
8+
9+
String sayYo(String param);
10+
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.bage.study.springboot.aop.annotation.gray;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class MyHelloService implements HelloService {
7+
8+
@Override
9+
@OrderGrayFlow(toClass = YouHelloService.class)
10+
public String sayHi(String param) {
11+
System.out.println("MyHelloService.sayHi:" + param);
12+
return "MyHelloService.sayHi:" + param;
13+
}
14+
15+
@Override
16+
@OrderGrayFlow(toClass = YouHelloService.class)
17+
public String sayHey(String param) {
18+
System.out.println("MyHelloService.sayHey:" + param);
19+
return "MyHelloService.sayHey:" + param;
20+
}
21+
22+
@Override
23+
public String sayYo(String param) {
24+
System.out.println("MyHelloService.sayYo:" + param);
25+
return "MyHelloService.sayYo:" + param;
26+
}
27+
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.bage.study.springboot.aop.annotation.gray;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class TheirHelloService implements HelloService {
7+
8+
@Override
9+
@OrderGrayFlow(toClass = MyHelloService.class, toMethod = "sayHi")
10+
public String sayHi(String param) {
11+
System.out.println("TheirHelloService.sayHi:" + param);
12+
return "TheirHelloService.sayHi:" + param;
13+
}
14+
15+
@Override
16+
public String sayHey(String param) {
17+
System.out.println("TheirHelloService.sasayHeyyHi:" + param);
18+
return "TheirHelloService.sayHey:" + param;
19+
}
20+
21+
@Override
22+
public String sayYo(String param) {
23+
System.out.println("TheirHelloService.sayYo:" + param);
24+
return "TheirHelloService.sayYo:" + param;
25+
}
26+
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.bage.study.springboot.aop.annotation.gray;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class YouHelloService implements HelloService {
8+
9+
@Autowired
10+
private TheirHelloService theirHelloService;
11+
12+
@Override
13+
@OrderGrayFlow(toClass = MyHelloService.class, toMethod = "sayHi")
14+
public String sayHi(String param) {
15+
System.out.println("YouHelloService.sayHi:" + param);
16+
return "YouHelloService.sayHi:" + param;
17+
}
18+
19+
@Override
20+
public String sayHey(String param) {
21+
System.out.println("YouHelloService.sasayHeyyHi:" + param);
22+
return "YouHelloService.sayHey:" + param;
23+
}
24+
25+
@Override
26+
public String sayYo(String param) {
27+
System.out.println("YouHelloService.sayYo:" + param);
28+
return "YouHelloService.sayYo:" + param;
29+
}
30+
31+
}

0 commit comments

Comments
 (0)