Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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 AspectJ不使用@Before_Java_Aspectj - Fatal编程技术网

Java AspectJ不使用@Before

Java AspectJ不使用@Before,java,aspectj,Java,Aspectj,我不知道为什么这个简单的应用程序不起作用。一切都连接正确,我没有得到任何错误。我只是尝试使用AspectJ在一个方法之前调用另一个方法 package com.springpractice.app; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) {

我不知道为什么这个简单的应用程序不起作用。一切都连接正确,我没有得到任何错误。我只是尝试使用AspectJ在一个方法之前调用另一个方法

package com.springpractice.app;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/springpractice/app/Beans.xml");

        Camera camera = (Camera) context.getBean("camera");

        try {

            camera.snap();

            camera.snap(100);
        } catch (Exception e) {
            System.out.println("Caught Exception "+ e.getMessage());
        }

        context.close();
    }


}
Camera.java

package com.springpractice.app;

import org.springframework.stereotype.Component;

@Component("camera") 
public class Camera implements PhotoSnapper {

    public Camera(){
        //System.out.println("Hello from constructor");
    }

    void snap(){
        System.out.println("Snaped a picture...");
    }

    void snap(int exposure){
        System.out.println("Exposure.."+exposure);
    }
}
Logger.java

package com.springpractice.app;

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

@Aspect
@Component("logger")
public class Logger {

@Pointcut("execution(* com.springpractice.app.Camera.snap(..))")
public void cameraSnap(){

}

@Before("cameraSnap()")
public void aboutToTakePicture(){
    System.out.println("About to take a picture");
}

}
beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xmlns:context="http://www.springframework.org/schema/context"
    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-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.springpractice.app"></context:component-scan>

    <aop:aspectj-autoproxy />   
</beans>

snap
方法需要是公共API的一部分

package com.springpractice.app;

import org.springframework.stereotype.Component;

@Component("camera") 
public class Camera implements PhotoSnapper {

    public Camera() {
        //System.out.println("Hello from constructor");
    }

    public void snap() {
        System.out.println("Snaped a picture...");
    }

    public void snap(int exposure) {
        System.out.println("Exposure.."+exposure);
    }
}
Snaped a picture...
Exposure..100
package com.springpractice.app;

import org.springframework.stereotype.Component;

@Component("camera") 
public class Camera implements PhotoSnapper {

    public Camera() {
        //System.out.println("Hello from constructor");
    }

    public void snap() {
        System.out.println("Snaped a picture...");
    }

    public void snap(int exposure) {
        System.out.println("Exposure.."+exposure);
    }
}