SPRING IN ACTION 第4版笔记-第四章Aspect-oriented Spring-001-什么是AOP

一、

Aspect就是把会在应用中的不同地方重复出现的非业务功能的模块化,比如日志、事务、安全、缓存

In software development, functions that span multiple points of an application are
called cross-cutting concerns. Typically, these cross-cutting concerns are conceptually
separate from (but often embedded directly within) the application’s business logic.
Separating these cross-cutting concerns from the business logic is where aspect-
oriented programming ( AOP ) goes to work.

DI helps you decouple application objects from
each other, AOP helps you decouple cross-cutting concerns from the objects they
affect.
Logging is a common example of the application of aspects, but it’s not the only
thing aspects are good for. Throughout this book, you’ll see several practical applica-
tions of aspects, including declarative transactions, security, and caching.

aspects help to modularize cross-cutting concerns. In short, a cross-
cutting concern can be described as any functionality that affects multiple points of an
application. Security, for example, is a cross-cutting concern, in that many methods in
an application can have security rules applied to them. Figure 4.1 gives a visual depic-
tion of cross-cutting concerns.
This figure represents a typical application that’s broken down into modules.
Each module’s main concern is to provide services for its particular domain. But each
module also requires similar ancillary functionality, such as security and transaction
management.

A common object-oriented technique
for reusing common functionality is to
apply inheritance or delegation. But inheri-
tance can lead to a brittle object hierarchy
if the same base class is used throughout an
application, and delegation can be cumber-
some because complicated calls to the dele-
gate object may be required.
Aspects offer an alternative to inheri-
tance and delegation that can be cleaner in
many circumstances. With AOP , you still
define the common functionality in one
place, but you can declaratively define how and where this functionality is applied
without having to modify the class to which you’re applying the new feature. Cross-
cutting concerns can now be modularized into special classes called aspects. This has
two benefits. First, the logic for each concern is in one place, as opposed to being scat-
tered all over the code base. Second, your service modules are cleaner because they
only contain code for their primary concern (or core functionality), and secondary
concerns have been moved to aspects.

时间: 2024-10-30 01:42:07

SPRING IN ACTION 第4版笔记-第四章Aspect-oriented Spring-001-什么是AOP的相关文章

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法

一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automatically create a proxy that delegates calls to either the proxied bean or to the introduction implementation, depending on whether the method called be

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2 配置文件为XML

一. 1.配置文件为xml时则切面类不用写aop的anotation 1 package com.springinaction.springidol; 2 3 public class Magician implements MindReader { 4 private String thoughts; 5 6 public void interceptThoughts(String thoughts) { 7 System.out.println("Intercepting volunteer

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect

一. 1. package concert; public interface CriticismEngine { public String getCriticism(); } 2. 1 package concert; 2 3 public class CriticismEngineImpl implements CriticismEngine { 4 public CriticismEngineImpl() {} 5 6 // injected 7 private String[] cri

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-007-定义切面的around advice

一.注解@AspectJ形式 1. package com.springinaction.springidol; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public cl

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-006-定义切面使用xml

一. 1.其他类和上个例子一样,观众类比使用@AspectJ方式的少了标签及pointcut,before等定义 1 package com.springinaction.springidol; 2 3 public class Audience { 4 public void takeSeats() { //<co id="co_takeSeats"/> 5 System.out.println("The audience is taking their sea

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY简介及用&lt;s:message&gt;和ReloadableResourceBundleMessageSource实现国际化

一. SPRING支持的GENERAL TAG LIBRARY 1. 二.用<s:message>和ReloadableResourceBundleMessageSource实现国际化 1.配置ReloadableResourceBundleMessageSource,它能it has the ability to reload message properties without recompiling or restarting the application. 1 package spi

SPRING IN ACTION 第4版笔记-第六章Rendering web views-001- Spring支持的View Resolver、InternalResourceViewResolver、JstlView

一.Spring支持的View Resolver 二.InternalResourceViewResolver Spring supports JSP views in two ways:? InternalResourceViewResolver ? Spring provides two JSP tag libraries, one for form-to-model binding and one providing general utility features. 1.在java中定义

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、&lt;jpa:repositories&gt;)

一.JpaRepository 1.要使Spring自动生成实现类的步骤 (1)配置文件xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xm

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's Lonely Hearts Club Band", "The Beatles"); } < bean id = "sgtPeppers" class = "soundsystem.BlankDisc" c: _title = &quo