Introduction
Oriented Oriented Programming (AOP) provides another perspective to think about program structure, which makes up for the shortcomings of object-oriented programming (OOP). In addition to classes, AOP provides facets. Modularize the focus, such as cross-cutting transaction management of multiple types and objects. (These concern terms are often referred to as crosscutting concerns.)
A key component of Spring is the AOP framework. Despite this, Spring IoC containers do not rely on AOP, which means you are free to choose whether to use AOP. AOP provides a powerful middleware solution, which makes Spring IoC containers more complete.
Spring 2.0 AOP:
Spring 2.0 introduces a simpler and more powerful way to customize the section, and users can choose to use schema-based methods or use @AspectJ annotations. For new applications, if the user is developing in Java 5, we recommend that the user use @AspectJ style, otherwise the pattern-based style can be used. Both styles fully support the Advice type and the point-in-cut language of AspectJ, although it is still weaving using Spring AOP.
This chapter mainly discusses Spring 2.0's support for pattern-based and @AspectJ-based AOP. Spring 2.0 completely retains the backward compatibility of Spring 1.2. The next chapter will discuss the underlying AOP support provided by the Spring 1.2 API.
AOP used in Spring:
Declarative enterprise services are provided, especially in place of EJB declarative services. The most important service is declarative transaction management, which is built on Spring's abstract transaction abstraction.
Allows users to implement custom sections and use AOP to improve the use of OOP.
Example
We often use the following types
1. AOP based on agent
2. Pure and simple java object facets
3. @Aspect annotation form
4. Let’s apply the Aspcet sections in the injection form one by one.
Let’s write a few basic classes first.
Interface class:
/** * Define an interface*/ public interface Sleepable { /** * Sleeping method*/ void sleep(); } Implementation class:
/** * I implement the sleeping interface*/ public class ChenLliNa implements Sleepable { @Override public void sleep() { // TODO Auto-generated method stub System.out.println("Be good, it's time to go to bed!"); } } Enhancement class:
/** * Define a sleep enhancement to achieve both pre- and post-set*/ public class SleepHelper implements MethodBeforeAdvice, AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("Apply a facial mask before going to bed"); } @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("Dream after sleeping"); } }1. Agent-based AOP
<!-- Create an enhanced advice --> <bean id ="sleepHelper"/> <bean id="lina"/> <!-- Define a point-cutting matches all sleep methods --> <bean id ="sleepPointcut"> <property name="pattern" value=".*sleep"></property> </bean> <!-- Define a section enhancement + point-cutting combination --> <bean id="sleepHelperAdvisor"> <property name="advice" ref="sleepHelper"/> <property name="pointcut" ref="sleepPointcut"/> </bean> <!-- Define a proxy object --> <bean id="linaProxy"> <property name="target" ref="lina"/> <property name="interceptorNames" value="sleepHelperAdvisor"/> <!-- <property name="proxyInterfaces" value="com.tgb.springaop.service.Sleepable"/> --> </bean>
As in the configuration file:
The pattern attribute specifies a regular expression. It matches all sleep methods. Use org.springframework.aop.support.DefaultPointcutAdvisor to combine tangent point and enhancement to form a complete tangent. After the final configuration is completed, a final proxy object is generated through org.springframework.aop.framework.ProxyFactoryBean.
2. Pure and simple java object facets
How to say that purely simple java object facets? In my opinion, it is relative to the first configuration, which does not require the use of a proxy, but automatically scans through the internal mechanism of spring. At this time, our configuration file should be modified as follows:
<!-- Create an enhanced advice --> <bean id ="sleepHelper"/> <!-- Target class --> <bean id="lina"/> <!-- Configure point-cut and notifications --> <bean id ="sleepAdvisor"> <property name="advice" ref="sleepHelper"></property> <property name="pattern" value=".*sleep"/> </bean> <!-- Automatic proxy configuration --> <bean/>
Is it much simpler than the first one? No need to configure the proxy anymore?
3. @Aspect annotation form
Based on our experience, we also know that the form of annotations is simpler than the configuration file. At this time, you need to comment on existing methods or classes:
/** * Add enhancement through annotation*/ @Aspect @Component public class SleepHelper03 { /*@Pointcut("execution(* com.tgb.springaop.service.impl..*(..))")*/ @Pointcut("execution(* *.sleep(..))") public void sleeppoint(){} @Before("sleeppoint()") public void beforeSleep(){ System.out.println("Apply facial mask before sleep"); } @AfterReturning("sleeppoint()") public void afterSleep(){ System.out.println("Dream after sleeping"); }
Just write in the configuration file:
<!--Scan package--> <context:component-scan base-package="com.tgb" annotation-config="true"/> <!-- ASPECTJ annotation-> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- Target class--> <bean id="lina"/>
4. Aspcet cut surfaces in injection form
I personally feel that this is the simplest, most commonly used, and most flexible. The configuration file is as follows:
<!-- Target class--> <bean id="lina"/> <bean id ="sleepHelper"/> <aop:config> <aop:aspect ref="sleepHelper"> <aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))"/> <aop:after method="afterSleep" pointcut="execution(* *.sleep(..))"/> </aop:aspect> </aop:config>
The SleepHelper02 class mentioned in the configuration file is as follows:
/** * Add enhancement through annotations*/ public class SleepHelper02 { public void beforeSleep(){ System.out.println("Apply a facial mask before sleeping"); } public void afterSleep(){ System.out.println("Dream after sleeping"); } }
Does it look very simple? Do you all use spring aop? !
Regarding how to call, I wrote several test classes here. You can take a look at it. They are basically the same:
/** * Configuration file spring_aop.xml via proxy*/ @Test public void test(){ ApplicationContext ct = new ClassPathXmlApplicationContext("spring_aop.xml"); Sleepable sleeper =(Sleepable) ct.getBean("linaProxy"); sleeper.sleep(); } /** * Configuration file spring_aop_01.xml Short answer java object*/ @Test public void test01(){ ApplicationContext ct = new ClassPathXmlApplicationContext("spring_aop_01.xml"); Sleepable sleeper = (Sleepable)ct.getBean("lina"); sleeper.sleep(); } /** * Configuration file spring_aop_03.xml annotated by aspect*/ @Test public void test03(){ ApplicationContext ct = new ClassPathXmlApplicationContext("spring_aop_03.xml"); Sleepable sleeper = (Sleepable)ct.getBean("lina"); sleeper.sleep(); } /** * Configuration file spring_aop_02.xml configuration file through apsect* @author Chen Lina* @version May 31, 2015 at 10:09:37 am */ @Test public void test02(){ ApplicationContext ct = new ClassPathXmlApplicationContext("spring_aop_02.xml"); Sleepable sleeper = (Sleepable)ct.getBean("lina"); sleeper.sleep(); }
From the test class, we can see that no matter what way we implement aop, their use is no different. The results of these test classes are the same: