English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring AOP-Beispiel

gibt an Spring1.2Alte AOP (Basierend auf dtd) implementierte Beispiele.

Obwohl in Spring 3Wird unterstützt, aber die Verwendung von advice wird auf der nächsten Seite erläutert, wo Spring AOP und AspectJ gemeinsam verwendet werden.

spring1.2Supported in old-style aop implementation4types of Advice.

Before AdviceIt executes before the actual method call. After AdviceIt executes after the actual method call. If the method returns a value, it is executed after the return value. Around AdviceIt executes before and after the actual method call. Throws AdviceIf the actual method throws an exception, execute the Advice.

Note: To understand the basic concepts of Spring AOP, please visit the previous page.

Advice hierarchy

Let's understand the Advice hierarchy through the following chart:

All are interfaces in aop.

MethodBeforeAdvice interface extension of BeforeAdvice interface.

AfterReturningAdvice interface extension of AfterAdvice interface.

ThrowsAdvice interface extension AfterAdvice interface.

MethodInterceptor interface extension Interceptor interface. It is used around Advice.

1MethodBeforeAdvice example

Erstellen Sie eine Klasse, die die tatsächliche Geschäftslogik enthält.

Datei: A.java

package com.w;3codebox;
public class A {
public void m(){System.out.println("actual business logic");}
}

Now, create an advisor class that implements the MethodBeforeAdvice interface.

Datei: BeforeAdvisor.java

package com.w;3codebox;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvisor implements MethodBeforeAdvice{
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		System.out.println("zusätzlicher Bedenken vor der tatsächlichen Logik");
	}
}

In the xml file, create3a bean, one for A class, the second for Advisor class, the third for ProxyFactoryBean class.

Datei: 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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="obj" class="com.w3codebox.A"></bean>
<bean id="ba" class="com.w3codebox.BeforeAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="obj"></property>
<property name="interceptorNames">
<list>
<value>ba</value>
</list>
</property>
</bean>
</beans>

Understanding ProxyFactoryBean class:

ProxyFactoryBean The class is provided by Spring Famework. It includes2The properties target and interceptorNames. An instance of A class will be regarded as the target object, and an instance of Advisor class will be regarded as the interceptor. You need to pass the advisor object as a list object as shown in the above xml file.

The writing of ProxyFactoryBean class is as follows:

public class ProxyFactoryBean{
private Object target;
private List interceptorNames;
//getters and setters
}

Now, let's call the actual method.

File: Test.java

package com.w;3codebox;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
	Resource r=new ClassPathResource("applicationContext.xml");
	BeanFactory factory=new XmlBeanFactory(r);
	A a = factory.getBean("proxy", A.class);
	a.m();
}
}

Output

zusätzlicher Bedenken vor der tatsächlichen Logik
tatsächliche Geschäftslogik

In MethodBeforeAdvice, we can print other information, such as method name, method parameters, target object, target object class name, proxy class, etc.

Sie müssen nur zwei Klassen BeforeAdvisor.java und Test.java ändern.

Datei: BeforeAdvisor.java

package com.w;3codebox;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvisor implements MethodBeforeAdvice{
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		System.out.println("zusätzlicher Bedenken vor der tatsächlichen Logik");
		System.out.println("Methoden-Information:");+method.getName()+" "+method.getModifiers());
		System.out.println("Argument-Information:");
		for(Object arg: args)
			System.out.println(arg);
		System.out.println("Zielobjekt:");+target);
		System.out.println("Zielobjekt-Klasse-Name: ");+target.getClass().getName());
	}
}

File: Test.java

package com.w;3codebox;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
	Resource r=new ClassPathResource("applicationContext.xml");
	BeanFactory factory=new XmlBeanFactory(r);
	A a = factory.getBean("proxy", A.class);
        System.out.println("Proxy-Klassenname: ");+a.getClass().getName());
	a.m();
}
}

Output

Proxy-Klassenname: com.w3codebox.A$EnhancerByCGLIB$409872b1
zusätzlicher Bedenken vor der tatsächlichen Logik
Methoden-Information:m 1
Argument-Information:
Zielobjekt:com.w3codebox.A@11dba45
Zielobjekt-Klasse-Name: com.w3codebox.A
tatsächliche Geschäftslogik

AfterReturningAdvice-Beispiel

Erstellen Sie eine Klasse, die die tatsächliche Geschäftslogik enthält.

Datei: A.java

Wie im obigen Beispiel.

Nun erstellen Sie eine Beraterklasse, die das AfterReturningAdvice-Interface implementiert.

Datei: AfterAdvisor.java

package com.w;3codebox;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvisor implements AfterReturningAdvice{
	@Override
	public void afterReturning(Object returnValue, Method method,
         Object[] args, Object target) throws Throwable {
		System.out.println("zusätzlicher Bedenken nach dem Rückschritt des Rates");
	}
}

Erstellen Sie wie im obigen Beispiel eine XML-Datei, Sie müssen hier nur die Klasse des Beraters ändern.

Datei: 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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="obj" class="com.w3codebox.A"></bean>
<bean id="ba" class="com.w3codebox.AfterAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="obj"></property>
<property name="interceptorNames">
<list>
<value>ba</value>
</list>
</property>
</bean>
</beans>

File: Test.java

Wie im obigen Beispiel.

Output

tatsächliche Geschäftslogik
zusätzlicher Bedenken nach dem Rückschritt des Rates

3)MethodInterceptor(AroundAdvice)-Beispiel

Erstellen Sie eine Klasse, die die tatsächliche Geschäftslogik enthält.

Datei: A.java

Wie im obigen Beispiel.

Erstellen Sie nun eine Klasse, die das Interface MethodInterceptor implementiert.

Datei: AroundAdvisor.java

package com.w;3codebox;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvisor implements MethodInterceptor{
	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		Object obj;
		System.out.println("zusätzlicher Bedenken vor der tatsächlichen Logik");
		obj = mi.proceed();
		System.out.println("zusätzlicher Bedenken nach der tatsächlichen Logik");
		return obj;
	}
}

Erstellen Sie wie im obigen Beispiel eine XML-Datei, Sie müssen hier nur die Klasse des Beraters ändern.

Datei: 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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="obj" class="com.w3codebox.A"></bean>
<bean id="ba" class="com.w3codebox.AroundAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="obj"></property>
<property name="interceptorNames">
<list>
<value>ba</value>
</list>
</property>
</bean>
</beans>

File: Test.java

Wie im obigen Beispiel.

Output

zusätzlicher Bedenken vor der tatsächlichen Logik
tatsächliche Geschäftslogik
zusätzlicher Bedenken nach der tatsächlichen Logik

4、ThrowsAdvice-Beispiel

Erstellen Sie eine Klasse, die die tatsächliche Geschäftslogik enthält.

Datei: Validator.java

package com.w;3codebox;
public class Validator {
	public void validate(int age)throws Exception{
		if(age<18){
			throw new ArithmeticException("Ungültiges Alter");
		}
		else{
			System.out.println("Stimme bestätigt");
		}
	}
}

Erstellen Sie nun eine Klasse, die das Interface ThrowsAdvice implementiert.

Datei: ThrowsAdvisor.java

package com.w;3codebox;
import org.springframework.aop.ThrowsAdvice;
public class ThrowsAdvisor implements ThrowsAdvice{
	public void afterThrowing(Exception ex){
		System.out.println("zusätzlicher Bedenken, falls ein Ausnahmevorkommt");
	}
}

Erstellen Sie wie im obigen Beispiel eine XML-Datei. Sie müssen nur die Validator- und Advisor-Klassen ändern.

Datei: 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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="obj" class="com.w3codebox.Validator"></bean>
<bean id="ba" class="com.w3codebox.ThrowsAdvisor</>/bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="obj"></property>
<property name="interceptorNames">
<list>
<value>ba</value>
</list>
</property>
</bean>
</beans>

File: Test.java

package com.w;3codebox;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
	Resource r=new ClassPathResource("applicationContext.xml");
	BeanFactory factory=new XmlBeanFactory(r);
	Validator v=factory.getBean("proxy",Validator.class);
	try{
	v.validate(12;
	}catch(Exception e){e.printStackTrace();}
}
}

Output

java.lang.ArithmeticException: Not Valid Age
additional concern if exception occurs
	at com.w3codebox.Validator.validate(Validator.java:7)
	at com.w3codebox.Validator$FastClassByCGLIB$562915cf.invoke(<generated>)
	at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
	at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invoke
Joinpoint(Cglib2AopProxy.java:692)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.
invoke(ThrowsAdviceInterceptor.java:124)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.
intercept(Cglib2AopProxy.java:625)
	at com.w3codebox.Validator$EnhancerByCGLIB$4230ed28.validate(<generiert>)
	at com.w3codebox.Test.main(Test.java:15)