"Spring Combat"--13 aspectj annotation Facets

Source: Internet
Author: User

Before we learned about the typical AOP-based usage of configuration, the following describes how to rely on annotations to implement AOP.

based on annotations, the complexity of the configuration file is reduced, but the coupling between programs is introduced , and the pros and cons are judged by the users themselves.

It should be noted that the version between the ASPECTJ and the JDK is determined, otherwise it will be an error, please see for details.

First look at the annotation-based slice class, where the tangent is not just a Pojo class, but closely coupled with AOP. But the configuration process and the way are the same as the original way.

Package Com.spring.test.chap44;import Org.aspectj.lang.annotation.afterreturning;import Org.aspectj.lang.annotation.afterthrowing;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import Org.aspectj.lang.annotation.pointcut;import Org.springframework.stereotype.Component, @Component @aspect Public classAudience {@Pointcut ("Execution (* com.spring.test.chap44.Instrumentalist.perform (..))")     Public voidperformance () {} @Before ("performance ()")     Public voidtakeseats () {System. out. println ("takeseats ()"); } @Before ("performance ()")     Public voidTurnoffcellphones () {System. out. println ("Turnoffcellphones ()"); } @AfterReturning ("performance ()")     Public voidapplaud () {System. out. println ("applaud ()"); } @AfterThrowing ("performance ()")     Public voidDemandrefund () {System. out. println ("Demandrefund ()"); }}

Next are some other essential classes:

Tangent interface class:

Package COM.SPRING.TEST.CHAP44;  Public Interface Performer {    publicvoid  perform ();}

Pointcut Implementation class:

Package Com.spring.test.chap44;import org.springframework.stereotype.Component; @Component  Public class instrumentalist implements performer{    publicvoid  perform () {        System. out. println ("__________ perform ___________");     }

Test class:

Package Com.spring.test.chap44;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.ClassPathXmlApplicationContext; Public classTest { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Bean.xml"); Performer Performer= (performer) Ctx.getbean ("Xingoo");    Performer.perform (); }}

The following are the key configuration files

The configuration file at this point is aware that spring will know which one is the normal bean and which is the notification. It is therefore necessary to add an attribute to ensure that AOP automatically recognizes notifications.

<aop:aspectj-autoproxy proxy-target-Class="true"/>

The configuration file is as follows:

<?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"Xmlns:tx="Http://www.springframework.org/schema/tx"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.0.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-3.0.xsd "><bean id="Xingoo" class="com.spring.test.chap44.Instrumentalist"/> <bean id="Audience" class="com.spring.test.chap44.Audience"/> <aop:aspectj-autoproxy proxy-target-class="true"/></beans>

The results of the implementation are as follows:

Turnoffcellphones () takeseats () __________ perform ___________applaud ()

 

"Spring Combat"--13 aspectj annotation Facets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.