Spring 3.x enterprise application development practice (10) ---- AOP aspect

Source: Internet
Author: User

1. Static common method name matching aspect

StaticMethodMatcherPointcutAdvisor represents a static method matching plane.

package com.smart.advisor;public class Waiter {public void greetTo(String name){System.out.println("Waiter greet to "+name+"...");}public void serveTo(String name){System.out.println("Waiter serving "+name+"...");}}

package com.smart.advisor;public class Seller {public void greetTo(String name){System.out.println("Seller greet to "+name+"...");}}

Package com. smart. advisor; import java. lang. reflect. method; import org. springframework. aop. classFilter; import org. springframework. aop. support. staticMethodMatcherPointcutAdvisor; public class GreetingAdvisor extends StaticMethodMatcherPointcutAdvisor {private static final long serialVersionUID = 1L; @ Overridepublic boolean matches (Method arg0, Class <?> Arg1) {// rule for matching the tangent method: The method name is greetTo // TODO Auto-generated method stubreturn "greetTo ". equals (arg0.getName () ;}@ Overridepublic ClassFilter getClassFilter () {// rules for point-to-point matching: for the type or subtype of Waiter // TODO Auto-generated method stubreturn new ClassFilter () {@ Overridepublic boolean matches (Class <?> Clazz) {// TODO Auto-generated method stubreturn Waiter. class. isAssignableFrom (clazz );}};}}

Package com. smart. advisor; import java. lang. reflect. method; import org. springframework. aop. methodBeforeAdvice; public class implements MethodBeforeAdvice {@ Overridepublic void before (Method arg0, Object [] arg1, Object arg2) throws Throwable {// TODO Auto-generated method stubSystem. out. println (arg2.getClass (). getName () + ". "+ arg0.getName (); // returns the tangent String clientName = (String) arg1 [0]; Syst Em. out. println ("How are you! Mr. "+ clientName + ".");}}

<Bean id = "waiterTarget" class = "com. smart. advisor. waiter "/> <bean id =" sellerTarget "class =" com. smart. advisor. seller "/> <bean id =" greetingAdvice "class =" com. smart. advisor. greetingBeforeAdvice "/> <bean id =" greetingAdvisor "class =" com. smart. advisor. greetingAdvisor "p: advice-ref =" greetingAdvice "/> <! -- Inject a pre-enhancement to the aspect --> <bean id = "parent" abstract = "true" class = "org. springframework. aop. framework. proxyFactoryBean "p: interceptorNames =" greetingAdvisor "p: proxyTargetClass =" true "/> <! -- Abstract = "true" refers to a parent <bean> fixed public configuration information --> <bean id = "waiter" parent = "parent" p: target-ref = "waiterTarget"/> <! -- Waiter proxy --> <bean id = "seller" parent = "parent" p: target-ref = "sellerTarget"/> <! -- Seller proxy -->

package com.smart.advisor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringAdvisorTest {@Testpublic void testAdvisor(){String configPath="com/smart/advisor/beans.xml";ApplicationContext ctx=new ClassPathXmlApplicationContext(configPath);Waiter waiter=(Waiter)ctx.getBean("waiter");Seller seller=(Seller)ctx.getBean("seller");waiter.greetTo("TomSon");waiter.serveTo("TomSon");seller.greetTo("TomSon");}}

Expected result:

Com. smart. advisor. Waiter. greetTo
How are you! Mr. TomSon.
Waiter greet to TomSon...
Waiter serving TomSon...
Seller greet to TomSon...

It can be seen that the cut surface is only woven into the connection point before the Waiter. greetTo () method is called, and the Waiter. serveTo () and Seller. greetTo () methods are not woven into the cut surface.

2. Static regular expression method matching aspect

Example:

Beans2.xml

<Bean id = "waiterTarget" class = "com. smart. advisor. waiter "/> <bean id =" sellerTarget "class =" com. smart. advisor. seller "/> <bean id =" greetingAdvice "class =" com. smart. advisor. greetingBeforeAdvice "/> <bean id =" regexAdvisor "class =" org. springframework. aop. support. regexpMethodPointcutAdvisor "p: advice-ref =" greetingAdvice "> <property name =" patterns "> <! -- Use a regular expression to define the full-qualified method name matching mode string of the target class --> <list> <value>. * greet. * </value> <! -- Match mode string --> </list> </property> </bean> <bean id = "waiter1" class = "org. springframework. aop. framework. proxyFactoryBean "p: interceptorNames =" regexAdvisor "p: target-ref =" waiterTarget "p: proxyTargetClass =" true "/>
Defines a matching mode string ". * greet. *", which matches the Waiter. greetTo () method.


Test data:

package com.smart.advisor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringAdvisorTest {@Testpublic void testAdvisor(){String configPath="com/smart/advisor/beans2.xml";ApplicationContext ctx=new ClassPathXmlApplicationContext(configPath);Waiter waiter=(Waiter)ctx.getBean("waiter1");waiter.greetTo("Tom");waiter.serveTo("Tom");}}

The result is as follows:

Com. smart. advisor. Waiter. greetTo
How are you! Mr. Tom.
Waiter greet to Tom...
Waiter serving Tom...


3. automatically create a proxy:

BeanNameAutoProxyCreator instance:

Beans3.xml

<bean id="waiterTarget" class="com.smart.advisor.Waiter"/><bean id="sellerTarget" class="com.smart.advisor.Seller"/><bean id="greetingAdvice" class="com.smart.advisor.GreetingBeforeAdvice"/><bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"p:beanNames="*erTarget"p:interceptorNames="greetingAdvice"p:optimize="true"/>

Test data:

package com.smart.advisor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringAdvisorTest {@Testpublic void testAdvisor(){String configPath="com/smart/advisor/beans3.xml";ApplicationContext ctx=new ClassPathXmlApplicationContext(configPath);Waiter waiter=(Waiter)ctx.getBean("waiterTarget");Seller seller=(Seller)ctx.getBean("sellerTarget");waiter.greetTo("John");seller.greetTo("Tom");}}
The result data is:

Com. smart. advisor. Waiter. greetTo
How are you! Mr. John.
Waiter greet to John...
Com. smart. advisor. Seller. greetTo
How are you! Mr. Tom.
Seller greet to Tom...

DefaultAdvisorAutoProxyCreator instance:

DefaultAdvisorAutoProxyCreator can scan the Advisor in the container and automatically weave the Advisor into the matched target Bean, which automatically creates a proxy for the matched target Bean.

Beans4.xml

<Bean id = "waiterTarget" class = "com. smart. advisor. waiter "/> <bean id =" sellerTarget "class =" com. smart. advisor. seller "/> <bean id =" greetingAdvice "class =" com. smart. advisor. greetingBeforeAdvice "/> <bean id =" regexpAdvisor "class =" org. springframework. aop. support. regexpMethodPointcutAdvisor "p: patterns = ". * greet. * "p: advice-ref =" greetingAdvice "/> <bean class =" org. springframework. aop. framework. autoproxy. defaultAd VisorAutoProxyCreator "/> <! -- Use defaadadvisorautoproxycreator to define a Bean, which includes the Advisor in the container into the matched object Bean. -->


Test data:

package com.smart.advisor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringAdvisorTest {@Testpublic void testAdvisor(){String configPath="com/smart/advisor/beans4.xml";ApplicationContext ctx=new ClassPathXmlApplicationContext(configPath);Waiter waiter=(Waiter)ctx.getBean("waiterTarget");Seller seller=(Seller)ctx.getBean("sellerTarget");waiter.serveTo("John");waiter.greetTo("John");seller.greetTo("Tom");}}

The result is:

Waiter serving John...
Com. smart. advisor. Waiter. greetTo
How are you! Mr. John.
Waiter greet to John...
Com. smart. advisor. Seller. greetTo
How are you! Mr. Tom.
Seller greet to Tom...


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.