β

Spring 学习(四) 基于注解的aop配置

码农日记 138 阅读

实体对象 Person.java

package cn.ytu.aop.annocation;

public class Person {

private String id;
private String name;

public Person() {
}

public Person(String id, String name) {
super();
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

定义PersonDaoImpl模拟实现数据库操作的类

package cn.ytu.aop.annocation;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

@Repository(value = "personDao")
public class PersonDaoImpl {

public void add() {
System.out.println("add person");

}

public void update() {
System.out.println("update person");

}

public void delete() {
System.out.println("delete person");

}

public List listAll() {
Person p1 = new Person("1001", "person1");
Person p2 = new Person("1002", "person2");
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
return list;
}

public Person find() {
Person p1 = new Person("1001", "person1");
return p1;
}
}

定义Transaction类,模拟数据库操作中的事务管理

import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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;
import org.springframework.stereotype.Component;

@Aspect
@Component(value = "transaction")
public class Transaction {

/**
* @Component==<bean id="transaction" class="..Transaction">
* @Aspect 标注切面类 == <aop:config> </aop:config> 标注切入
*         @Pointcut("execution(*cn.yty.aop.annotation.PersonDaoImpl.*(..))"
*         ) private void aa(){}//切入点签名(方法名称可以任意取) 相当于配置文件中的<aop:pointcut
*         expression
*         ="execution(* cn.ytu.aop.annotation.PersonDaoImpl.*(..))"
*         id="aa()"/>
* @Pointcut 标注一个切入点
*/

@Pointcut("execution(* cn.ytu.aop.annocation.PersonDaoImpl.*(..))")
private void aa() {
}

@Before("aa()")
// 标注前置通知
public void beforeMethod() {
System.out.println("before Method");
}

@AfterReturning(value = "aa()", returning = "val")
// //标注后置通知
public void afterMethod(JoinPoint joinPoint, Object val) {
List<Person> list = (List<Person>) val;
for (Person p : list) {
System.out.println(p.getName());
}
}

@After("aa()")
// 标注最终通知
public void finalMeth() {
System.out.println("final method");
}

@Around("aa()")
// 标注环绕通知
public void arroundMethod(ProceedingJoinPoint joinPoint) {
try {
joinPoint.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@AfterThrowing(value = "aa()", throwing = "ex")
public void throwMethod(Throwable ex) {
System.out.println(ex.getMessage());
}
}

 

配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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">

<!-- aop的注解解析器 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 类扫描的注解解析器 -->
<context:component-scan base-package="cn.ytu.aop.annocation"></context:component-scan>
<!-- 1、导入springAOP的注解解析器 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
、导入类扫描的注解解析器 <context:component-scanbase-package="cn.itcast.spring0401.aop.annotation">
</context:component-scan> -->
<!-- 原理: 当启动spring容器的时候,spring容器解析配置文件 *就会解析到类扫描的注解解析器,会在base-package指定的包及子包中扫描所有的类,看类上是否有@Compontent,@Service,@Controller,@Repository注解,
*如果有,则spring容器创建该类的实例 *解析到aop的注解解析器,会在纳入spring管理的bean中,看哪个类上面是否有@Aspect注解。如果有,
则会在方法中查找@Pointcut,就会找到切入点表达式,根据切入点表达式,在纳入spring范围的bean内查找,
看哪个bean符合切入点表达式,如果符合则创建代理对象当客户端访问某一个bean时,如果该bean有代理对象,则返回代理对象,否则返回该bean的对象 -->

</beans>
作者:码农日记
Android, iOS, HTML5, Cloud and more
原文地址:Spring 学习(四) 基于注解的aop配置, 感谢原作者分享。

发表评论