SpingAOP案例(一)注解(Annotation)方式

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/20698675
有些类请参照上一篇文章的其它类,这里就不列举了
//这是切面类
package com.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class TestAnnotationAspect {

	
	@Pointcut("execution(* com.spring.service.*.*(..))")
    @SuppressWarnings("unused")
	private void pointCutMethod() {

		
    }  
	
	@Before("pointCutMethod()")
	 public void doBefore() {  
	        System.out.println("前置通知");  
	    }  
	 
	 //声明后置通知  
    @AfterReturning(pointcut = "pointCutMethod()", returning = "result")  
	 public void doAfterReturning(String result) {  
	        System.out.println("后置通知");  
	        result="";
	        System.out.println("---" + result + "---");  
	    }  
	 
	 //声明例外通知  
	    @AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")  
	    public void doAfterThrowing(Exception e) {  
	        System.out.println("例外通知");  
	        System.out.println(e.getMessage());  
	    }  
	    //声明环绕通知  
	    @Around("pointCutMethod()")  
	    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
	        System.out.println("进入方法---环绕通知");  
	        Object o = pjp.proceed();  
	        System.out.println("退出方法---环绕通知");  
	        return o;  
	    }  
}
//这是配置文件
<?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:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">  
        
       <!--   xml方式
    <aop:config>  
        <aop:aspect id="TestAspect" ref="aspectBean">  
           
            <aop:pointcut id="businessService" expression="execution(* com.spring.service.*.*(..))" />  
            <aop:before pointcut-ref="businessService" method="doBefore"/>  
            <aop:after pointcut-ref="businessService" method="doAfter"/>  
            <aop:around pointcut-ref="businessService" method="doAround"/>  
            <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>  
        </aop:aspect>  
    </aop:config>  
      
    <bean id="aspectBean" class="com.spring.aop.TestAspect" />  
    <bean id="aService" class="com.spring.service.AServiceImpl" ></bean>  
    <bean id="bService" class="com.spring.service.BServiceImpl"></bean>  
    -->
    
     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />  
  
    <bean id="aspectBean" class="com.spring.aop.TestAnnotationAspect" />  
    <bean id="aService" class="com.spring.service.AServiceImpl"></bean>  
    <bean id="bService" class="com.spring.service.BServiceImpl"></bean> 
</beans>  


 
 
相关文章
|
9月前
|
Java
java注解(作用于注解)
java注解(作用于注解)
84 0
|
安全 Java 编译器
一文带你全面深入理解Java注解Annotation
一文带你全面深入理解Java注解Annotation
203 1
一文带你全面深入理解Java注解Annotation
|
Java 编译器 测试技术
java注解annotation学习
java注解annotation学习
94 0
|
Java 编译器 Spring
Java注解(Annotation)的基本原理以及实现自定义注解
在我们使用springboot的时候我们知道因为注解的存在,使得我们的开发变得格外的方便、快捷。之前的文章Spring常用注解大全,值得你的收藏!!!对于spring中各类注解也进行过介绍。然而注解也并不是因为spring框架的兴起才出现的,而是很早就已经在java中被使用。
944 0
Java注解(Annotation)的基本原理以及实现自定义注解
|
开发框架 Java 编译器
Java注解Annotation小结
Java注解Annotation小结
|
前端开发 Java 应用服务中间件
曾经的你我高攀不起,现在的我只想注解和配置(下)
四、Controller配置总结 实现Controller接口 使用注解开发@Controller
曾经的你我高攀不起,现在的我只想注解和配置(下)
|
安全 前端开发 Java
曾经的你我高攀不起,现在的我只想注解和配置(上)
注解开发及配置 三、Annotation注解开发 1. 步骤 2. 总结
|
Java 开发者
自定义 Annotation | 学习笔记
快速学习 自定义 Annotation
|
Java Spring
自定义注解实现方式解析
自定义注解实现方式解析
207 0
自定义注解实现方式解析
|
前端开发 安全 Java
Java注解(Annotation):请不要小看我!(1)
Java注解(Annotation):请不要小看我!
97 0
Java注解(Annotation):请不要小看我!(1)

热门文章

最新文章