使用Spring Aspects进行日志记录

时间:2014-06-25 05:47:26

标签: java spring

我对方面的概念很陌生。所以,我正在尝试使用方面来打印每个执行的函数的名称。看了几个教程后,我得到了类似的东西:

package com.skyfall.model.slide2;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class testAspect {

    @Before("execution(public * *(..))")
    public void log(JoinPoint joinPoint) {
        System.out.println("----logBefore is running.-----");
        System.out.println("executing ".concat(joinPoint.getSignature().getName()));

    }
}

但是,这似乎没有任何效果。

编辑:这是我正在使用的特定类:

package com.skyfall.model.slide2;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class CostDto {

    private String text;
    private int value;
    private static final String RUPEES = "Rs.";
    private static final String SPACE_DELIMITTER = " ";

    public CostDto(String text) {
        this.text = text;
        this.value = getValueFromText(text);
    }

    public CostDto(int value) {
        this.value = value;
        this.text = getTextFromValue(value);
    }

    public CostDto(String text, int value) {
        this.text = text;
        this.value = value;
    }

    public int getValueFromText(String text) {
        int length = text.length();
        String value = text.substring(0, length - RUPEES.length());
        value = value.trim();
        return Integer.parseInt(value);
    }

    public String getTextFromValue(int value) {
        return String.valueOf(value) + SPACE_DELIMITTER + RUPEES;
    }

    public String getText() {
        return text;
    }

    public int getValue() {
        return value;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}

这是JUnit测试:

package com.skyfall.model.slide2;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.skyfall.persistence.base.BaseControllerTest;

public class CostDtoTest extends BaseControllerTest {

    private CostDto cost1;

    @Before
    public void init() {
        cost1 = null;
    }

    @Test
    public void testValueConstructor() {
        cost1 = new CostDto("123 Rs.");
        assertEquals(123, cost1.getValue());
    }

    @Test
    public void testTextConstructor() {
        cost1 = new CostDto(8901);
        System.out.println(cost1.getText());
        assertEquals("8901 Rs.", cost1.getText());
    }

    @Test
    public void testValueAndTextConstructor() {
        cost1 = new CostDto("491 Rs.", 491);
        assertEquals(491, cost1.getValue());
        assertEquals("491 Rs.", cost1.getText());
    }
}

测试工作正常,打印出“8901 Rs”。正如所料,但testAspect没有做任何事情。

0 个答案:

没有答案
相关问题