官方文档链接
调用newProxyInstance
方法
方法有三个参数:
(1)类加载器
(2)增强方法所在的类,这个类实现的接口,支持多个接口
(3)实现这个接口InvocationHandler,创建代理对象,写增强的方法
public interface UserDao {
public void add(int a,int b) ;
public String update(String id);
}
public class UserDaoImp1 implements UserDao{
@Override
public int add(int a,int b) {
return a+b;
}
@Override
public String update(String id) {
return id;
}
}
public class JDKProxy {
public static void main(String[] args) {
//创建接口实现类代理对象
Class[] interfaces={UserDao.class};
UserDaoImp1 userDao=new UserDaoImp1();
UserDao dao=(UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(),interfaces,new UserDaoProxy(userDao));
System.out.println(dao.add(1,2));
}
}
//创建代理对象
class UserDaoProxy implements InvocationHandler{
//1.创建的是谁的代理对象,把谁传过来
//有参数构造传递
private Object obj;
public UserDaoProxy(Object obj){
this.obj=obj;
}
//增强的逻辑
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//方法之前
System.out.println("方法之前执行"+method.getName()+":传递的参数..."+ Arrays.toString(args));
//被增强的方法执行
Object res=method.invoke(obj,args);
//方法之后
System.out.println("方法之后执行..."+obj);
return res;
}
}
类里面哪些方法可以被增强,这些方法称为连接点。(能不能)
实际被真正增强的方法,被称为切入点。(做不做)
*是个动作
把通知应用到切入点的过程
public class User {
public void add(){
System.out.println("adding...");
}
}
//增强的类
public class UserProxy {
//前置通知
public void before(){
System.out.println("before...");
}
}
<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.xsd">
<context:component-scan base-package="com.company.aop">context:component-scan>
beans>
(2)使用注解创建User和UserProxy对象
//增强的类
@Component
public class UserProxy{}
//被增强的类
@Component
public class User{}
(3)在增强类上面添加注解@Aspect
//增强的类
@Component
@Aspect//生成代理对象
public class UserProxy{}
(4)在Spring的配置文件中,开启生成代理对象
<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
//增强的类
@Component
@Aspect
public class UserProxy {
//前置通知
//@Before注解表示作为前置通知
@Before(value = "execution(* com.company.aop.User.add(.. ))")
public void before(){
System.out.println("before...");
}
//最终通知(方法之后就执行,有异常也执行)
@After(value = "execution(* com.company.aop.User.add(.. ))")
public void after(){
System.out.println("after...");
}
//后置通知(有返回值以后才执行,有异常不执行)
@AfterReturning(value = "execution(* com.company.aop.User.add(.. ))")
public void afterReturning(){
System.out.println("afterReturning...");
}
//异常通知(add方法有异常才会执行)
@AfterThrowing(value = "execution(* com.company.aop.User.add(.. ))")
public void afterThrowing(){
System.out.println("afterThrowing...");
}
//环绕通知(方法之前之后都执行)
@Around(value = "execution(* com.company.aop.User.add(.. ))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕之前...");
//被增强的方法执行
proceedingJoinPoint.proceed();
System.out.println("环绕之后...");
}
}
//相同切入点抽取
@Pointcut(value = "execution(* com.company.aop.User.add(.. ))")
public void pointdemo(){
}
//前置通知
//@Before注解表示作为前置通知
@Before(value = "pointdemo()")
public void before(){
System.out.println("before...");
}
@Component
@Aspect
@Order(1)
public class PersonProxy{}
@Configuration //作为配置类
@ComponentScan(basePackages = {"com.company"}) //开启组件扫描
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringConfig {
}
<bean id="book" class="com.company.aopxml.Book">bean>
<bean id="bookProxy" class="com.company.aopxml.BookProxy">bean>
<aop:config>
<aop:pointcut id="p" expression="execution(* com.company.aopxml.Book.buy(..))"/>
<aop:aspect ref="bookProxy">
<aop:before method="before" pointcut-ref="p">aop:before>
aop:aspect>
aop:config>
p.s 配置文件方式真的好清晰啊…感觉比注解方式更容易理解