Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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/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 春季AOP AOP:在提前运行之后_Java_Spring_Maven_Aop_Spring Aop - Fatal编程技术网

Java 春季AOP AOP:在提前运行之后

Java 春季AOP AOP:在提前运行之后,java,spring,maven,aop,spring-aop,Java,Spring,Maven,Aop,Spring Aop,最近,我在一个非常简单的应用程序上试用了Spring的AOP功能,我一直坚持在适当的时间运行该方法,这意味着该节中定义的方法应该在 在我的代码中,和中定义的方法都在主方法之前运行。当然,这是正常的,但不是在后者 预期产出应为: HERE IS THE AOP BEFORE From App ran 5k HERE IS THE AOP After 我当前的输出是: HERE IS THE AOP BEFORE HERE IS THE AOP After From App ran 5k

最近,我在一个非常简单的应用程序上试用了Spring的AOP功能,我一直坚持在适当的时间运行该方法,这意味着该节中定义的方法应该在

在我的代码中,和中定义的方法都在主方法之前运行。当然,这是正常的,但不是在后者

预期产出应为:

HERE IS THE AOP BEFORE

From App ran 5k

HERE IS THE AOP After
我当前的输出是:

HERE IS THE AOP BEFORE

HERE IS THE AOP After

From App ran 5k
知道为什么吗

Pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springDemo</groupId>
<artifactId>FirstSpringDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>FirstSpringDemo</name>
<url>http://maven.apache.org</url>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.11</version>
    </dependency>
</dependencies>
</project>
TrackerCoach.java

package main.java.springDemo;

public class TrackCoach implements Coach {
@Override
public String getDailyWorkout() {
    return "Go and run 5k";
}
}
package main.java.springDemo;

public interface Coach {
    String getDailyWorkout();

}
SayAOP.java

package main.java.springDemo;


public class SayAOP {

   public void shoutAOPBefore() {

    System.out.println("HERE IS THE AOP BEFORE");
}

public void shoutAOPAfter(){

    System.out.println("HERE IS THE AOP After");
}

}
Coach.java

package main.java.springDemo;

public class TrackCoach implements Coach {
@Override
public String getDailyWorkout() {
    return "Go and run 5k";
}
}
package main.java.springDemo;

public interface Coach {
    String getDailyWorkout();

}
applicationContext.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:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- Define your beans here -->


<aop:config>

    <aop:aspect id="aop" ref="SayAOP">

        <aop:pointcut id="pid" expression="execution(* main.java.springDemo.Coach.getDailyWorkout(..))"/>

        <aop:before pointcut-ref="pid" method="shoutAOPBefore"/>
        <aop:after pointcut-ref="pid" method="shoutAOPAfter"/>
    </aop:aspect>

</aop:config>

<bean id="myTrackCoach"
      class="main.java.springDemo.TrackCoach">

</bean>

<bean id="SayAOP"
      class="main.java.springDemo.SayAOP">
</bean>

</beans>

那是因为
trackCoach.getDailyWorkout()
作为System.out.println参数调用,因此首先调用before,然后调用trackCoach.getDailyWorkout(),然后调用after,最后调用System.out。 例如:

public class TrackCoach implements Coach {
    @Override
    public String getDailyWorkout() {
        System.out.println("Go and run 5k");
        return "Go and run 5k";
    }
}
你会看到发生了什么。那是因为
trackCoach.getDailyWorkout()
作为System.out.println参数调用,因此首先调用before,然后调用trackCoach.getDailyWorkout(),然后调用after,最后调用System.out。 例如:

public class TrackCoach implements Coach {
    @Override
    public String getDailyWorkout() {
        System.out.println("Go and run 5k");
        return "Go and run 5k";
    }
}

你会看到发生了什么。

谢谢你,沃伊特,这就是问题所在!我接受了,但我不能投票给你的答案,直到我达到15声誉,对不起!谢谢你Wojtek,这就是问题所在!我接受了,但我不能投票给你的答案,直到我达到15声誉,对不起!