配置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>

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


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

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

除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。

您可以自由: 复制、发行、展览、表演、放映、广播或通过信息网络传播本作品 创作演绎作品

惟须遵守下列条件: 署名 — 您必须按照作者或者许可人指定的方式对作品进行署名。

你可能感兴趣的:(AOP,xml,职场,休闲)