配置applicationContext.xml实现AOP

昨天是使用Annotation注解的方式实现的AOP,今天再将其改为使用配置文件来实现。使用这种方法的话,SecurityHandler类就又少了一大截的代码,只需要提供一个用于标识切点的方法即可。

package cn.ineeke.spring;
public class SecurityHandler {
private void printSomthing(){
System.out.println("--------Security----------");
}
}

虽说类中是不需要使用注解了,但是这个东西肯定是得有的,只不过是将其转移到applicationContext.xml文件中了而已。使用标签配置好DAO类和SecurityHandler类。

使用标签开始配置AOP。标签用于指定哪个bean是切面。标签用于指定切点。标签在这里用于定义Advice,同前面所说的一样,这里还可以是等等。

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="securityHandler" class="cn.ineeke.spring.SecurityHandler"/>
<bean id="userDAO" class="cn.ineeke.spring.UserDAOImpl"/>
<aop:config>
<aop:aspect id="securityAspect" ref="securityHandler">
<aop:pointcut id="addMethod" expression="execution(* *(..))"/>
<aop:before method="printSomthing" pointcut-ref="addMethod"/>
</aop:aspect>
</aop:config>
</beans>

调用方法同之前的一样,这样看起来应该比昨天那个清晰的多了,看标签也比较好容易理解。


除非另有声明,本站遵循【署名-非商业性使用-相同方式共享 3.0 共享协议】授权。

转载原创文章请注明,转载自:Neeke[http://www.ineeke.com]

本文链接: http://www.ineeke.com/archives/applicationcontext-aop/

2009年1月29日 | 归档于 J2EE技术 | 2 条评论
  1. 2009年2月2日 02:04 | #1

    谢谢楼主。新年快乐

  2. 2009年2月2日 00:35 | #2

    够专业,这也难怪谷歌对你的博客评价这么高。

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
n:-zy n:-zr n:-zan n:-xf n:-wx n:-tz n:-tt n:-ts n:-sy n:-st n:-ss n:-sk n:-qd n:-pz n:-lh n:-kun n:-ku n:-hx n:-hd n:-gt n:-gg n:-bz

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!