spring中同时注解responseBody和自定义aop,但是aop不起不作用

道秋adol 。。。 项目经理  2015-10-18 10:35:03
代码如下
controller代码如下:

package com.test.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.test.annotation.Log;
import com.test.bean.User;
import com.test.service.IUserService;

@Controller
@RequestMapping("/test")
public class TestController {

@Autowired
private IUserService userService;


@RequestMapping("/aop1")
@Log(name = "你访问的你aop1方法")
@ResponseBody
public String aop1(HttpServletResponse response) throws IOException {
System.out.println("调用action aop1");
// response.setHeader("Content-Type", "text/html;charset=UTF-8");
// response.getWriter().print("apo1");
// response.getWriter().flush();
return "aop1";
}

@Log(name = "您访问了aop2方法")
@RequestMapping(value = "aop2")
@ResponseBody
public String aop2(String string) throws InterruptedException {
System.out.println("调用action aop2");
Thread.sleep(1000L);
User user = new User();
user.setName(string);
userService.save(user);
return string;
}
}


自定义Log代码

package com.test.annotation;

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
@Documented
@Inherited
public @interface Log {
String name() default "";
}


package com.test.aop;

import java.lang.reflect.Method;
import java.util.UUID;

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

import com.test.annotation.Log;

@Aspect
@Component
public class LogAop_1 {

ThreadLocal<Long> time = new ThreadLocal<Long>();

ThreadLocal<String> tag = new ThreadLocal<String>();

/**
* 在所有标注@Log的地方注入
*
* @param joinPoint
* */
@Before("@annotation(com.test.annotation.Log)")
public void beforeExce(JoinPoint joinPoint) {
time.set(System.currentTimeMillis());
tag.set(UUID.randomUUID().toString());
info(joinPoint);
MethodSignature ms=(MethodSignature) joinPoint.getSignature();
Method method=ms.getMethod();
System.out.println(method.getAnnotation(Log.class).name()+"标记"+tag.get());
}

@After("@annotation(com.test.annotation.Log)")
public void afterExce(JoinPoint joinPoint){
MethodSignature ms=(MethodSignature) joinPoint.getSignature();
Method method=ms.getMethod();
System.out.println("标记为"+tag.get()+"的方法"+method.getName()+"运行消耗"+(System.currentTimeMillis()-time.get())+"ms");
}

@Around("@annotation(com.test.annotation.Log)")
public void aroundExce(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("我是LogAop_1");
System.out.println("我是Around,来打酱油的");
joinPoint.proceed();
}
private void info(JoinPoint joinPoint) {
System.out.println("--------------------------------------------------");
System.out.println("King:\t" + joinPoint.getKind());
System.out.println("Target:\t" + joinPoint.getTarget().toString());
Object[] os = joinPoint.getArgs();
System.out.println("Args:");
for (int i = 0; i < os.length; i++) {
System.out.println("\t==>参数[" + i + "]:\t" + os[i].toString());
}
System.out.println("Signature:\t" + joinPoint.getSignature());
System.out.println("SourceLocation:\t" + joinPoint.getSourceLocation());
System.out.println("StaticPart:\t" + joinPoint.getStaticPart());
System.out.println("--------------------------------------------------");
}
}

其中的mode

package com.test.bean;

public class User {

private String name;

public String getName() {
return name;
}

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

}

...全文
435 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
道秋adol 2016-11-23
其实在aop中的Around这个注解方法,应该加上Object返回值,返回return

 @Around("@annotation(com.test.annotation.Log)")
    public Object aroundExce(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("我是LogAop_1");
        System.out.println("我是Around,来打酱油的");
       return  joinPoint.proceed();
    }
回复
ruchiruzui2012 2016-09-28
我好像也遇到这样的问题了,我的是controller走,数据读出来没问题,该扫描的都扫描进去了,该注入的都注入进去了,但是@Before注解的主要日志方法不走(无反应),我的controller也有@RequestBody注解
回复
stk1112 2016-03-30
我也遇到了这样的问题,请问楼主怎么解决的? 加上@Log(name="xxx")时收不到返回值,去掉之后前台收到了。
回复
道秋adol 2015-10-18
在response中显示“failed to load response data”,
回复
道秋adol 2015-10-18
但是加了Log(name = "你访问的你aop1方法")之后,访问aop1的时候,浏览器上打印不出 “aop1”的字样,但是访问是200成功,
回复
道秋adol 2015-10-18
其中controller方法的注解如果没加上自定义aop Log注解,显示完全正常,访问也正常,
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-10-18 10:35
社区公告
暂无公告