Skip to content

Commit

Permalink
改变位置
Browse files Browse the repository at this point in the history
  • Loading branch information
CPU-Code committed Jul 23, 2021
1 parent 3eef82b commit a2e8332
Show file tree
Hide file tree
Showing 24 changed files with 408 additions and 26 deletions.
6 changes: 4 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ java编程基础 面向对象 javaAPI 集合 IO GUI JDBC 多线程 网络编程
- [JDBC](JDBC/src/com/cpucode/java/README.md)
- [多线程](thread/src/com/cpucode/java/README.md)
- [Lambda表达式](Lambda/README.md)
- [网络编程](network/src/com/cpucode/java/README.md)
- [网络编程](network/README.md)
- [JDK8](jdk8/README.md)
- [单元测试](junit_test/src/com/cpucode/java/README.md)
- [反射](reflect/src/com/cpucode/java/README.md)
- [反射](reflect/README.md)
- [注解](annotation/src/com/cpucode/java/README.md)
- [JVM](JVM/src/com/cpucode/java/README.md)
- [java数据结构与算法](data_algorithm/README.md)
Expand Down
10 changes: 10 additions & 0 deletions reflect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

## [反射](src/main/java/com/cpucode/java/reflect)

- [x] [Get_class_object__获取class对象](src/main/java/com/cpucode/java/reflect/Get_class_object.java)
- [x] [Get_class_methods__获取class方法](src/main/java/com/cpucode/java/reflect/Get_class_methods.java)
- [x] [Get_structure__获取class构造方法](src/main/java/com/cpucode/java/reflect/Get_structure.java)
- [x] [Get_methods__获取成员方法](src/main/java/com/cpucode/java/reflect/Get_methods.java)
- [x] [Reflect_test__反射测试框架](src/main/java/com/cpucode/java/reflect/Reflect_test.java)

- [返回目录](#文件目录)
54 changes: 54 additions & 0 deletions reflect/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.cpucode</groupId>
<artifactId>reflect</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- others start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

</project>
26 changes: 22 additions & 4 deletions reflect/reflect.iml
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="true">
<exclude-output />
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.6" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.8" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.8" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.8" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.8" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.8" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.8" level="project" />
</component>
</module>
10 changes: 0 additions & 10 deletions reflect/src/com/cpucode/java/README.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.cpucode.java.aop.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
* Annotation版Aspect切面Bean
*
* @Component 声明这是一个组件
* @Aspect 声明这是一个切面Bean,AnnotaionAspect是一个面,由框架实现的
*
* @author : cpucode
* @date : 2021/7/22
* @time : 16:23
* @github : https://github.com/CPU-Code
* @csdn : https://blog.csdn.net/qq_44226094
*/
@Component
@Aspect
public class AnnotaionAspect {

private final static Logger log = Logger.getLogger(AnnotaionAspect.class);

/**
* 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
* 切点的集合,这个表达式所描述的是一个虚拟面(规则)
* 就是为了Annotation扫描时能够拿到注解中的内容
*/
@Pointcut("execution(* com.cpucode.java.aop.aspect..*(..))")
public void aspect(){}

/**
* 配置前置通知,使用在方法aspect()上注册的切入点
* 同时接受JoinPoint切入点对象,可以没有该参数
* @param joinPoint
*/
@Before("aspect()")
public void before(JoinPoint joinPoint){
log.info("before " + joinPoint);
}

/**
* 配置后置通知,使用在方法aspect()上注册的切入点
* @param joinPoint
*/
@After("aspect()")
public void after(JoinPoint joinPoint){
log.info("after " + joinPoint);
}

/**
* 配置环绕通知,使用在方法aspect()上注册的切入点
* @param joinPoint
*/
@Around("aspect()")
public void around(JoinPoint joinPoint){
long start = System.currentTimeMillis();

try {
((ProceedingJoinPoint) joinPoint).proceed();
long end = System.currentTimeMillis();

log.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms!");
} catch (Throwable e) {
long end = System.currentTimeMillis();

log.info("around " + joinPoint +
"\tUse time : " + (end - start) +
" ms with exception : " + e.getMessage());
}
}

/**
* 配置后置返回通知,使用在方法aspect()上注册的切入点
* @param joinPoint
*/
@AfterReturning("aspect()")
public void afterReturn(JoinPoint joinPoint){
log.info("afterReturn " + joinPoint);
}

/**
* 配置抛出异常后通知,使用在方法aspect()上注册的切入点
* @param joinPoint
* @param ex
*/
@AfterThrowing(pointcut="aspect()", throwing="ex")
public void afterThrow(JoinPoint joinPoint, Exception ex){
log.info("afterThrow " + joinPoint + "\t" + ex.getMessage());
}
}
60 changes: 60 additions & 0 deletions reflect/src/main/java/com/cpucode/java/aop/aspect/ArgsAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.cpucode.java.aop.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
* 获取参数Aspect切面Bean
* @Component 声明这是一个组件
* @Aspect 声明这是一个切面Bean
* @author : cpucode
* @date : 2021/7/22
* @time : 16:16
* @github : https://github.com/CPU-Code
* @csdn : https://blog.csdn.net/qq_44226094
*/
@Component
@Aspect
public class ArgsAspect {
private final static Logger log = Logger.getLogger(ArgsAspect.class);

/**
* 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
*/
@Pointcut("execution(* com.cpucode.java.aop.service..*(..))")
public void aspect(){
System.out.println("aspect() @Pointcut");
}

/**
* 配置前置通知,拦截返回值为 com.cpucode.java.aop.model.Member 的方法
* @param joinPoint
*/
@Before("execution(com.cpucode.java.aop.model.Member com.cpucode.java.aop.service..*(..))")
public void beforeReturnUser(JoinPoint joinPoint){
log.info("beforeReturnUser " + joinPoint);
}

/**
* 配置前置通知,拦截参数为 com.cpucode.java.aop.model.Member 的方法
* @param joinPoint
*/
@Before("execution(* com.cpucode.java.aop.service..*(com.cpucode.java.aop.model.Member))")
public void beforeArgUser(JoinPoint joinPoint){
log.info("beforeArgUser " + joinPoint);
}

/**
* 配置前置通知,拦截含有long类型参数的方法,并将参数值注入到当前方法的形参id中
* @param joinPoint
* @param id
*/
@Before("aspect()&&args(id)")
public void beforeArgId(JoinPoint joinPoint, long id){
log.info("beforeArgId " + joinPoint + "\tID:" + id);
}
}
Loading

0 comments on commit a2e8332

Please sign in to comment.