Understanding and application scenarios of Spring Aop

What is spring Aop

AOP (Aspect-OrientedProgramming, aspect-oriented programming), can be said to be OOP (Object-Oriented Programing, object-oriented programming) supplement and improvement. OOP introduces concepts such as encapsulation, inheritance, and polymorphism to establish an object hierarchy that simulates a collection of common behaviors. When we need to introduce common behavior for distributed objects, OOP is powerless. That is, OOP allows you to define top-to-bottom relationships, but is not suitable for defining left-to-right relationships. For example the log function. Logging code tends to be spread horizontally across all object hierarchies, irrespective of the core functionality of the objects to which it spreads. The same is true for other types of code such as security, exception handling, and transparent persistence. This kind of irrelevant code scattered everywhere is called cross-cutting code, and in OOP design, it leads to a lot of code duplication, which is not conducive to the reuse of various modules.

 

The AOP technology is just the opposite. It uses a technology called "cross-cutting" to dissect the inside of the encapsulated object, and encapsulate the public behavior that affects multiple classes into a reusable module, and name it. For "Aspect", that is, aspect. The so-called "aspect", simply put, is to encapsulate the logic or responsibilities that are not related to the business, but are commonly called by the business modules, so as to reduce the repetitive code of the system, reduce the coupling between modules, and facilitate the future development. operability and maintainability. AOP represents a horizontal relationship. If the "object" is a hollow cylinder, which encapsulates the properties and behavior of the object; then the aspect-oriented programming method is like a sharp blade, cutting these hollow cylinders on to get its internal message. The cut section is the so-called "face". Then it restored these cut-away sections with its ingenious hands, leaving no traces.

 

Using "cross-cutting" techniques, AOP divides the software system into two parts: core concerns and cross-cutting concerns. The main flow of business processing is the core concern, and the less relevant part is the cross-cutting concern. A characteristic of cross-cutting concerns is that they often occur in multiple places in the core concern, but are basically similar everywhere. Such as authorization authentication, logging, transaction processing. The role of Aop is to separate various concerns in the system, separating core concerns and cross-cutting concerns. As Adam Magee, senior solution architect at Avanade, put it, the core idea of ​​AOP is to "separate the business logic in an application from the generic services that support it."

 

The technologies for implementing AOP are mainly divided into two categories: one is to use dynamic proxy technology to decorate the message by intercepting the message to replace the execution of the original object behavior; the other is to use static weaving to introduce A specific syntax creates "aspects" that allow the compiler to weave code about "aspects" during compilation.


How to use spring Aop

 We often use the following

        1. Proxy-based AOP

        2. Pure and simple Java object aspect

        3. In the form of @Aspect annotation

        4. Aspcet facets in the form of injection

 

Spring Aop Application Scenario

Scenario 1: Aop and Things

<aop:aspectj-autoproxy />

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" read-only="false" propagation="REQUIRED" />
<tx:method name="update*" read-only="false" propagation="REQUIRED" />
<tx:method name="delete*" read-only="false" propagation="REQUIRED" />
<tx:method name="create*" read-only="false" propagation="REQUIRED" />
<tx:method name="import*" read-only="false" propagation="REQUIRED" />
<tx:method name="remove*" read-only="false" propagation="REQUIRED" />
<tx:method name="submit*" read-only="false" propagation="REQUIRED" />
<tx:method name="check*" read-only="false" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>

 <aop:advisor pointcut="execution(* com.able.service.*.*(..))" advice-ref="txAdvice" /> 

</aop:config>

场景二:Aop与日志

可以参考http://blog.csdn.net/czmchen/article/details/42392985

 

场景三:Aop与缓存

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326640586&siteId=291194637