zoukankan      html  css  js  c++  java
  • Spring--AOP--面向切面编程

    AOP: 面向切面编程. 通过动态代理实现.

    AOP就3条线, 2条线给剪断.

    实现:动态代理

    如果实现interface的话, 用Proxy, InvocationHandler.

    不实现interface的话, 用cglib操作二进制码实现.

    AOP用在哪里:

    性能, 日志, 权限检查, transaction.

    项目里如何使用: 声明式事务管理.

    JDK提供了两个提供动态代理的类: Proxy, InvocationHandler.

    UserServiceTest.java:

    package com.bjsxt.service;
    import java.lang.reflect.Proxy;
    import org.junit.Test;
    import com.bjsxt.aop.LogInterceptor;
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.dao.impl.UserDAOImpl;
    import com.bjsxt.model.User;
    import com.bjsxt.spring.BeanFactory;
    import com.bjsxt.spring.ClassPathXmlApplicationContext;
    public class UserServiceTest { @Test public void testProxy() { UserDAO userDAO = new UserDAOImpl(); LogInterceptor li = new LogInterceptor(); li.setTarget(userDAO); UserDAO userDAOProxy = (UserDAO)Proxy.newProxyInstance(userDAO.getClass().getClassLoader(), userDAO.getClass().getInterfaces(), li); System.out.println(userDAOProxy.getClass()); userDAOProxy.delete(); userDAOProxy.save(new User()); } }

    代理 LogInterceptor.java:

    package com.bjsxt.aop;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    public class LogInterceptor implements InvocationHandler {
    	private Object target;
    	
    	public Object getTarget() {
    		return target;
    	}
    
    	public void setTarget(Object target) {
    		this.target = target;
    	}
    
    	public void beforeMethod(Method m) {		
    		System.out.println(m.getName() + " start");
    	}
    
    	public Object invoke(Object proxy, Method m, Object[] args)
    			throws Throwable {
    		beforeMethod(m);
    		m.invoke(target, args);
    		return null;
    	}
    }
    

    UserDAOImpl.java:

    package com.bjsxt.dao.impl;
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    public class UserDAOImpl implements UserDAO {
    	public void save(User user) {	
    		System.out.println("user saved!");
    	}
    
    	public void delete() {
    		System.out.println("user deteleted");		
    	}
    }
    

    UserDAO.java:

    package com.bjsxt.dao;
    import com.bjsxt.model.User;
    
    public interface UserDAO {
    	public void save(User user);
    	public void delete();
    }
    

      

     

      

     

  • 相关阅读:
    spring boot @value 使用
    mac Navicat premium 链接oracle
    List sort 排序
    mac 配置jdk+maven环境变量
    mybatis 根据主键批量insert或update
    git 常用命令
    SpringBoot整合Activiti案例
    Activiti网关-并行网关
    Activiti网关-包含网关
    Activiti网关-排他网关
  • 原文地址:https://www.cnblogs.com/wujixing/p/5455513.html
Copyright © 2011-2022 走看看