使用spring註解方式實現AOP 二

2021-08-31 08:11:28 字數 1672 閱讀 6715

如果需要對業務方法中的引數和返回值做處理的情況下:

package com.chris.aop;

import org.springframework.stereotype.service;

@service("testservice")

public class testservicebean

public string update(string string)

public void add(string name,int age)

}

切面類:

package com.chris.aop;

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;

@component//一定要將該類交給spring容器管理

@aspect//宣告該類為乙個切面

public class interceptordemo ;//切入點的名稱就是方法名

@before("allmethod() && args(name,***)")//定義切入點allmethod()中方法引數為(string,int)的方法的前置通知

public void dosomethingbefore(string name,int ***)

@after("allmethod()")//定義切入點allmethod()的後置通知

public void dosomethingafter()

@afterreturning(pointcut="allmethod()",returning="update")//定義切入點allmethod()中返回值為string的方法的最終通知

public void dosomethingfinally(string update)

@afterthrowing(pointcut="allmethod()",throwing="ex")//定義切入點allmethod()中丟擲exception異常的方法的異常通知

public void dosomethinginex(exception ex)

@around("allmethod()")//定義切入點allmethod()的環繞通知,環繞通知方法一定要按照下面的形式定義(只可以修改方法名和引數名)

public object dosomethingaround(proceedingjoinpoint pjp) throws throwable

}

使用註解方式配置Spring

在配置檔案中新增如下 spring會自動掃瞄改包下的所有註解 component user 當分不清時用它 controller user web層 service user service層 repository dao層 不寫名稱預設是類名小寫 scope scopename prototype...

註解方式配置Spring實現Ioc

1.首先需要配置spring,支援註解 加上這三句話在beans中 配置xml命名空間 xmlns context spring context 4.1.xsd 提示一下 xsd檔案,是用來約束xml檔案的語法和格式 約束xml檔案,有兩種標準 dtd dtd schema xsd 2.初始化和裝配...

spring基於註解方式實現事務

環境搭建 資料來源,資料庫驅動 spring jdbc模組org.springframework groupid spring jdbc artifactid 4.3.12 release version dependency c3p0 groupid c3p0 artifactid 0.9.1 2...