前言

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了樂亭免費建站歡迎大家使用!
最近項目已經(jīng)開發(fā)完成,但發(fā)現(xiàn)需要加用戶操作日志,如果返回去加也不太現(xiàn)實,所以使用springAOP來完成比較合適。下面來一起看看詳細(xì)的介紹:
注解工具類:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogAnnotation {
String operateModelNm() default "";
String operateFuncNm() default "";
String operateDescribe() default "";
}切面類:
@Aspect
public class MyInterceptor {
@Pointcut("execution(** com.luchao.spring.test3.service.impl.*.*(..))")
private void anyMethod(){}//定義一個切入點
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println(name);
System.out.println("前置通知");
}
@AfterReturning("anyMethod()")
public void doAfter(){
System.out.println("后置通知");
}
@After("anyMethod()")
public void after(JoinPoint point){
System.out.println("最終通知");
}
@AfterThrowing("anyMethod()")
public void doAfterThrow(){
System.out.println("例外通知");
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
Method targetMethod = methodSignature.getMethod();
// System.out.println("classname:" + targetMethod.getDeclaringClass().getName());
// System.out.println("superclass:" + targetMethod.getDeclaringClass().getSuperclass().getName());
// System.out.println("isinterface:" + targetMethod.getDeclaringClass().isInterface());
// System.out.println("target:" + pjp.getTarget().getClass().getName());
// System.out.println("proxy:" + pjp.getThis().getClass().getName());
// System.out.println("method:" + targetMethod.getName());
Class[] parameterTypes = new Class[pjp.getArgs().length];
Object[] args = pjp.getArgs();
for(int i=0; i<args.length; i++) {
if(args[i] != null) {
parameterTypes[i] = args[i].getClass();
}else {
parameterTypes[i] = null;
}
}
//獲取代理方法對象
String methodName = pjp.getSignature().getName();
Method method = pjp.getSignature().getDeclaringType().getMethod(methodName, parameterTypes);
if(method.isAnnotationPresent(LogAnnotation.class)){
System.out.println("存在1");
}
//獲取實際方法對象,可以獲取方法注解等
Method realMethod = pjp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
if(realMethod.isAnnotationPresent(LogAnnotation.class)){
realMethod.getAnnotation(LogAnnotation.class).operateDescribe();
System.out.println("存在2");
}
System.out.println("進(jìn)入環(huán)繞通知");
Object object = pjp.proceed();//執(zhí)行該方法
System.out.println("退出方法");
return object;
}
}配置類:
@Configurable
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.luchao.spring.test3")
public class test3Config {
@Bean
public MyInterceptor myInterceptor(){
return new MyInterceptor();
}
@Bean
public EncoreableIntroducer encoreableIntroducer(){
return new EncoreableIntroducer();
}
}服務(wù)類:
@Component
public class PersonServiceBean implements PersonServer {
/**
* 保存方法
* @param name
*/
@LogAnnotation(operateModelNm = "測試方法", operateFuncNm = "保存方法")
public void save(String name) {
System.out.println("我是save方法");
}
/**
* 更新方法
* @param name
* @param id
*/
public void update(String name, Integer id) {
System.out.println("我是update()方法");
}
/**
* 獲取方法
* @param id
* @return
*/
public String getPersonName(Integer id) {
System.out.println("我是getPersonName()方法");
return "xxx";
}
}測試方法:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = test3Config.class)
public class SpringAOPTest {
@Autowired
private PersonServer personServer;
@Test
public void inteceptorTest(){
Encoreable encoreable = (Encoreable)personServer;
encoreable.performEncore();
personServer.save("test");
}
}在springAOP切面中使用的是代理,所以直接獲取的是代理對象,不能獲取真實對象的一些信息,如注解等。
//獲取代理方法對象 String methodName = pjp.getSignature().getName(); Method method = pjp.getSignature().getDeclaringType().getMethod(methodName, parameterTypes);
如果要獲取真實對象,獲取注解的信息,可以方便我們進(jìn)行判斷記錄。
//獲取實際方法對象,可以獲取方法注解等 Method realMethod = pjp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
這樣就完成了一個簡單的操作日志記錄demo。另外,如果不是講某個方法設(shè)置切點,可以ant風(fēng)格的切點切入方式,設(shè)置多個或所有方法。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。
網(wǎng)站題目:利用springAOP記錄用戶操作日志的方法示例
本文來源:http://chinadenli.net/article12/ggjhdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站建設(shè)、Google、動態(tài)網(wǎng)站、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)