Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@AspectJ:未应用建议_Java_Spring_Aspectj - Fatal编程技术网

Java Spring@AspectJ:未应用建议

Java Spring@AspectJ:未应用建议,java,spring,aspectj,Java,Spring,Aspectj,我正在尝试在调用start()之前编写代码 这是我想要建议的TestClass: package com.test; public class TestClass { public static void main(String[] args) { new TestClass().start(); } private void start() { System.out.println("Test started"); } }

我正在尝试在调用
start()之前编写代码

这是我想要建议的TestClass:

package com.test;

public class TestClass {

    public static void main(String[] args) {

        new TestClass().start();
    }
    private void start() {

        System.out.println("Test started");
    }
}
这是一个方面,包括建议:

package com.test;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {

    @Before("call(void com.test.TestClass.start())")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("logBefore() is running");
    }

}
这是我的spring.xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy/>
<context:annotation-config/>
<context:component-scan base-package="com.test"/>
这方面:

package com.test;

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

@Aspect
public class LogAspect {
    @Before("execution(void com.test.TestClass.start(..))")
    public void logBefore(JoinPoint joinPoint) {

        System.out.println("logging before "
                + joinPoint.getSignature().getName());
    }
}
以及Spring-Config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:aspectj-autoproxy/>    
    <bean id="myTest" class="com.test.TestClassImpl" />
    <bean id="logAspect" class="com.test.LogAspect" />

</beans>


感谢您的帮助。

A
call
AspectJ切入点在从另一个类调用该方法时应用。当您自己直接调用它时,您应该使用执行切入点,因此更改:

@Before("call(void com.test.TestClass.start())")

也让
Spring
创建bean,而不是自己直接创建一个



旁白:注意,您可以将
Spring
XML文件与
src/resources
中的源文件分开,它们将由
ClassPathXmlApplicationContext

拾取,还可以通过Spring应用程序上下文实例化
Test
类,不是通过调用
new
。虽然我确实使用Maven来进行实际的编织,但我确实让您的代码正常工作了。@Reimeus:请将您的Maven项目发送到我的电子邮件地址好吗?我想弄清楚。你只需要POM,这是一个
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:aspectj-autoproxy/>    
    <bean id="myTest" class="com.test.TestClassImpl" />
    <bean id="logAspect" class="com.test.LogAspect" />

</beans>
@Before("call(void com.test.TestClass.start())")
@Before("execution(void com.test.TestClass.start())")