简体   繁体   中英

Adding new Aspects to Spring PetClinic

In tried to add a new aspect class to the aspects package in org.springframework.samples.petclinic.

My aspect class is as follows:

package org.springframework.samples.petclinic.aspects;

import org.apache.openjpa.jdbc.sql.Join;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.samples.petclinic.context.SessionContext;

import java.util.Date;

@Aspect
public class MethodLogAspect {

    Logger logger = LoggerFactory.getLogger(MethodLogAspect.class);

    @Pointcut("execution(* org.springframework.samples..*.*(..))")
    public void methodLogging(){}

    @Before("methodLogging()")
    public void logMethodStart(JoinPoint joinPoint){

        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getName();
        logger.info("class name: "+className+"invoked method:"+methodName+" at "+ ((new Date()).getTime()));
    }

    @After("methodLogging()")
    public void logMethodEnd(JoinPoint joinPoint){
                String methodName = joinPoint.getSignature().getName();
                String className = joinPoint.getTarget().getClass().getName();
                logger.info("class name: "+className+"finished invoking method:"+methodName+" at "+ ((new Date()).getTime()));
    }

}

I then went ahead and aspect in the the aop.xml in /resources/META-INF as follows:

<?xml version="1.0"?>

<!-- Custom aspects for the PetClinic sample application -->
<aspectj>

    <weaver>
        <include within="org.springframework.samples.petclinic..*"/>
    </weaver>

    <aspects>
        <aspect name="org.springframework.samples.petclinic.aspects.UsageLogAspect"/>
        <aspect name="org.org.springframework.samples.petclinic.aspects.MethodLogAspect"></aspect>
        <concrete-aspect name="org.springframework.samples.petclinic.aspects.ApplicationTraceAspect"
                extends="org.springframework.samples.petclinic.aspects.AbstractTraceAspect">
            <pointcut name="traced" expression="execution(* org.springframework.samples..*.*(..))"/>
        </concrete-aspect>
    </aspects>

</aspectj>

When I build the war and deploy it, none of the output specified in my aspect shows up in the log. I am not sure exactly which step I am missing here. I also feel like I do not understand the mechanics of how everything is tied together. Can somebody point out what I am missing and give me a nudge in the right direction.

Thanks

Edit:

I was able to fix this issue by adding the bean(aspect) to the applicationContext-jdbc.xml in webapp/WEB-INF/spring folder. I am not sure why this would work? Can somebody provide me with an explanation? -Thanks

I am not sure of Aspectj weaving configurations but in Spring AOP you could use

<aop:aspectj-autoproxy/> 

to enable autodetection of @Aspect annotations . Read http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM