为什么 After Advice 在方法调用之前得到打印
Why After Advice is getting print before method call
我的 Spring AOP 程序未按预期工作。
我在下面创建了简单的 AOP 注释程序,但输出不是我想的那样。
程序:
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"
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="student" class="com.surajhome.practice.spring.Student" >
<property name="name" value="Suraj Kudale"></property>
<property name="age" value="27"></property>
</bean>
<bean id="logging" class="com.surajhome.practice.spring.Logging"></bean>
</beans>
Student.java
包 com.surajhome.practice.spring;
public class Student {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
String name;
int age;
}
Logging.Java
package com.surajhome.practice.spring;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Logging {
@Pointcut("execution(* com.surajhome.practice.spring.*.*(..) )")
public void selectAll()
{
}
@After("selectAll()")
public void afterAdvice()
{
System.out.println("After Advice called");
}
@Before("selectAll()")
public void beforeAdvice()
{
System.out.println("Before Advice called");
}
public void afterReturningAdvice()
{
System.out.println("After Returning Advice called");
}
public void afterThrowingException()
{
System.out.println("After Exception Advice called");
}
}
MainApp.java
package com.surajhome.practice.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext appContext=new ClassPathXmlApplicationContext("Beans.xml");
Student std=(Student) appContext.getBean("student");
System.out.println(std.getName());
System.out.println(std.getAge());
}
}
输出:
调用建议之前
建议调用后
苏拉杰库达尔
在 Advice 调用之前
建议调用后
27
应该是:
在 Advice 调用之前
苏拉杰库达尔
建议调用后
在 Advice 调用之前
27
建议调用后
想想调用 System.out.println(std.getName());
时发生的流程,首先调用获取名称的 @Before 方法,然后获取名称 returns 一个值,然后才调用 @After System.out.println 获取字符串并打印它
我的 Spring AOP 程序未按预期工作。 我在下面创建了简单的 AOP 注释程序,但输出不是我想的那样。
程序:
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"
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="student" class="com.surajhome.practice.spring.Student" >
<property name="name" value="Suraj Kudale"></property>
<property name="age" value="27"></property>
</bean>
<bean id="logging" class="com.surajhome.practice.spring.Logging"></bean>
</beans>
Student.java
包 com.surajhome.practice.spring;
public class Student {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
String name;
int age;
}
Logging.Java
package com.surajhome.practice.spring;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Logging {
@Pointcut("execution(* com.surajhome.practice.spring.*.*(..) )")
public void selectAll()
{
}
@After("selectAll()")
public void afterAdvice()
{
System.out.println("After Advice called");
}
@Before("selectAll()")
public void beforeAdvice()
{
System.out.println("Before Advice called");
}
public void afterReturningAdvice()
{
System.out.println("After Returning Advice called");
}
public void afterThrowingException()
{
System.out.println("After Exception Advice called");
}
}
MainApp.java
package com.surajhome.practice.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext appContext=new ClassPathXmlApplicationContext("Beans.xml");
Student std=(Student) appContext.getBean("student");
System.out.println(std.getName());
System.out.println(std.getAge());
}
}
输出:
调用建议之前
建议调用后
苏拉杰库达尔
在 Advice 调用之前
建议调用后
27
应该是:
在 Advice 调用之前
苏拉杰库达尔
建议调用后
在 Advice 调用之前
27
建议调用后
想想调用 System.out.println(std.getName());
时发生的流程,首先调用获取名称的 @Before 方法,然后获取名称 returns 一个值,然后才调用 @After System.out.println 获取字符串并打印它