gpt4 book ai didi

java - Spring AOP aop :after running early

转载 作者:行者123 更新时间:2023-12-02 02:56:45 28 4
gpt4 key购买 nike

我最近一直在一个非常简单的应用程序上尝试 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>

我的主类,称为 App:

package main.java.springDemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
public static void main(String[] args) {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Coach trackCoach = context.getBean("myTrackCoach", Coach.class);

System.out.println(" From App " + trackCoach.getDailyWorkout());


}
}

TrackerCoach.java

package main.java.springDemo;

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

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");
}

}

教练.java

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";
}
}

你会看到会发生什么。

关于java - Spring AOP aop :after running early,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42963308/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com