Spring AOP笔记

首先,web.xml配置一个listener

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


其实,使用默认的springListener配置文件路径:WEB-INF/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<aop:aspectj-autoproxy/>
<bean id="logAspect" class="com.amhuman.comm.LogAspect"></bean>
<bean id="userService" class="com.amhuman.service.UserServiceImpl" />
<bean class="com.amhuman.comm.SpringHelper"></bean>
</beans>


接下来就是编写切面了(这里还用到了自定义的注解,下面会补上代码)

package com.amhuman.comm;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;

@Aspect
public class LogAspect {

@After("execution(public * *(..))")
public void afterSayHello(){
System.out.println("After : ");
}

@Before("execution(public * *(..))")
public void beforeSayHello(){
System.out.println("Before : ");
}

@Around("execution(public * *(..)) && @annotation(com.amhuman.comm.Log)")
public Object doLog(ProceedingJoinPoint pjp) throws Throwable{
String param = "";
Log logAnno = getAnno(pjp);
if(logAnno.id() != -1){
Object obj = pjp.getArgs()[logAnno.id()];
if(logAnno.fieldName() == ""){
param = obj.toString();
}else{
String fieldName = logAnno.fieldName();
Method getMethod = obj.getClass().getMethod("getUserName", null);
System.out.println(obj.getClass().toString());
System.out.println(getMethod.toString());
Object objddd = getMethod.invoke(obj, null);
System.out.println(objddd);
// param = getMethod.invoke(obj, null).toString();
}
}
System.out.println("#before asp : "+ param);
Object ret = pjp.proceed();
System.out.println("#aftor asp");
return ret;
}

private Log getAnno(ProceedingJoinPoint pjp) throws SecurityException, NoSuchMethodException{
Class<?> currClass = pjp.getTarget().getClass();
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
String shortMethodName = method.getName();
Method realMethod = currClass.getMethod(shortMethodName, method.getParameterTypes());
Log logAnnotation = realMethod.getAnnotation(Log.class);
return logAnnotation;
}
}


注解的代码:

package com.amhuman.comm;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Log {
public int id() default -1;
public String fieldName() default "";
}


写个单元测试跑下:

package com.amhuman.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.amhuman.entity.User;
import com.amhuman.service.IUserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-test.xml" })
public class SpringTest {

@Resource
IUserService userService;

public IUserService getUserService() {
return userService;
}

public void setUserService(IUserService userService) {
this.userService = userService;
}

@Test
public void testUser(){
System.out.println(userService.getDefaultUser().getUserName());
System.out.println("================================");
System.out.println(userService.findById(100).getUserName());
System.out.println("================================");
System.out.println(userService.find(new User()).getUserName());
}

}


对应的service代码

package com.amhuman.service;

import org.springframework.util.StringUtils;

import com.amhuman.comm.Log;
import com.amhuman.entity.User;

public class UserServiceImpl implements IUserService {
private String userName;

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

/* (non-Javadoc)
* @see com.amhuman.service.IUserService#getDefaultUser()
*/
@Override
public User getDefaultUser(){
System.out.println("###");
User user = new User();
user.setUserId(1001);
if(StringUtils.hasLength(this.userName)){
user.setUserName(this.userName);
}else{
user.setUserName("WUYC-TEST");
}
return user;
}

@Override
@Log(id = 0,fieldName="userId")
public User find(User user) {
if(user == null){
return getDefaultUser();
}
user.setUserName("WUTIANYU");
return user;
}

@Override
@Log(id = 0)
public User findById(Integer id) {
if(id == null){
return getDefaultUser();
}
User user = new User();
user.setUserId(id);
user.setUserName("UserName["+id+"]");
return user;
}
}



搞定!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值