Proxy.newProxyInstance方法参数详解:
Jdk创建代理类,不会手动写那个代理类,通过Proxy.newProxyInstance();创建代理对象
2.通过原始类对象的id,获得的是代理对象,是通过BeanPostProcessor来加工完成代理对象创建的
代理对象创建方式:分为传统方法,和注解方法创建代理对象
他们的底层实现是通过JDK或cglib方式创建两种方式 BeanPostProcessor创建代理对象
jdk方式创建代理对象:
TestJDKproxy类:
package com.itheima.jdk;
import com.itheima.User;
import com.itheima.UserService;
import com.itheima.UserServiceImpl;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class TestJDKproxy {
public static void main(String[] args) {
//1.创建原始对象
final UserService userService=new UserServiceImpl();
//书写额外功能,匿名内部类实现InvocationHandler接口
InvocationHandler handler=new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("----额外方法日志方法-----");
//原始方法
Object ret = method.invoke(userService, args);
return ret;
}
};
//2.JDK创建动态代理对象jdk提供的方法 第一个参数是借用类加载器 第二个参数是和原始类实现相同的接口 第三个参数是额外功能
UserService userServiceProxy = (UserService) Proxy.newProxyInstance(TestJDKproxy.class.getClassLoader(), userService.getClass().getInterfaces(), handler);
userServiceProxy.regist( new User());
userServiceProxy.login("suns","123456");
}
}
cglib创建代理对象:
UserService类:
package com.itheima.cglib;
import com.itheima.User;
public class UserService {
public void login(String name,String password){
System.out.println("UserService.login");
}
public void register(User user){
System.out.println("UserService.register");
}
}
TestCglib类:
package com.itheima.cglib;
import com.itheima.User;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class TestCglib {
public static void main(String[] args) {
//创建原始对象
final UserService userService=new UserService();
Enhancer enhancer=new Enhancer();//创建Enhancer对象
enhancer.setClassLoader(TestCglib.class.getClassLoader());//借用加载器
enhancer.setSuperclass(userService.getClass());//继承父类
MethodInterceptor interceptor=new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("---额外功能cglib-----");
Object ret = method.invoke(userService, args);
return ret;
}
};
enhancer.setCallback(interceptor);//额外功能
//创建代理对象
UserService userServiceProxy = (UserService) enhancer.create();
//调用方法
userServiceProxy.login("suns","123456");
userServiceProxy.register(new User());
}
}
Spring工厂对代理对象的加工
UserService接口:
package com.itheima.factory;
import com.itheima.User;
public interface UserService {
public void login(String name,String password);
public void register(User user);
}
UserServiceImpl类:
package com.itheima.factory;
import com.itheima.User;
public class UserServiceImpl implements UserService {
@Override
public void login(String name, String password) {
System.out.println("UserServiceImpl.login");
}
@Override
public void register(User user) {
System.out.println("UserServiceImpl.register");
}
}
ProxyBeanPostProcessor:
package com.itheima.factory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
//bean是传过来原始类对象
//通过匿名内部类实现接口
InvocationHandler handler=new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("----额外功能 log------");
Object ret = method.invoke(bean, args);//执行原始方法 bean原始类对象,args原始方法参数
return ret;
}
};
//创建代理类对象 第一个参数是借用类加载器,第二个获得接口,第三个额外方法实现接口
UserService userServiceProxy = (UserService) Proxy.newProxyInstance(ProxyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), handler);
return userServiceProxy;
}
}
applicationContext2.xml:
ProxyTest类:
package com.itheima.factory;
import com.itheima.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProxyTest {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext2.xml");
UserService userService = (UserService) ctx.getBean("userService");//虽然创建的是原始对象,但是返回的是代理对象
userService.login("suns","1233456");
userService.register(new User());
}
}
基于Aspect切面注解的AOP编程:
MyAspect类:
package com.itheima.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
//通过注解表明是切面类
@Aspect
public class MyAspect {
//切入点复用,在定义的额外方法中引用这个切入点的函数
@Pointcut("execution(* login(..))")
public void myPointcut(){}
//通过注解添加额外方法 在注解里添加表达式代表切入点
@Around(value="myPointcut()")
public Object arround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("-----额外功能 log-------");
Object ret = joinPoint.proceed();
return ret;
}
//再添加一个额外功能
@Around(value="myPointcut()")
public Object arround1(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("-----额外功能 oop-------");
Object ret = joinPoint.proceed();
return ret;
}
}
applicationContext3.xml
TestAspect类:
package com.itheima.aspect;
import com.itheima.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAspect {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext3.xml");
UserService userService = (UserService) ctx.getBean("userService");//虽然创建的是原始对象,但是返回的是代理对象
userService.login("suns","1233456");
userService.register(new User());
}
}
UserService接口:
package com.itheima.aspect;
import com.itheima.User;
public interface UserService {
public void login(String name, String password);
public void register(User user);
}
UserServiceImpl类:
package com.itheima.aspect;
import com.itheima.User;
import com.itheima.log;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class UserServiceImpl implements UserService, ApplicationContextAware {
//通过实现一个接口,重写一个方法,接收TestAspect创建的工厂
private ApplicationContext ctx;//接收工厂
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.ctx=applicationContext;
}
@log //加注解
@Override
public void register(User user) {
System.out.println("UserServiceImpl.register");
//this指定的当前类,是原始对象,不是代理类,login不会执行额外功能
UserService userService = (UserService) ctx.getBean("userService");//获得代理对象
userService.login("suns","123456");
}
@Override
public void login(String name, String password) {
System.out.println("UserServiceImpl.login");
}
}
MyAspect类:
package com.itheima.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
//通过注解表明是切面类
@Aspect
public class MyAspect {
//切入点复用,在定义的额外方法中引用这个切入点的函数
//@Pointcut("execution(* login(..))")
//类作为切入点,这样所有的方法都作为切入点
@Pointcut("execution(* *..UserServiceImpl.*(..))")
public void myPointcut(){}
//通过注解添加额外方法 在注解里添加表达式代表切入点
@Around(value="myPointcut()")
public Object arround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("-----额外功能 log-------");
Object ret = joinPoint.proceed();
return ret;
}
//再添加一个额外功能
@Around(value="myPointcut()")
public Object arround1(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("-----额外功能 oop-------");
Object ret = joinPoint.proceed();
return ret;
}
}
TestAspect类:
package com.itheima.aspect;
import com.itheima.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAspect {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext3.xml");
UserService userService = (UserService) ctx.getBean("userService");//虽然创建的是原始对象,但是返回的是代理对象
//userService.login("suns","1233456");//让register方法内部调用login方法
userService.register(new User());
}
}



