Spring(九) AOP(二)spring面向切面编程--采用静态配置文件的方式

Spring对AOP的支持([b]采用静态配置文件的方式[/b])

1、spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/commons-logging.jar
* SPRING_HOME/lib/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar

2、配置方法
<aop:config>
<aop:aspect id="securityAspect" ref="securityHandler">
<aop:pointcut id="allAddMethod" expression="execution(* com.bjsxt.spring.*.add*(..)) || execution(* com.bjsxt.spring.*.del*(..))"/>
<aop:before pointcut-ref="allAddMethod" method="checkSecurity"/>
</aop:aspect>
</aop:config>


[b]详细代码如下:[/b]

1.接口:

package com.wlh.spring;

public interface UserManager {
public void addUser(String username,String pwd);
public void delUser(int id);
public void findUser(int id);
public void updateUser(int id ,String username,String pwd);
}



2.目标类(接口实现类):

package com.wlh.spring;

public class UserManagerImpl implements UserManager {

public void addUser(String username, String pwd) {
System.out.println("====addUser()=====");
}

public void delUser(int id) {
System.out.println("====delUser()=====");
}

public void findUser(int id) {
System.out.println("====findUser()=====");
}

public void updateUser(int id, String username, String pwd) {
System.out.println("====updateUser()=====");
}

}



3.Aspect类:


package com.wlh.spring;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class SecurityHandler {

private void checkSecurity() {
System.out.println("-----checkSecurity-------");
}



}




4.配置文件如下:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="userManager" class="com.wlh.spring.UserManagerImpl"/>

<bean id="securityHandler" class="com.wlh.spring.SecurityHandler"/><!-- 切面类 -->
<aop:config>
<!--[color=red]Aspect由Pointcut和Advice组成[/color]
<aop:aspect id="securityAspect" ref="securityHandler"><!-- 引用切面类 -->
<!--[color=red] 声明Pointcut--表达式方法声明匹配被切入的类及其方法[/color] -->
<aop:pointcut id="applyMethod" expression="execution(* com.wlh.spring.*.add*(..)) || execution(* com.wlh.spring.*.del*(..))"/>
<!-- [color=red]声明Advice--即,切面类的要切入方法[/color] -->
<aop:before pointcut-ref="applyMethod" method="checkSecurity"/>
</aop:aspect>

</aop:config>

</beans>

5.客户端调用:

package client;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wlh.spring.UserManager;
public class Client {

public static void main(String []args){

BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager=(UserManager) factory.getBean("userManager");
userManager.addUser("wlh","wlh");

}
}


6:输出结果:

-----checkSecurity-------
====addUser()=====
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值