zoukankan      html  css  js  c++  java
  • Sping--AOP--Annotation

    Aspectj 概念:

    1. joinpoint:切入点, 比如@Before, @After, @Around

    2. Pointcut:切入点集合, 比如 @Pointcut("execution(public * com.bjsxt.service..*.*(..))")

      public void myMethod(){};  //切入点集合的名字

    3. Aspect:切面逻辑, 切面类, 比如LogInterceptor之前加入的@Aspect

    4. Advice: 加入点的建议, 类似于Aspect的逻辑, 相当于@Before, 加在切入点的建议.

    5. Target: 被代理对象, 逻辑织入哪里...

    6. Weave: 织入

    AOP的Annotation方式:

    1. 加上对应的xsd文件sprin-aop.xsd

    2. beans.xml <aop:aspectj-autoproxy/>

    3. 此时就可以解析对应的annotation了

    4. 建立我们的拦截类

    5. 用@Aspect注解这个类

    6. 用@Before来注解方法

    7. 写明白切入点(execution...)

    8. 让spring对我们的拦截器类进行管理 @Component

    常见的annotation:

    1. @Pointcut

    2. @Before

    3. @AfterReturning

    4. @AfterThrowing

    5. @After

    6. @Around

    beans.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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    	<context:annotation-config />
    	<context:component-scan base-package="com.bjsxt"/>
     	<aop:aspectj-autoproxy />
    </beans>
    

    LogInterceptor.java: 注意@Component

    package com.bjsxt.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LogInterceptor {	
    	@Before("execution(public * com.bjsxt.dao..*.*(..)))")
    	public void before() {
    		System.out.println("method before");
    	}
    	@AfterReturning("execution(public * com.bjsxt.dao..*.*(..)))")
    	public void AfterReturning() {
    		System.out.println("method after returning");
    	}	
    }
    

      

    简化成下面写法:

    package com.bjsxt.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LogInterceptor {
    	@Pointcut("execution(public * com.bjsxt.dao..*.*(..))")
    	public void myMethod(){};
    @Before("myMethod()") public void before() { System.out.println("method before"); }
    @AfterReturning("myMethod()") public void AfterReturning() { System.out.println("method after Returning"); } }

      

    UserServiceTest.java:

    package com.bjsxt.service;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.bjsxt.model.User;
    
    //Dependency Injection
    //Inverse of Control
    public class UserServiceTest {
    
    	@Test 
    	public void testAdd() throws Exception {
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    		UserService service = (UserService)ctx.getBean("userService");
    		System.out.println(service.getClass());
    		service.add(new User());		
    		ctx.destroy();		
    	}
    }
    

    UserService.java:

    package com.bjsxt.service;
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    
    @Component("userService")
    public class UserService {
    	
    	private UserDAO userDAO;  
    	
    	public void init() {
    		System.out.println("init");
    	}
    	
    	public void add(User user) {
    		userDAO.save(user);
    	}
    	public UserDAO getUserDAO() {
    		return userDAO;
    	}
    	
    	@Resource(name="u")
    	public void setUserDAO( UserDAO userDAO) {
    		this.userDAO = userDAO;
    	}	
    	public void destroy() {
    		System.out.println("destroy");
    	}
    }
    

    UserDAOImpl.java:

    package com.bjsxt.dao.impl;
    
    import org.springframework.stereotype.Component;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    @Component("u") 
    public class UserDAOImpl implements UserDAO {
    
    	public void save(User user) {
    		//Hibernate
    		//JDBC
    		//XML
    		//NetWork
    		System.out.println("user saved!");
    		//throw new RuntimeException("exeption!");
    	}
    
    }
    

    UserDAO.java:

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

    User.java:

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

     

    结果:

    class com.bjsxt.service.UserService
    method before
    user saved!
    method after Returning
    

      

    LogInterceptor.java的around用法:

    package com.bjsxt.aop;
    
    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.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LogInterceptor {
    	@Pointcut("execution(public * com.bjsxt.dao..*.*(..))")
    	public void myMethod(){};
    	
    	@Before("myMethod()")
    	public void before() {
    		System.out.println("method before");
    	}
    	
    	@Around("myMethod()")
    	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
    		System.out.println("method around start");
    		pjp.proceed();
    		System.out.println("method around end");
    	}
    	
    }
    

    beans.xml: XML catalog里引入 aop的 xsd

    <?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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    	<context:annotation-config />
    	<context:component-scan base-package="com.bjsxt"/>
     	<aop:aspectj-autoproxy />
    </beans>
    

    UserServiceTest.java:

    package com.bjsxt.service;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.bjsxt.model.User;
    
    //Dependency Injection
    //Inverse of Control
    public class UserServiceTest {
    
    	@Test 
    	public void testAdd() throws Exception {
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    		
    		UserService service = (UserService)ctx.getBean("userService");
    		System.out.println(service.getClass());
    		service.add(new User());
    		service.delete();
    		ctx.destroy();
    		
    	}
    
    }
    

    UserService.java:

    package com.bjsxt.service;
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    
    @Component("userService")
    public class UserService {
    	
    	private UserDAO userDAO;  
    	
    	public void init() {
    		System.out.println("init");
    	}
    	
    	public void add(User user) {
    		userDAO.save(user);
    	}
    	public void delete() {
    		userDAO.delete();
    	}
    	public UserDAO getUserDAO() {
    		return userDAO;
    	}
    	
    	@Resource(name="u")
    	public void setUserDAO( UserDAO userDAO) {
    		this.userDAO = userDAO;
    	}	
    	public void destroy() {
    		System.out.println("destroy");
    	}
    }
    

    UserDAOImpl.java:

    package com.bjsxt.dao.impl;
    
    import org.springframework.stereotype.Component;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    @Component("u") 
    public class UserDAOImpl implements UserDAO {
    
    	public void save(User user) {
    		System.out.println("user saved!");
    		//throw new RuntimeException("exeption!");
    	}
    	public void delete() {		
    		System.out.println("user delete!");
    		//throw new RuntimeException("exeption!");
    	}
    }
    

    UserDAO.java  :

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

      

    结果:

    class com.bjsxt.service.UserService$$EnhancerByCGLIB$$b6531e3e
    method around start
    method before
    user saved!
    method around end
    method around start
    method before
    user delete!
    method around end
    

      

     

    改成service的话, 因为没有实现接口, 所以需要 引入一个包: cglib-nodep-2.1_3.jar

    package com.bjsxt.aop;
    
    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.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LogInterceptor {
    	@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
    	public void myMethod(){};
    	
    	@Before("myMethod()")
    	public void before() {
    		System.out.println("method before");
    	}
    	
    	@Around("myMethod()")
    	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
    		System.out.println("method around start");
    		pjp.proceed();
    		System.out.println("method around end");
    	}
    	
    }
    

    结果:

    class com.bjsxt.service.UserService$$EnhancerByCGLIB$$9160c20d
    method around start
    method before
    user saved!
    method around end
    

      

      

      

      

      

  • 相关阅读:
    Automatically Display Menu on Hover
    WPF自学教程系列1:如何将WPF空间嵌套到Form窗口?
    NET中的内存管理,GC机制,内存释放过程. 转载
    C++ 初始化和赋值的区别
    2.尽量用const, enum, inline代替#define Prefer const, enum, inline to #define.
    1.视C++为一个语言联邦 View C++ as a federation of languages
    C++内存对齐
    C++ class和struct的区别
    redhat AS5 Samba服务配置
    windowsXP & 2003 加固
  • 原文地址:https://www.cnblogs.com/wujixing/p/5455533.html
Copyright © 2011-2022 走看看