Skip to content

Commit b6cf674

Browse files
添加动态编程相关的内容
1 parent fd71c01 commit b6cf674

21 files changed

Lines changed: 481 additions & 0 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package FDynamic.Annotation.DI;
2+
3+
public class ServiceA {
4+
5+
@SimpleInject
6+
ServiceB b;
7+
8+
public void callB(){
9+
b.action();
10+
}
11+
12+
public ServiceB getB() {
13+
return b;
14+
}
15+
16+
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package FDynamic.Annotation.DI;
2+
3+
@SimpleSingleton
4+
public class ServiceB {
5+
6+
public void action(){
7+
System.out.println("I'm B");
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package FDynamic.Annotation.DI;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.Target;
5+
6+
import static java.lang.annotation.ElementType.FIELD;
7+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
8+
9+
@Retention(RUNTIME)
10+
@Target(FIELD)
11+
public @interface SimpleInject {
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package FDynamic.Annotation.DI;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.Target;
5+
6+
import static java.lang.annotation.ElementType.TYPE;
7+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
8+
9+
@Retention(RUNTIME)
10+
@Target(TYPE)
11+
public @interface SimpleSingleton {
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package FDynamic.Annotation.SimpleDemo;
2+
3+
/**
4+
* @author supremepole
5+
*/
6+
public class Main {
7+
public static void main(String[] args){
8+
StudentInfoUtil.getStudentInfo(Student.class);
9+
//学生信息:学号∶1, 名字∶张三, 地址∶北京市
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package FDynamic.Annotation.SimpleDemo;
2+
3+
/**
4+
* @author supremepole
5+
*/
6+
public class Student {
7+
@StudentInfo(id=1,name="张三",address="北京市")
8+
private String studentInfo;
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package FDynamic.Annotation.SimpleDemo;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* @author supremepole
7+
*/
8+
@Target(ElementType.FIELD)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Documented
11+
public @interface StudentInfo {
12+
//学号
13+
public int id() default -1;
14+
//名字
15+
public String name() default "";
16+
//地址
17+
public String address() default "";
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package FDynamic.Annotation.SimpleDemo;
2+
3+
import java.lang.reflect.Field;
4+
5+
/**
6+
* @author supremepole
7+
*/
8+
public class StudentInfoUtil {
9+
public static void getStudentInfo(Class<?> clazz){
10+
StringBuilder sb=new StringBuilder();
11+
sb.append("学生信息:");
12+
Field[] fields = clazz.getDeclaredFields();//通过反射获取处理注解
13+
for(Field field:fields){
14+
if(field.isAnnotationPresent(StudentInfo.class)){
15+
StudentInfo fruitProvider =(StudentInfo)
16+
field.getAnnotation(StudentInfo.class);//处理注解信息
17+
sb.append("学号∶").append(fruitProvider.id());
18+
sb.append(", 名字∶").append(fruitProvider.name());
19+
sb.append(", 地址∶").append(fruitProvider.address());
20+
System.out.println(sb);
21+
}
22+
}
23+
}
24+
}

src/FDynamic/Proxy/AOP/Aspect.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package FDynamic.Proxy.AOP;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.Target;
5+
6+
import static java.lang.annotation.ElementType.TYPE;
7+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
8+
9+
@Retention(RUNTIME)
10+
@Target(TYPE)
11+
public @interface Aspect {
12+
Class<?>[] value();
13+
}
14+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package FDynamic.Proxy.AOP;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.Arrays;
5+
6+
@Aspect({ ServiceB.class })
7+
public class ExceptionAspect {
8+
public static void exception(Object object,
9+
Method method, Object[] args, Throwable e) {
10+
System.err.println("exception when calling: " + method.getName()
11+
+ "," + Arrays.toString(args));
12+
}
13+
}

0 commit comments

Comments
 (0)