Spring AOP–Examples

abg-technologyNet

Spring AOP–Examples

Setting Context

<?xml version="1.0" encoding="UTF-8"?><br />
<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"
       xmlns:context="http://www.springframework.org/schema/context"
       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-3.1.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"><br />
    <aop:aspectj-autoproxy/><br />
    <context:component-scan base-package="org.anotes.springexample.aop"/><br />
</beans>

Before, After, Around (example1)
Objects
public class Person {<br />
    protected String name;<br />
    public void walk(){<br />
        logger.info(name+&#8221; is walking&#8221;);<br />
    }

@Component<br />
@Scope(&#8220;prototype&#8221;)<br />
public class Painter extends Person{<br />
    public void paint(){<br />
        logger.info(name+&#8221; is painting&#8221;);<br />
    }<br />
}

@Component<br />
@Scope(&#8220;prototype&#8221;)<br />
public class Teacher extends Person {<br />
    public void teach(){<br />
        logger.info(name+&#8221; is teaching&#8221;);<br />
    }<br />
}

Aspect

@Aspect<br />
@Component(&#8220;aspectExample1&#8221;)<br />
public class AspectExample {<br />
    protected final Logger logger = LoggerFactory.getLogger(getClass());<br />
    @Pointcut(&#8220;execution(* org.anotes.springexample.aop.example1.*.walk(..))&#8221;)<br />
    private void anyWalking() {<br />
    }<br />
    @Pointcut(&#8220;execution(* org.anotes.springexample.aop.example1.*.*(..)) &#038;&#038; bean(teacher)&#8221;)<br />
    private void teacherMethods() {<br />
    }<br />
    @Pointcut(&#8220;within(org.anotes.springexample.aop.example1.*))&#8221;)<br />
    public void inPainterAndTeacherMethods() {<br />
    }</p>
<p>    @Before(&#8220;anyWalking()&#8221;)<br />
    public void logBeforeAnyWalking(JoinPoint joinPoint) {<br />
        logger.info(&#8220;Before walking:{}&#8221;, joinPoint.getTarget());<br />
    }<br />
    @After(&#8220;anyWalking()&#8221;)<br />
    public void logAfterAnyWalking() {<br />
        logger.info(&#8220;After walking&#8221;);<br />
    }<br />
    @Before(&#8220;teacherMethods() &#038;&#038; args(param)&#8221;)<br />
    public void logPainterAndTeacherMethodWithStringArg(String param) {<br />
        logger.info(&#8220;Teacher Methods:Before methods with String parameter:{}&#8221;, param);<br />
    }<br />
    @Around(&#8220;inPainterAndTeacherMethods()&#8221;)<br />
    public void logAllMethodsPainterAndTeacher(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {<br />
        logger.info(&#8220;Around: Before executing:&#8221; + proceedingJoinPoint.getSignature());<br />
        proceedingJoinPoint.proceed();<br />
        logger.info(&#8220;Around: After executing:&#8221; + proceedingJoinPoint.getSignature());<br />
    }<br />
}<br />

Lines

Line 5: Define a Pointcut that point to “walk” method of any Class that are in the “example1” package

Line 8: Define a Pointcut that point to all methods of the bean “teacher”

Line 11: Define a Pointcut that point to any method of any Class that are in the “example1” package

Line 15: Define a Before Advice that will be called before executing any “walk” method

Line 19: Define a After Advice that will be called before executing any “walk” method

Line 23: Define a Before Advice that will be called before executing any method in the “teacher” bean that has at first parameter a String

Line 27: Define a Around Advice that will be called before executing any method in the “painter” and “teacher” bean

Code to test the Advice

public class Main {<br />
    private static final Logger logger = LoggerFactory.getLogger(Main.class);<br />
    public static void main(String[] args) {<br />
        String configFiles = &#8220;classpath*:/app-context.xml&#8221;;<br />
        ApplicationContext context = new ClassPathXmlApplicationContext(configFiles);<br />
        example1(context);<br />
    }<br />
    private static void example1(ApplicationContext context) {<br />
        logger.info(&#8220;EXAMPLE 1 &#8211; Before, After, Around &#8220;);<br />
        Teacher teacher = context.getBean(Teacher.class);<br />
        teacher.setName(&#8220;Ross&#8221;);<br />
        Teacher teacher2 = context.getBean(Teacher.class);<br />
        teacher2.setName(&#8220;Jhon&#8221;);<br />
        Painter painter = context.getBean(Painter.class);<br />
        painter.setName(&#8220;Pedro&#8221;);<br />
        teacher.walk();<br />
        painter.walk();<br />
    }

Output

            aop.Main EXAMPLE 1 &#8211; Before, After, Around  [INFO ]<br />
aspect.AspectExample Teacher Methods:Before methods with String parameter:Ross [INFO ]<br />
aspect.AspectExample Around: Before executing:void org.anotes.springexample.aop.example1.Person.setName(String) [INFO ]<br />
aspect.AspectExample Around: After executing:void org.anotes.springexample.aop.example1.Person.setName(String) [INFO ]<br />
aspect.AspectExample Teacher Methods:Before methods with String parameter:Jhon [INFO ]<br />
aspect.AspectExample Around: Before executing:void org.anotes.springexample.aop.example1.Person.setName(String) [INFO ]<br />
aspect.AspectExample Around: After executing:void org.anotes.springexample.aop.example1.Person.setName(String) [INFO ]<br />
aspect.AspectExample Around: Before executing:void org.anotes.springexample.aop.example1.Person.setName(String) [INFO ]<br />
aspect.AspectExample Around: After executing:void org.anotes.springexample.aop.example1.Person.setName(String) [INFO ]<br />
aspect.AspectExample Before walking:org.anotes.springexample.aop.example1.Teacher@1989b5 [INFO ]<br />
aspect.AspectExample Around: Before executing:void org.anotes.springexample.aop.example1.Person.walk() [INFO ]<br />
    example1.Teacher Ross is walking [INFO ]<br />
aspect.AspectExample After walking [INFO ]<br />
aspect.AspectExample Around: After executing:void org.anotes.springexample.aop.example1.Person.walk() [INFO ]<br />
aspect.AspectExample Before walking:org.anotes.springexample.aop.example1.Painter@c3c315 [INFO ]<br />
aspect.AspectExample Around: Before executing:void org.anotes.springexample.aop.example1.Person.walk() [INFO ]<br />
    example1.Painter Pedro is walking [INFO ]<br />
aspect.AspectExample After walking [INFO ]<br />
aspect.AspectExample Around: After executing:void org.anotes.springexample.aop.example1.Person.walk() [INFO ]

Alfonso Bueno Gayo

Webmaster desde 2010, administrador de sistemas, fan de WordPress, SEO, Internet, Nuevas Tecnologías, Fotográfia...

Comparte este post