Java Tutorial/Spring/Spring Aspect — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 18:22, 31 мая 2010
Содержание
Annotation Scope
File: context.xml
<source lang="java">
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <aop:aspectj-autoproxy /> <context:component-scan base-package="bean"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> </context:component-scan>
</beans></source>
AOP Annotation
File: Main.java
<source lang="java">
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Method; import org.springframework.aop.Advisor; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; public class Main {
public static void main(String[] args) { SampleBean target = new SampleBean(); AnnotationMatchingPointcut pc = new AnnotationMatchingPointcut(null, SimpleAnnotation.class); Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleBeforeAdvice()); ProxyFactory pf = new ProxyFactory(); pf.setTarget(target); pf.addAdvisor(advisor); SampleBean proxy = (SampleBean) pf.getProxy(); proxy.getName(); proxy.getHeight(); }
} class SimpleBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("Before method " + method); }
} @SimpleAnnotation class SampleBean {
@SimpleAnnotation public String getName() { return "AAA"; } public void setName(String name) { } public int getHeight() { return 201; }
} @Target( { ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @interface SimpleAnnotation { } class AnnotationAfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.print("After annotated method: " + method); }
}</source>
Aspect Annotation
File: context.xml
<source lang="java">
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <aop:aspectj-autoproxy /> <context:component-scan base-package="bean"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> </context:component-scan>
</beans></source>
Aspect Annotation Pointcut Around After
File: context.xml
<source lang="java">
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="test" class="TestBean"/> <bean class="LoggingAspectPC"> <property name="beforeMessage" value="Before %s %s"/> <property name="afterMessage" value="After %s %s"/> </bean> <aop:aspectj-autoproxy />
</beans></source>
Aspect Filter
File: context.xml
<source lang="java">
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <context:component-scan base-package="bean"> <context:include-filter type="annotation" expression="bean.Magic"/> <context:include-filter type="assignable" expression="bean.ruponentMarker"/> <context:include-filter type="aspectj" expression="* void bean.*Service*(..)"/> </context:component-scan>
</beans></source>
AspectJ AutoProxy
File: context.xml
<source lang="java">
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="test" class="TestBean2"> <property name="simpleBean" ref="simple"/> </bean> <bean id="simple" class="SimpleBean"/> <bean class="Main"/> <aop:aspectj-autoproxy/>
</beans></source>
AspectJ Expression Pointcut
File: Main.java
<source lang="java">
import org.springframework.aop.Advisor; import org.springframework.aop.aspectj.AspectJExpressionPointcut; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.DefaultPointcutAdvisor; import bean.MyClass; import bean.SimpleAfterAdvice; public class Main {
public static void main(String[] args) { MyClass target = new MyClass(); AspectJExpressionPointcut pc = new AspectJExpressionPointcut(); pc.setExpression("execution(* bean..*.get*(..))"); Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAfterAdvice()); ProxyFactory pf = new ProxyFactory(); pf.setTarget(target); pf.addAdvisor(advisor); MyClass proxy = (MyClass) pf.getProxy(); System.out.println(proxy.getName()); proxy.setName("New Name"); System.out.println(proxy.getHeight()); }
}</source>
Spring AOP Examples
Spring Aspect Logging
File: IBusinessLogic.java
<source lang="java">
public interface IBusinessLogic {
public void foo(); public void bar() throws BusinessLogicException;
}</source>
Spring Tracing Aspect
File: IBusinessLogic.java
<source lang="java">
public interface IBusinessLogic {
public void foo();
}</source>