AOP
作用
不修改源码的情况下 对程序进行增强 增加一些新功能
AOP 可以进行权限校验 记录日志
AOP 依赖IOC的实现
使用代理类包装目标类 把代理类添加到IOC中然后我们就可以在运行目标类方法的时候
让代理类拦截这个目标类的方法 替这个目标类运行方法
代理机制
JDK动态代理
Cglib动态代理
使用切面引入jar包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
我们可以使用配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--开启AOP注解-->
<aop:aspectj-autoproxy />
<context:annotation-config />
<context:component-scan base-package="com.tledu.spring04"/>
</beans>
或者注解开发
创建注解类然后三个注解
注解支持 @EnableAspectJAutoProxy
@Configuration //代表配置文件
@ComponentScan("需要加入ioc的包") //指定spring容器初始化的时候要扫描的包
新建一个切面包
@Aspect 代表着是切面类
@Component 让切面类注入到ioc容器
创建一个方法 不需要写方法体
添加注解
@Pointcut 切入点 里面是写的拦截哪一个方法 方法名要写全路径
AOP切面对方法的增强是通过
@Before 完成前需要做的事情
@After 完成后需要做的事情
参数是上面我们定义的方法
JoinPoint是连接点 通过getArgs方法获取参数 用数组接受
java反射
java运行过程中 我们可以获取任意一个类的所有属性和方法 对于任意一个对象
可以调用他的任意一个犯法和属性 动态获取信息
class对象获取
forName()
对象.getClass()
类名.class
方法
getName 获取全名
getSimpleName 获取类名
getDeclaredFileds 获取所有的属性 返回值为Filed数组
getDeclaredMethods 获取所有的方法 返回值为Method数组
Filed类 方法
set(对象,值);
如下
field.setAccessible(true);
暴力破解 对于private修饰的变量注入值直接使用这个
Method方法
getName
invoke 参数(对象,方法的参数)
自定义注解
使用两个标签
@Target 参数为 多种 意思是用在哪里
@Retention 三种参数
source 编译时丢弃
class jvm启动丢弃
runtime 运行时可以被捕获