Results 1 to 4 of 4

Thread: Aspect does not applied

  1. #1
    Join Date
    Nov 2009
    Posts
    7

    Default Aspect does not applied

    I want use annotation and aspect to make an logging function:
    annotation:
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;

    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface LogInfo {
    String operationName();
    }

    Aspect:
    @Aspect
    public class LogAspect {
    @Resource
    private LogDao logDao;
    @Pointcut(value="@annotation(logInfo)",argNames="l ogInfo")
    public void methodWithLogInfo(LogInfo logInfo) {
    }
    @Around("methodWithLogInfo(logInfo)")
    public Object writeLog(ProceedingJoinPoint point,LogInfo logInfo) throws Throwable {
    HttpServletRequest request = null;
    Object[] args = point.getArgs();
    for (int i = 0; i < args.length; i++) {
    if (args[i] instanceof HttpServletRequest) {
    request = (HttpServletRequest) args[i];
    }
    }
    CmsUser currUser = CmsUtils.getUser(request);
    Set<CmsRole> roles=currUser.getRoles();
    String usertype="";
    for(CmsRole role:roles){
    usertype=role.getName()+" ";
    }
    Sys_log log=new Sys_log();
    log.setHandle(logInfo.operationName());
    log.setIntime(new Date());
    log.setUserid(currUser.getId());
    log.setUsertype(usertype);
    logDao.save(log);
    return point.proceed();
    }

    }


    using palce:
    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    @LogInfo(operationName = "用户登录")
    public String submit(String username, String password, String captcha,
    String processUrl, String returnUrl, String message,
    HttpServletRequest request, HttpServletResponse response,
    ModelMap model) {
    }


    application-context.xml
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:unis="http://www.springframework.org/schema/unis" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-lazy-init="true">
    <aop:aspectj-autoproxy/>
    </beans>



    when I invoke public String submit() method ,nothing happened,some one help me to applied aspect to the function!tks!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Use [ code][/code ] tags when posting code that way it remains readable...

    Why should your aspect be applied? You haven't configured/added your aspect to your application-context next to that you want to apply it to a controller which is loaded by the childcontext (DispatcherServlet) and not the root context (ContextLoaderListener) and aspect/aop only gets applied on the same context it it configured in...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2009
    Posts
    7

    Default

    tks,it worked using your way!

  4. #4
    Join Date
    Nov 2009
    Posts
    7

    Default

    Quote Originally Posted by Marten Deinum View Post
    Use [ code][/code ] tags when posting code that way it remains readable...

    Why should your aspect be applied? You haven't configured/added your aspect to your application-context next to that you want to apply it to a controller which is loaded by the childcontext (DispatcherServlet) and not the root context (ContextLoaderListener) and aspect/aop only gets applied on the same context it it configured in...

    you teached me how to use aop!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •