springAOP
一,AOP
1,面向切面编程 Aspect Oriented Programming
2,编程思想的发展路程
① Logic java: java 逻辑编程
② OOP : 面向对象编程
③ OIP : interface 面向接口编程
④ 面向配置文件编程
以上的思想, 都是逐步升级的概念
⑤ AOP 在OOP的基础上,增强了OOP的功能
3, 实现方式
① 基于配置(xml)
② 基于注解
4、代码案例
(1)配置版
创建第一个类
package com.bh.dao;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.springframework.stereotype.Repository;
public class EmpDAO {
  public int save(){
      System.out.println("保存了一条 emp 数据");
      return 1;
  }
}
创建第二个类
package com.bh.dao;
import org.springframework.stereotype.Repository;
public class DeptDAO {
    public int save(){
        System.out.println("保存了一条 dept 数据");
        return 1;
    }
    public int remove(){
        System.out.println("删除了 dept 的数据");
        return 1;
    }
}
创建增强类
package com.bh.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import java.util.Date;
public class LogAdvice {
    public int around(JoinPoint joinPoint) throws Throwable{
        System.out.println("start=========" + new Date());
       Integer signature = joinPoint.getSignature().getModifiers();
        System.out.println("end=========" + new Date());
        return signature;
    }
}
配置applicationContext.xml文件
<?xml version="1.0" encoding="utf-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		">
	<bean id="dept" class="com.bh.dao.DeptDAO"></bean>
	<bean id="emp" class="com.bh.dao.EmpDAO"></bean>
	<bean id="logadvice" class="com.bh.aop.LogAdvice"></bean>
	<!--配置增强类-->
	<aop:config>
		<aop:aspect ref="logadvice">
			<!--<aop:around method="around" pointcut="execution(* com.bh.dao.*.*(..))"></aop:around>-->
			<!--execution(* com.bh.dao.*.*(..))为切入点表达式:表示要增强的方法-->
			<aop:before method="around" pointcut="execution(* com.bh.dao.*.*(..))"></aop:before><!--before为前置增强-->
		</aop:aspect>
	</aop:config>
</beans>
测试类
package com.bh.test;
import com.bh.dao.DeptDAO;
import com.bh.dao.EmpDAO;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        DeptDAO dept = (DeptDAO) ac.getBean("dept");
        //dept.save();
        //dept.remove();
        EmpDAO emp = (EmpDAO) ac.getBean("emp");
        emp.save();
    }
}
结果
start=========Sat Jun 03 15:01:30 CST 2023
end=========Sat Jun 03 15:01:30 CST 2023
保存了一条 emp 数据
创建带注解的类
package com.bh.dao;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.springframework.stereotype.Repository;
@Repository
public class EmpDAO {
  public int save(){
      System.out.println("保存了一条 emp 数据");
      return 1;
  }
}
创建带注解的类
package com.bh.dao;
import org.springframework.stereotype.Repository;
@Repository
public class DeptDAO {
    public int save(){
        System.out.println("保存了一条 dept 数据");
        return 1;
    }
    public int remove(){
        System.out.println("删除了 dept 的数据");
        return 1;
    }
}
创建带注解的切面增强类
package com.bh.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@Aspect//这个注解说明此类为切面类
public class LogAdvice1 {
    //前置通知
  /*  @Before("execution(* com.bh.dao.*.*(..))")
    public void before1(JoinPoint jp) {
        System.out.println("method start =========" + new Date());
    }*/
    @Around("execution(* com.bh.dao.*.*(..))")//@Around表示环绕增强(其他同理)、execution(* com.bh.dao.*.*(..))切入点表达式
    public int around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("start=========" + new Date());
        Integer proceed = (Integer) proceedingJoinPoint.proceed();
        System.out.println("end=========" + new Date());
        return proceed;
    }
}
配置applicationAutoContext.xml文件
<?xml version="1.0" encoding="utf-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		">
	<!--基于注解AOP:有效-->
	<aop:aspectj-autoproxy/>
	<!--开启spring扫描注解-->
	<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="com.bh"></context:component-scan>
</beans>
测试类
package com.bh.test;
import com.bh.dao.DeptDAO;
import com.bh.dao.EmpDAO;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
    public static void main(String[] args) {
        //读取配置文件
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationAutoContext.xml");
     //调用方法获得类实例
        DeptDAO bean = ac.getBean(DeptDAO.class);
        bean.save();
    }
}
结果
start=========Sat Jun 03 15:10:16 CST 2023
保存了一条 dept 数据
end=========Sat Jun 03 15:10:16 CST 2023

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号