Spring configures AOP implementations to define pointcuts and weave enhancements

Source: Internet
Author: User
Tags throwable

After the configuration of AOP, can cut into the log function, access to cut-in, transaction management, performance monitoring and other functions.

The first is to implement the jar package that is needed to weave the enhancement , in addition to the usual

Com.springsource.org.apache.commons.logging-1.1.1.jar,

Com.springsource.org.apache.log4j-1.2.15.jar,

Spring-beans-3.2.0.release.jar,

Spring-context-3.2.0.release.jar,

Spring-core-3.2.0.release.jar,

Outside of Spring-expression-3.2.0.release.jar also need

Spring-aop-3.2.0.release.jar,

Com.springsource.org.aopalliance-1.0.0.jar, (AOP Union Jar)

Com.springsource.org.aspectj.weaver-1.6.8.release.jar ( weave in direction jar) three packs.

Then I have an interface

Public interface Hello {

public void say ();

}

and an implementation class

public class Helloimpl implements hello{

public void Say () {

SYSTEM.OUT.PRINTLN ("Implementation of the Say Method");

}

}

and two enhancement classes

Predecessor Enhancements:

public class Logbefor implements methodbeforeadvice{

@Override

public void before (method, object[] args, Object target)

Throws Throwable {

System.out.println (method);

System.out.println (args);

SYSTEM.OUT.PRINTLN (target);

}

}

Post-Build enhancements:

public class Logafter implements afterreturningadvice{

@Override

public void afterreturning (Object returnvalue, method,

Object[] args, Object target) throws Throwable {

System.out.println ("return value" +returnvalue);

System.out.println ("Method:" +method);

SYSTEM.OUT.PRINTLN ("parameter:" +args);

System.out.println ("proxied object:" +target);

}

}

Next, you need to add an AOP namespace to the beans element in the spring configuration file to import the AOP Related tags: for example:

<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 ">

This is more than before: xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"

Two more in the xsi:schemalocation.

Http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd;

Next, implement the AOP configuration in the Spring configuration file , such as my 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.xsd

Http://www.springframework.org/schema/aop

Http://www.springframework.org/schema/aop/spring-aop.xsd ">

<bean id= "Hello" class= "Cn.cnti.aop.HelloImpl" ></bean>

<!--Enhanced Class--

<bean id= "Before" class= "Cn.cnti.aop.LogBefor" >

</bean>

<bean id= "after" class= "Cn.cnti.aop.LogAfter" >

</bean>

<aop:config>

<!--defining Pointcuts--

<aop:pointcut id= "Pointcut"

expression= "Execution (public void Say ())"/>

<aop:advisor advice-ref= "Before" pointcut-ref= "Pointcut"/>

<aop:advisor advice-ref= "after" pointcut-ref= "Pointcut"/>

</aop:config>

</beans>

If you don't remember clearly,spring-framework-3.2.0.release-dist→spring-framework-3.2.0.release→ docs→spring-framework-reference→html→aop.html Search for AOP will have a template when opened.

where execution is the pointcut indicator, which is a pointcut expression in parentheses, you can configure the method to cut in, and the pointcut expression supports fuzzy matching:

Public * Say (entity. User): "*" means a match for all types of return values

public void * (entity. User): "*" means matching all method names.

public void say (..): ".." Represents the number and type of matches for all parameters.

* Cn.jnti.service.*.* (..): This expression matches All methods of all classes under the Cn.jnti.service package.

* Cn.jnti.service. *. * (..): This expression matches All methods of all classes under the Cn.jnti.service package and its sub-packages.

Wait, there's a lot more.

Finally we test:

@Test

public void Mtest () {

Beanfactory bf=new classpathxmlapplicationcontext ("Appcontext.xml");

Hello h = (hello) bf.getbean ("Hello");

H.say ();

}

******************************************************************************************************

If you want to implement the log output, the original jar package based on the need

Cglib-nodep-2.2.3.jar and org.springframework.asm-3.1.1.release.jar bags

Then add a static constant to the predecessor and post enhancements:

Private static final Logger Log=logger.getlogger (Logbefor.class); and the

Private static final Logger Log=logger.getlogger (Logafter.class);

Then in the method directly with Lo.info ("Call" +target6 "" +method.getname () + "method" + "parameter is:" +arrays.tostring (args)); on the line.

Spring configures AOP implementations to define pointcuts and weave enhancements

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.