- 浏览: 1148517 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (411)
- Java Foundation (41)
- AI/机器学习/数据挖掘/模式识别/自然语言处理/信息检索 (2)
- 云计算/NoSQL/数据分析 (11)
- Linux (13)
- Open Source (12)
- J2EE (52)
- Data Structures (4)
- other (10)
- Dev Error (41)
- Ajax/JS/JSP/HTML5 (47)
- Oracle (68)
- FLEX (19)
- Tools (19)
- 设计模式 (4)
- Database (12)
- SQL Server (9)
- 例子程序 (4)
- mysql (2)
- Web Services (4)
- 面试 (8)
- 嵌入式/移动开发 (18)
- 软件工程/UML (15)
- C/C++ (7)
- 架构Architecture/分布式Distributed (1)
最新评论
-
a535114641:
LZ你好, 用了这个方法后子页面里的JS方法就全不能用了呀
页面局部刷新的两种方式:form+iframe 和 ajax -
di1984HIT:
学习了,真不错,做个记号啊
Machine Learning -
赵师傅临死前:
我一台老机器,myeclipse9 + FB3.5 可以正常使 ...
myeclipse 10 安装 flash builder 4.6 -
Wu_Jiang:
触发时间在将来的某个时间 但是第一次触发的时间超出了失效时间, ...
Based on configured schedule, the given trigger will never fire. -
cylove007:
找了好久,顶你
Editable Select 可编辑select
The AspectJ Programming Guide:
http://www.eclipse.org/aspectj/doc/next/progguide/index.html
Spring ref 9 - Aspect Oriented Programming with Spring:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html
引用
CORE concepts:
Aspect: a modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style).
Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.
Advice: action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice. (Advice types are discussed below.) Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join point.
Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.
Introduction: declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)
Target object: object being advised by one or more aspects. Also referred to as the advised object. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.
AOP proxy: an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.
Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.
Types of advice:
Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.
After throwing advice: Advice to be executed if a method exits by throwing an exception.
After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
Aspect: a modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style).
Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.
Advice: action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice. (Advice types are discussed below.) Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join point.
Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.
Introduction: declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)
Target object: object being advised by one or more aspects. Also referred to as the advised object. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.
AOP proxy: an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.
Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.
Types of advice:
Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.
After throwing advice: Advice to be executed if a method exits by throwing an exception.
After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
关于 advice types 的不错解释:
http://stackoverflow.com/questions/12643620/throwing-exceptions-in-spring-aop
引用
try {
//@Before
method();
//@AfterReturning
} catch(Throwable t) {
//@AfterThrowing
} finally {
//@After
}
//@Before
method();
//@AfterReturning
} catch(Throwable t) {
//@AfterThrowing
} finally {
//@After
}
Spring AOP是基于Proxy的,所以无法拦截内部方法调用(internal method call),即下面的对Service.b()的intercept是不起作用的:(google key words:spring aop internal method call)
@Component @Aspect public class Advice { @Around("execution(* somePackages.Service.b(..))") public Object swallowThrowing(ProceedingJoinPoint pjp) { try { return pjp.proceed(); } catch (Throwable e) { //do something } } public class Service { public void a() { b(); //equivalent to this.b(); any call to "this" from within your service instance is directly invoked on that instance and cannot be intercepted by the wrapping proxy (the proxy is not even aware of any such call) } public void b() { } }想要拦截内部方法调用请使用aspectJ的load-time weaving(或者compile time weaving?)。
SPring aop ref 9.6.1 Understanding AOP proxies:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies
Spring AOP top problem #1 - aspects are not applied:
http://denis-zhdanov.blogspot.com/2009/07/spring-aop-top-problem-1-aspects-are.html
http://stackoverflow.com/questions/14111422/spring-aop-does-not-intercept-methods-within-springs-container
http://stackoverflow.com/questions/7338760/spring-aop-intercepting-calls-from-within-the-same-service-class
引用
Once inside a class instance, any method-calls to methods in the same instance will be directly against the actual instance object, not the wrapping proxy so AOP advices will not be considered.
在任意地方(特指不被spring管理、无法获取到ApplicationContext的地方) new 另外一个新的对象,并为 new 出的对象注入 Spring bean:
http://stackoverflow.com/questions/11755152/spring-instrument-and-auto-injection-in-new-object
Injection on new-created objects (rather than managed beans) is only possible with compile- or load-time weaving, not with the run-time proxies that Spring uses by default.
引用
1 load-time weaving:
http://stackoverflow.com/questions/4703206/spring-autowiring-using-configurable
需要注入的类上写上 @Configurable / @Autowired:
2 Compile-time weaving
因为是在编译时织入,所以如果你是用maven来做项目的build的,则需要在项目的 pom.xml 中为项目添加编译时的aspectj支持:
http://www.kubrynski.com/2013/09/injecting-spring-dependencies-into-non.html
https://jira.springsource.org/browse/SPR-6819
spring applicatonContext.xml 中:
http://stackoverflow.com/questions/6522540/maven-ajdt-project-in-eclipse
总结:Load-time weaving 因为是在类加载时织入,所以如果是在web war项目,则需要 web container 支持 Load-time weaving 才行;如果如上面的 tomcat 的例子一样,其本身不支持,则需要改变其依赖的lib和server.xml,这在 prod env 下很可能不是你能决定的,所以,首选 Compile-time weaving。
http://stackoverflow.com/questions/4703206/spring-autowiring-using-configurable
需要注入的类上写上 @Configurable / @Autowired:
//new BackgroundFactory(), 则 由 spring ioc 管理的 UserService 实例会自动被注入进来 @Configurable(preConstruction=true, autowire=Autowire.BY_NAME,dependencyCheck=true) public class BackgroundFactory implements Factory<List<Background>> { private static final Logger logger = LoggerFactory.getLogger(BackgroundFactory.class); @Autowired private UserService userService; @Override public List<Background> create(Integer userId) { List<Background> backgrounds = userService.findBackgroundsByUserId(userId); return backgrounds; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }applicationContext.xml 中:
<context:spring-configured/> <context:load-time-weaver weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" aspectj-weaving="on"/> <context:component-scan base-package="。。。" />在tomcat 中需要做的(否则会报 [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method,参见:http://stackoverflow.com/questions/14176417/spring-maven-project-beancreationexception http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-aj-ltw-environments):
引用
1 copy org.springframework.instrument.tomcat.jar into $CATALINA_HOME/lib;
2 tomcat 的 server.xml 中为 Context 标签指定 spring 的 Loader:
Maven depen:2 tomcat 的 server.xml 中为 Context 标签指定 spring 的 Loader:
<Context docBase="xxx-webapp" path="/xxx-webapp" reloadable="true" source="org.eclipse.jst.jee.server:xxx-webapp"> <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/> </Context>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency>load-time weaving时,spring xml 中 <context:spring-configured/> 和 <context:load-time-weaver /> 两个标签都可以用各自对应的注解 @EnableSpringConfigured 和 @EnableLoadTimeWeaving 来替代,即:移除 spring xml 中这两个标签,而在 BackgroundFactory 类上使用 @EnableSpringConfigured 和 @EnableLoadTimeWeaving:
//new BackgroundFactory(), 则 由 spring ioc 管理的 UserService 实例会自动被注入进来 @EnableSpringConfigured @EnableLoadTimeWeaving(...) @Configurable(preConstruction=true, autowire=Autowire.BY_NAME,dependencyCheck=true) public class BackgroundFactory implements Factory<List<Background>> { ...但需要说明的是,Compile-time weaving 时,不能用 @EnableSpringConfigured!必须是在xml中用 <context:spring-configured/>!@EnableSpringConfigured 这个注解只适用于和 @EnableLoadTimeWeaving 配合使用的 load-time weaving 的情况!
2 Compile-time weaving
因为是在编译时织入,所以如果你是用maven来做项目的build的,则需要在项目的 pom.xml 中为项目添加编译时的aspectj支持:
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.5</version> <configuration> <source>1.6</source> <target>1.6</target> <complianceLevel>1.6</complianceLevel> <Xlint>ignore</Xlint> <showWeaveInfo>true</showWeaveInfo> <aspectLibraries> <aspectLibrary> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </aspectLibrary> </aspectLibraries> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>你应该有些奇怪这里为什么要依赖 persistence-api;这是 spring-aspects 的一个bug;如果不引入persistence-api依赖,则在 m2e 下会报 can't determine annotations of missing type javax.persistence.Entity(最开始我在 m2e 和 mvn 下都报这个错,但后来在不依赖 persistence-api 的情况下 mvn 不报这个错了,m2e 还是报;有些奇怪,尚未搞清楚为什么)。详见:
http://www.kubrynski.com/2013/09/injecting-spring-dependencies-into-non.html
https://jira.springsource.org/browse/SPR-6819
spring applicatonContext.xml 中:
<!-- 如上所说,这里只能用<context:spring-configured />的方式,而不能使用@EnableSpringConfigured --> <context:spring-configured /> <context:component-scan base-package="。。。" />BackgroundFactory 类跟上面的一样,添加 @Configurable & @Autowired 注解即可:
//new BackgroundFactory(), 则 由 spring ioc 管理的 UserService 实例会自动被注入进来 @Configurable(preConstruction=true, autowire=Autowire.BY_NAME, dependencyCheck=true) public class BackgroundFactory implements Factory<List<Background>> { private static final Logger logger = LoggerFactory.getLogger(BackgroundFactory.class); @Autowired private UserService userService; 。。。如果你在eclipse中使用了m2e,会发现m2e无法识别aspectj-maven-plugin这个plugin的 <execution>,会报错误 Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:aspectj-maven-plugin:1.4:compile (execution: default, phase: compile);这是因为你的eclipse中尚未安装 m2t 的 AJDT Connector,参考这里安装即可:
http://stackoverflow.com/questions/6522540/maven-ajdt-project-in-eclipse
引用
Step1:install AJDT(AspectJ development tools) in eclipse
Step2: install "AJDT m2e Configurator", repo url:http://dist.springsource.org/release/AJDT/configurator/
Step2: install "AJDT m2e Configurator", repo url:http://dist.springsource.org/release/AJDT/configurator/
另外,如果你是在一个可获取到 ApplicationContext 的 spring bean 中 new 一个对象,并想将一些依赖注给该new出的对象,可以使用 AutowireCapableBeanFactory:
http://stackoverflow.com/questions/3813588/how-to-inject-dependencies-into-a-self-instantiated-object-in-spring
http://stackoverflow.com/questions/3693971/can-i-inject-a-java-object-using-spring-without-any-xml-configuration-files
http://stackoverflow.com/questions/129207/getting-spring-application-context
good stuffs:
Spring AOP: Dynamic Proxies vs. CGLib proxies:
http://insufficientinformation.blogspot.com/2007/12/spring-dynamic-proxies-vs-cglib-proxies.html
一个 After Throwing Advice 的例子:
http://www.captaindebug.com/2011/09/using-aspectjs-afterthrowing-advice-in.html#.UOjMtbTMh1N
Logging Standard Exceptions with Spring AOP:
http://tech.tejusparikh.com/post/10920309943/logging-standard-exceptions-with-spring-aop
发表评论
-
Lucene & Solr
2013-05-07 17:30 2423Params of solr query (参见 solrj ... -
Continuous Integration Server:Jenkins & Hudson
2013-04-15 16:15 1466Jenkins: http://jenkins-ci.org/ ... -
Spring Integration
2013-03-26 16:52 3042Spring Integration Reference ... -
高可用与负载均衡:Haproxy(or lVS) + keepalived
2013-01-29 20:35 3186sources: Setting Up A High ... -
Spring Batch: 大数据量批量并行处理框架
2013-01-11 16:19 4841Spring Batch Documentati ... -
Performance & Load test tool : JMeter
2012-12-18 14:28 1290Official: http://jmeter.apa ... -
rabbitmq & spring amqp
2012-12-04 00:09 8713My main AMQP post on blogger ... -
javaMail 邮件
2012-11-23 20:14 3495SMTP POP3的区别到底是什么? http://w ... -
未完 Spring MVC
2012-11-15 22:41 2122Annotations for Http pa ... -
JUnit 单元测试
2012-10-30 12:27 2569测试的分类: http://s ... -
Hibernate
2011-08-02 11:48 1184Hibernate缓存: 一级缓存的生命周期和session的 ... -
Maven Repository Management & Nexus
2011-07-30 11:39 1434Why do I need a Repositor ... -
XStream
2011-07-13 00:18 1380XStream 内置 Converters: http://x ... -
Hibernate注解: 联合主键:@IdClass vs @EmbeddedId
2011-07-12 21:01 15383Hibernate Annotations -> 2.2 ... -
Struts2 Tag 标签
2011-05-04 00:43 1640struts2 OGNL 中的#、%等符号的区别: Apach ... -
Spring
2011-04-07 19:10 2898Spring API & Reference: htt ... -
iBATIS cacheModel 缓存
2011-04-07 14:29 1353http://ibatis.apache.org/docs/d ... -
Template Engine: FreeMarker & Velocity
2011-01-16 22:56 4111FreeMarker http://freemar ... -
Struts2 标签 <s:url>中添加多个<s:param>的奇怪问题 待整理
2011-01-14 13:27 1917问题描述(我的回复): http://went3456.ite ... -
J2EE核心技术
2010-12-31 01:16 2656http://www.berheley.com/jsnl/J2 ...
相关推荐
标题 "aop:aspect" 指涉的是Spring框架中的面向切面编程(Aspect-Oriented Programming, AOP)的一个核心概念。在Spring AOP中,`aop:aspect`是XML配置中定义一个切面的元素,它允许我们将关注点(如日志、事务管理...
Aspect-oriented software design (ASOD), which follows the Java philosophy of "write once, run anywhere", is a new way of thinking about program construction. It is a rapidly growing and evolving ...
AOP(Aspect-Oriented Programming)的核心思想是将应用程序中的关注点分离为独立的模块,称为方面(Aspects)。这有助于提高代码的可读性、可维护性和重用性。在面向对象编程中,横切关注点往往分散在多个类和方法...
### 面向切面编程(Aspect-Oriented Programming, AOP) #### 一、概述 面向切面编程(Aspect-Oriented Programming, AOP)是一种编程范式,旨在提高程序模块化程度,通过分离关注点(Separation of Concerns, SoC...
Spring 2.5 是 Spring 框架的一个重要版本,它在面向切面编程(Aspect-Oriented Programming,AOP)方面提供了强大的支持。面向切面编程是一种编程范式,旨在将关注点分离,使代码更加模块化,尤其是处理那些跨越多...
在Spring框架中,面向切面编程(Aspect Oriented Programming,简称AOP)是一种重要的设计模式,它扩展了传统的面向对象编程(OOP),使得我们可以将关注点分离,特别是那些横切关注点,如日志、事务管理、权限检查...
在IT行业中,面向切面编程(Aspect Oriented Programming,简称AOP)是一种强大的设计模式,它允许程序员将关注点从核心业务逻辑中分离出来,比如日志记录、事务管理等。Spring框架是Java领域实现AOP的一个流行工具...
Aspect-Oriented Programming 面向方面编程 简称AOP AOP所要做的事情就是从系统中分离出方面,然后集中实现,从面可以独立编写业务逻辑代码,在系统运行的时候,再将方面代码“织入”到系统中
Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它允许我们通过分离关注点来简化应用程序的开发。在传统的面向对象编程中,业务逻辑与日志记录、事务管理、性能监控等横切...
Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它提供了一种模块化和声明式的方式来处理横切关注点,如日志、事务管理、安全性等。这些关注点通常会分散在应用程序的多个类...
其中,Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它提供了一种处理横切关注点的有效方式,使得代码更加模块化,易于维护。本文将基于"LYFspringAOP系列:自己写的spring...
在Spring框架中,注解和XML配置是两种主要的方式来实现面向切面编程(Aspect-Oriented Programming,简称AOP)。AOP是一种编程范式,它允许程序员定义“切面”,这些切面封装了关注点,如日志、事务管理、性能监控等...
在Spring框架中,AOP(Aspect Oriented Programming,面向切面编程)是一种强大的设计模式,它允许程序员将关注点从核心业务逻辑中分离出来,如日志、事务管理等。在"day39-Spring 06-Spring的AOP:带有切点的切面...
在Spring框架中,AOP(Aspect Oriented Programming,面向切面编程)是一种强大的设计模式,它允许程序员将关注点分离,比如日志、事务管理等,从核心业务逻辑中解耦出来。本篇文章将深入探讨如何通过Spring的静态...
AOP(Aspect Oriented Programming)是一种编程思想,是对传统的面向对象编程(OOP)的补充。在OOP中,我们关注的是对象和对象之间的关系;而在AOP中,关注点被分解为独立的“切面”,这些切面可以包含业务逻辑中的...
Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它提供了一种管理横切关注点(如日志、事务管理等)的有效方式。通过将这些关注点与核心业务逻辑分离,AOP有助于保持代码的...
AOP(Aspect Oriented Programming,面向切面编程)是Spring框架中的一个重要概念,它提供了一种将关注点(如日志、事务管理等)与核心业务逻辑分离的方式,从而实现代码的模块化和可重用性。AOP通过定义切面...
在软件开发中,面向切面编程(Aspect-Oriented Programming,简称AOP)是一种强大的设计模式,它允许我们把关注点分离到不同的切面,使得代码更加模块化,易于维护。Spring框架是AOP应用最广泛的平台之一,特别是...
在Spring框架中,面向切面编程(Aspect Oriented Programming,简称AOP)是一种强大的设计模式,它允许我们定义横切关注点,如日志、事务管理、权限检查等,然后将这些关注点与核心业务逻辑解耦。这篇教程将详细讲解...
AOP(Aspect Oriented Programming,面向切面编程)是一种编程思想,它的核心在于将程序中的横切关注点(Cross-cutting Concerns)从业务逻辑中抽离出来,形成独立的组件,从而使得业务逻辑更加纯粹,易于维护。...