一、Aop(重点)
创建Aop的j六种创建方式
jdk动态代理、cglib字节码增强(没有接口)、半自动、全自动、Aspectj框架、注解
一、jdk动态代理
1.创建service接口和实现类(目标类)
2创建切面类
3.创建代理类(使用静态工厂)
4创建xml
5创建测试类
//1.创建service接口和实现类(目标类)
public interface UserServcie {
public void addUser();
public void deleteUser();
public void updateUser();
}
public class UserServcieImpl implements UserServcie {
public void addUser() {
System.out.println("添加用户");
}
public void deleteUser() {
System.out.println("删除用户");
}
public void updateUser() {
System.out.println("修改用户");
}
}
//2创建切面类
public class MyAspect {
public void before(){
System.out.println("执行前");
}
public void after(){
System.out.println("执行后");
}
}
//3.创建代理类(使用静态工厂)
public class MyBeanFactory {
public static UserServcie createUserServcie(){
//1.目标类
final UserServcie userServcieProxy=new UserServcieImpl();
//切面类
final MyAspect myAspect=new MyAspect();
UserServcie userServcie1=(UserServcie)Proxy.newProxyInstance(
MyBeanFactory.class.getClassLoader(),
userServcieProxy.getClass().getInterfaces(),
new InvocationHandler(){
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//执行前
myAspect.before();
//执行目标方法 放行
Object obj= method.invoke(userServcieProxy,args);
//执行后
myAspect.after();
return obj;
}
}
);
return userServcieProxy;
}
}
//4创建xml
<!--目标类-->
<bean id="userServcieId" class="com.java.day03.a_bzd.UserServcieImpl"></bean>
<!--切面类-->
<bean id="myAspectId" class="com.java.day03.a_bzd.MyAspect"></bean>
<bean id="proxyUserServcieId" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="com.java.day03.a_bzd.UserServcie"></property>
<property name="target" ref="userServcieId"></property>
<property name="interceptorNames" value="myAspectId"></property>
</bean>
//5创建测试类
@Test
public void test01(){
UserServcie userServcie=MyBeanFactory.createUserServcie();
userServcie.addUser();
userServcie.updateUser();
userServcie.deleteUser();
}
二、cglib字节码增强(没有接口)
1.创建实现接口(目标类)
2创建切入类
3创建代理类
4.创建测试类
//1.创建实现接口(目标类)
public class UserServcieImpl {
public void addUser(){
System.out.println("添加用户2");
}
public void deleteUser(){
System.out.println("删除用户2");
}
public void updateUser(){
System.out.println("修改用户2");
}
}
//2创建切入类
public class MyAspect {
public void before(){
System.out.println("执行前");
}
public void after(){
System.out.println("执行后");
}
}
//3创建代理类
public class MyBeanFactory {
public static UserServcieImpl createUserServcie(){
//1.目标类
final UserServcieImpl userService = new UserServcieImpl();
final MyAspect myAspect = new MyAspect();
// 3.代理类 ,采用cglib,底层创建目标类的子类
//3.1 核心类
Enhancer enhancer = new Enhancer();
//3.2 确定父类
enhancer.setSuperclass(userService.getClass());
/* 3.3 设置回调函数 , MethodInterceptor接口 等效 jdk InvocationHandler接口
* intercept() 等效 jdk invoke()
* 参数1、参数2、参数3:以invoke一样
* 参数4:methodProxy 方法的代理
*
*
*/
enhancer.setCallback(new MethodInterceptor(){
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
//前
myAspect.before();
//执行目标类的方法
Object obj = method.invoke(userService, args);
// * 执行代理类的父类 ,执行目标类 (目标类和代理类 父子关系)
methodProxy.invokeSuper(proxy, args);
//后
myAspect.after();
return obj;
}
});
//3.4 创建代理
UserServcieImpl proxService = (UserServcieImpl) enhancer.create();
return proxService;
}
}
//4.创建测试类
public class Test0007 {
public static void main(String[] args) {
UserServcieImpl userServcie = MyBeanFactory.createUserServcie();
userServcie.addUser();
userServcie.deleteUser();
userServcie.updateUser();
}
}
三、半自动
1创建UserService接口和实现类(目标类)
2创建半自动类(切面类)
3创建xml文件
4创建测试类
//1创建UserService接口和实现类(目标类)
public interface UserServcie {
public void addUser();
public void deleteUser();
public void updateUser();
}
public class UserServcieImpl implements UserServcie {
public void addUser(){
System.out.println("添加用户");
}
public void deleteUser(){
System.out.println("删除用户");
}
public void updateUser(){
System.out.println("修改用户");
}
}
//2创建半自动类(切面类)
/*半自动*/
public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("执行前");
Object obj= invocation.proceed();
System.out.println("执行后");
return obj;
}
}
//3创建xml文件
<!--目标类-->
<bean id="userServcieId" class="com.java.day03.a_bzd.UserServcieImpl"></bean>
<!--切面类-->
<bean id="myAspectId" class="com.java.day03.a_bzd.MyAspect"></bean>
<bean id="proxyUserServcieId" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="com.java.day03.a_bzd.UserServcie"></property>
<property name="target" ref="userServcieId"></property>
<property name="interceptorNames" value="myAspectId"></property>
</bean>
//4创建测试类
public class Test01 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans3.xml");
//半自动,调用者,如果不需要使用AOP又要去修改一次代码
UserServcie userServcie = (UserServcie) ac.getBean("proxyUserServcieId");
userServcie.addUser();
userServcie.deleteUser();
userServcie.updateUser();
}
}
四、全自动
1创建UserService接口和实现类(目标类)
2创建全自动类(切面类)
3创建xml文件
4创建测试类
//1创建UserService接口和实现类(目标类)
public interface UserServcie {
public void addUser();
public void deleteUser();
public void updateUser();
}
public class UserServcieImpl implements UserServcie {
public void addUser(){
System.out.println("添加用户");
}
public void deleteUser(){
System.out.println("删除用户");
}
public void updateUser(){
System.out.println("修改用户");
}
}
//2创建全自动类(切面类)
/*全自动*/
public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("前");
Object obj= invocation.proceed();
System.out.println("后");
return obj;
}
}
//3创建xml文件
<!--目标类-->
<bean id="userServcie" class="com.java.day03.b_qzd.UserServcieImpl"></bean>
<!--切面类-->
<bean id="myAspect" class="com.java.day03.b_qzd.MyAspect"></bean>
<aop:config proxy-target-class="true">
<!--声明切入点-->
<aop:pointcut id="myPointcut" expression="execution(* com.java.day03.b_qzd.*.*(..))"/>
<aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"></aop:advisor>
</aop:config>
//4创建测试类
public class Test02 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans003.xml");
//半自动,调用者,如果不需要使用AOP又要去修改一次代码
UserServcie userServcie = (UserServcie) ac.getBean("userServcie");
userServcie.addUser();
userServcie.deleteUser();
userServcie.updateUser();
}
}
五、Aspectj框架
1.导入Aspectj的jar包
2创建service接口和实现类
3创建切面类
4创建xml
5创建测试类
//1.导入Aspectj的jar包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
//2创建service接口和实现类
public interface UserServcie {
public void addUser();
public String deleteUser();
public void updateUser();
}
public class UserServcieImpl implements UserServcie {
public void addUser(){
System.out.println("添加用户");
}
public String deleteUser(){
System.out.println("删除用户");
int i = 1/0;
return "我返回的值";
}
public void updateUser(){
System.out.println("修改用户");
}
}
//3创建切面类
public class MyAspect {
/**
* 前置通知
*/
public void myBefore(JoinPoint joinPoint){
//这里面是可以有业务代码的,可以new其它类的对象,调用方法等等
//joinPoint.getSignature().getName() 得到我所执行的方法名
System.out.println("前置通知"+joinPoint.getSignature().getName());
}
/**
* 后置通知
*/
public void myAfterRetuning(JoinPoint joinpoint,Object obj){
//通常后置通需,需要拿到返回的结果再进行下面的操作
System.out.println("后置通知 返回的值为"+obj);
}
/**
* 环绕通知
*/
public Object myAround(ProceedingJoinPoint joinPoint) {
System.out.println("执行前 ");
Object obj= null;
try { //也可以抛出
//执行目标方法
obj= joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("执行后");
return obj;
}
/**
* 异常通知
*/
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知 : " + e.getMessage());
}
/**
* 最终通知
*/
public void myAfter(JoinPoint joinPoint){
System.out.println("最终通知");
}
}
//4创建xml
<!--使用框架-->
<!--1.目标类-->
<bean id="userServcie" class="com.javacto.day03.c_aspectj_xml.UserServcieImpl"></bean>
<!--2.切面类-->
<bean id="myAspect" class="com.javacto.day03.c_aspectj_xml.MyAspect"></bean>
<!--3.aop编程 采用框架
<aop:aspect ref="myAspect"> 加上它就是说,我这里采用aspect框架
-->
<aop:config>
<aop:aspect ref="myAspect">
<aop:pointcut id="myPointcut" expression="execution(* com.javacto.day03.c_aspectj_xml.*.*(..))"/>
<!-- 3.1前置通知 -->
<!--<aop:before method="myBefore" pointcut-ref="myPointcut"></aop:before>-->
<!--后置通知-->
<!-- <aop:after-returning method="myAfterRetuning" pointcut-ref="myPointcut" returning="obj"></aop:after-returning>-->
<!--环绕通知-->
<!--<aop:around method="myAround" pointcut-ref="myPointcut"></aop:around>-->
<!--异常-->
<aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing>
<!--最终-->
<aop:after method="myAfter" pointcut-ref="myPointcut"></aop:after>
</aop:aspect>
</aop:config>
//5创建测试类
public class Test03 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans0003.xml");
//半自动,调用者,如果不需要使用AOP又要去修改一次代码
UserServcie userServcie = (UserServcie) ac.getBean("userServcie");
userServcie.addUser();
userServcie.deleteUser();
userServcie.updateUser();
}
}
六、使用注解
1.导入Aspectj的jar包并且创建xml
2创建service接口和实现类
3创建代理类
4创建测试类
//1.导入Aspectj的jar包并且创建xml
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!--扫描装配bean 哪里的代码不需写xml配置文件了-->
<context:component-scan base-package="com.javacto.day04"></context:component-scan>
<!--aop注解生效-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
//2创建service接口和实现类
public interface UserServcie {
public void addUser();
public String deleteUser();
public void updateUser();
}
@Component
public class UserServcieImpl implements UserServcie {
public void addUser(){
System.out.println("添加用户a");
}
public String deleteUser(){
System.out.println("删除用户a");
int i = 1/0;
return "我返回的值";
}
public void updateUser(){
System.out.println("修改用户a");
}
}
//3创建代理类
@Component
@Aspect
public class MyAspect {
/**
* 前置通知
* */
// @Before("execution(* com.javacto.day04.a_aspectj_anno.UserServcieImpl.updateUser())")
public void myBefore(JoinPoint joinPoint){
//这里面是可以有业务代码的,可以new其它类的对象,调用方法等等
//joinPoint.getSignature().getName() 得到我所执行的方法名
System.out.println("前置通知"+joinPoint.getSignature().getName());
}
//声明一个公共的切入点
@Pointcut("execution(* com.javacto.day04.a_aspectj_anno.*.*(..))")
public void myPointCut(){
}
/**
* 后置通知
*/
// @AfterReturning(value = "myPointCut()",returning = "obj")
public void myAfterRetuning(JoinPoint joinpoint,Object obj){
//通常后置通需,需要拿到返回的结果再进行下面的操作
System.out.println("后置通知 返回的值为"+obj);
}
/**
* 环绕通知
*/
//@Around(value = "myPointCut()")/
// @Around("myPointCut()")
public Object myAround(ProceedingJoinPoint joinPoint) {
System.out.println("执行前 ");
Object obj= null;
try { //也可以抛出
//执行目标方法
obj= joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("执行后");
return obj;
}
/**
* 异常通知
*/
@AfterThrowing(value = "myPointCut()",throwing = "e")
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知 : " + e.getMessage());
}
/**
* 最终通知
*/
@After("myPointCut()")
public void myAfter(JoinPoint joinPoint){
System.out.println("最终通知");
}
}
//4创建测试类
public class Test04 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans04.xml");
//半自动,调用者,如果不需要使用AOP又要去修改一次代码
UserServcie userServcie = (UserServcie) ac.getBean("userServcieImpl");
userServcie.addUser();
userServcie.deleteUser();
userServcie.updateUser();
}
}