spring AOP 之 註解 配置實現(附 Java 程式碼例項)
阿新 • • 發佈:2019-01-20
轉載自http://blog.csdn.net/qq_27093465/article/details/53381527
匯入類掃描的註解解析器名稱空間:xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
引號新增這2個url
xml配置檔案如下配置:<context:component-scan base-package="com.lxk.spring.aop.annotation"/>
匯入springAOP的註解解析器
名稱空間:xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
引號新增這2個url
xml配置檔案如下配置:<aop:aspectj-autoproxy/>
上面的2個解析器的前後順序不分先後。
介面類,目標介面
- package com.lxk.spring.aop.annotation;
-
import
- publicinterface PersonDao {
- void deletePerson();
- List<Person> getPerson() throws Exception;
- void savePerson();
- void updatePerson();
- }
- package com.lxk.spring.aop.annotation;
- import com.google.common.collect.Lists;
-
import org.springframework.stereotype.Repository;
- import java.util.List;
- /**
- * Created by lxk on 2016/11/28
- */
- @Repository(value = "personDaoImpl")
- publicclass PersonDaoImpl implements PersonDao {
- publicvoid deletePerson() {
- System.out.println("delete perosn");
- }
- public List<Person> getPerson() throws Exception {
- List<Person> personList = Lists.newArrayList();
- Person person1 = new Person();
- person1.setPid(1L);
- person1.setPname("person1");
- //int a = 1 / 0;
- System.out.println("get person");
- personList.add(person1);
- Person person2 = new Person();
- person2.setPid(2L);
- person2.setPname("person2");
- personList.add(person2);
- return personList;
- }
- publicvoid savePerson() {
- System.out.println("delete perosn");
- }
- publicvoid updatePerson() {
- System.out.println("delete perosn");
- }
- }
- package com.lxk.spring.aop.annotation;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.*;
- import org.springframework.stereotype.Component;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 切面類
- * <p>
- * Created by lxk on 2016/11/28
- */
- @Aspect
- @Component(value = "transaction")
- publicclass Transaction {
- @Pointcut("execution(* com.lxk.spring.aop.annotation.PersonDaoImpl.*(..))")
- privatevoid aa() {
- }//切入點簽名
- /**
- * 前置通知
- */
- @Before("aa()")
- publicvoid beforeMethod() {
- System.out.println("before method");
- }
- /**
- * 後置通知
- */
- @AfterReturning(value = "aa()", returning = "val")
- publicvoid afterMethod(JoinPoint joinPoint, Object val) {
- List<Person> personList = (ArrayList<Person>) val;
- if (personList != null && !personList.isEmpty()) {
- for (Person person : personList) {
- System.out.println(person.getPname());
- }
- }
- System.out.println("after method");
- }
- /**
- * 最終通知
- */
- @After("aa()")
- publicvoid finalMethod(JoinPoint joinPoint) {
- joinPoint.getArgs();
- System.out.println("final method");
- }
- /**
- * 環繞通知
- */
- @Around("aa()")
- public Object aroundMethod(ProceedingJoinPoint joinPoint) {
- Object obj = null;
- try {
- obj = joinPoint.proceed();
- } catch (Throwable e) {
- System.out.println(e.getMessage());
- }
- System.out.println("around method");
- return obj;
- }
- /**
- * 異常通知
- */
- @AfterThrowing(value = "aa()", throwing = "ex")
- publicvoid throwingMethod(Throwable ex) {
- System.out.println(ex.getMessage());
- }
- }
xml配置檔案
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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">
- <aop:aspectj-autoproxy/>
- <context:component-scanbase-package="com.lxk.spring.aop.annotation"/>
- </beans>
測試和model檔案
- package com.lxk.spring.aop.annotation;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * Created by lxk on 2016/11/28
- */
- publicclass AOPAnnotationTest {
- @Test
- publicvoid test() throws Exception {