• spring 中AOP的基本知识点


    首先AOP就是一个动态代理,主要运用在事务控制,日志记录,安全控制等方面

    1.连接点(Joinpoint):一个连接点 总是 代表一个方法的执行.

    2.切入点(Pointcut):匹配连接点的 表达式

    3.通知(Advice):连接点执行的动作  包括 执行前 执行后 环绕  

        通知的类型分为五种: 前置通知    返回后通知     抛出异常后通知     后通知    环绕通知

    4.切面(Aspect): 连接点+切入点+通知=切面

    5.目标对象(Target Object):就是委托类对象

    6.织入(Weaving):动态代理的过程

    使用xml配置AOP 实现添加日志操作

    XML配置:

    applicationContext-action.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    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" >
      <bean id="userAction" class="com.cdsxt.action.UserAction" scope="prototype" >
        <property name="userService" ref="userService" />
      </bean>
    </beans>

    applicationContext-service.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    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" >
      <bean id="userService" class="com.cdsxt.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao" />
      </bean>
    </beans>

    applicationContext-dao.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    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" >
      <bean id="userDao" class="com.cdsxt.dao.impl.UserDaoImpl" parent="baseDao" ></bean>
    </beans>

    applicationContext-resource.xml:一般把base资源性配置和公共性配置(比如连接数据库)配置在这里面,AOP的配置也在这里面

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd" >
      <bean id="baseDao" class="com.base.impl.BaseDaoImpl" lazy-init="true" ></bean>
      <!-- 用aop完成以下逻辑:
        执行service 层 所有类 的 add 方法 后 添加日志操作
        步骤如下:
        1:写一个类 并 在applicationContext-resource.xml内配置
        2: 写aop:config
            配置切入点
            配置切面 ref="自定义的通知类"
            配置通知类型
      -->
      <bean id="logAdvice" class="com.cdsxt.advice.LogAdvice" />
      <aop:config >
        <aop:pointcut expression="execution(public * com.cdsxt.service.impl.*.add*(..))" id="logCut"/>
          <aop:aspect ref="logAdvice"> 

          <!-- <aop:after method="addLog" pointcut-ref="logCut" />
            <aop:before method="addBefore" pointcut-ref="logCut"/>
            <aop:around method="addAround" pointcut-ref="logCut"/> -->
            <aop:after-returning method="addReturn" pointcut-ref="logCut"/>

          </aop:aspect>
      </aop:config>
    </beans>

    base:

    public interface BaseDao<T> {}

    public class BaseDaoImpl<T> implements BaseDao<T> {
      private Class clazz;
      public BaseDaoImpl() {
        ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
        clazz=(Class) type.getActualTypeArguments()[0];
      }
      public Class getClazz() {
        return clazz;
      }
      public void setClazz(Class clazz) {
        this.clazz = clazz;
      }
    }

    action:

    public class UserAction {
      private UserService userService;
      public void add(){
        System.out.println("======UserAction=======");
        userService.add();
      }
      public UserService getUserService() {
        return userService;
      }
      public void setUserService(UserService userService) {
        this.userService = userService;
      }
      public static void main(String[] args) {
        String[] rs = new String[]{"applicationContext-action.xml",
                      "applicationContext-dao.xml","applicationContext-resource.xml","applicationContext-service.xml"};
        ApplicationContext context = new ClassPathXmlApplicationContext(rs);
        UserAction u1= (UserAction) context.getBean("userAction");
        u1.add();
      }
    }

    service:

    public interface UserService {
      public void add();
    }

    public class UserServiceImpl implements UserService{
      private UserDao userDao;
      @Override
      public void add() {
        System.out.println("===========UserServiceImpl==========");
        userDao.add();
      }
      public UserDao getUserDao() {
        return userDao;
      }
      public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
      }
    }

    dao:

    public interface UserDao extends BaseDao<User>{
      public void add();
    }

    public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{
      @Override
      public void add() {
        System.out.println("======UserDaoImpl========");
      }
    }

    po:

    public class User {}

    advice:

    public class LogAdvice {
      public void addReturn(){

        System.out.println("正常返回后通知!!!");
      }
      public void addLog(){
        System.out.println("完成了日志操作!!!");
      }
      public void addBefore(JoinPoint jp){
        System.out.println("权限判断!!!!");
        System.out.println(jp.getTarget().getClass());
      }
      public void addAround(ProceedingJoinPoint pjp) {
        try {
          System.out.println("环绕执行前");
          pjp.proceed();
          System.out.println("环绕执行后");
        } catch (Throwable e) {
          e.printStackTrace();
        }
      }

    }

  • 相关阅读:
    【项目】项目1
    Python脚本1
    Python基础24
    Python基础23(习惯)
    01-Spring(1)
    12-Shell(2)
    11-Shell(1)
    10-搭建EE环境
    09-常用指令(3)
    08-常用指令(2)
  • 原文地址:https://www.cnblogs.com/hwgok/p/5333657.html
Copyright © 2020-2023  润新知