Spring AOP和自定义注释不起作用

时间:2014-05-07 12:43:38

标签: java spring annotations aop aspectj

我正在尝试使用Spring aop和自定义注释创建审核机制。出于某些原因,在我的单元测试中,弹簧不会触发我的AOP端点。

自定义注释:

    package com.mycompany.cismanager.annotations;

import com.mycompany.cismanager.aspect.AuditingAspect;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by idanf on 04/05/2014.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface Audit {

    AuditingAspect.ActionAuditType auditType();
}

我的Aspect课程:

package com.mycompany.cismanager.aspect;

@Aspect
public class AuditingAspect {


    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AuditingAspect.class);


    public enum ActionAuditType {
        EXTERNAL_USER_REGISTRATION("Register external user"),

        EXTERNAL_USER_MODIFICATION("Modify external user");


        private String mDescription;

        private ActionAuditType(String aDescription) {
            mDescription = aDescription;
        }


        public String getDescription() {
            return mDescription;
        }

        public void setDescription(String aDescription) {
            mDescription = aDescription;
        }
    }


    @Before(value = "execution(@com.mycompany.cismanager.annotations.Audit * *(..)) && @annotation(audit)", argNames = "audit")
    public void logTheAuditActivity(JoinPoint aPoint, Audit audit) {
        //  String userName = getUserName();
        logger.info("Parms Auditing User Name: " + audit.auditType().getDescription());
        //   mlogger.info("auditType:" + parmsAudit.auditType().getDescription());

        String arguments = getArgs(aPoint.getArgs());

        logger.debug("arguments=" + arguments);

        // if (arguments.length() > 0) {
        //      mlogger.info("args-" + arguments);
        //   }
    }

    private String getArgs(Object[] args) {
        String arguments = "";
        int argCount = 0;

        for (Object object : args) {
            if (argCount > 0) {
                arguments += ", ";
            }
            arguments += "arg[" + ++argCount + "]=" + "[" + object + "]";
        }
        return arguments;
    }

    private String getUserName() {
        try {
            return SecurityContextHolder.getContext().getAuthentication().getName();
        } catch (NullPointerException npe) {
            return "Unknown User";
        }
}

和applicationContext.xml声明:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

..

  <aop:aspectj-autoproxy proxy-target-class="true"/>

  <bean id="auditingAspect" class="com.mycompany.cismanager.aspect.AuditingAspect" />


..

我正在使用junit调试我的代码,并且没有触发(跳过)端点:

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class AuditingTest extends CommonServicesAbstractTest {

    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AuditingTest.class);



    @Test
    @Audit(auditType = AuditingAspect.ActionAuditType.EXTERNAL_USER_MODIFICATION)
    public void getAllUsersTest() throws Exception {

        logger.debug("log something");

    }

感谢, 射线。

0 个答案:

没有答案