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.接口:
2.目标类(接口实现类):
3.Aspect类:
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.客户端调用:
6:输出结果:
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()=====