Spring可以采用Annotation方式实现AOP,也可以采用配置文件方式实现AOP。本文讲解采用配置文件方式。
以配置文件方式实现AOP的时候,切面类就是一个普通的类,需要在配置文件中配置过后才成为切面类。
面向接口编程 UserManager.java:
package com.cos;
public interface UserManager {
public void createUser();
public void readUser();
public void updateUser();
public void deleteUser(int id);
}
接口的实现 UserManagerImpl.java:
package com.cos;
public class UserManagerImpl implements UserManager {
public void createUser() {
System.out.println("-----------------createUser-------------------");
}
public void readUser() {
System.out.println("-----------------readUser-------------------");
}
public void updateUser() {
System.out.println("-----------------updateUser-------------------");
}
public void deleteUser(int id) {
System.out.println("-----------------deleteUser-------------------");
}
}
切面类 SecurityHandler.java:
package com.cos;
//切面类
public class SecurityHandler{
public void checkSecurity(){
System.out.println("----------阿斯多夫--------");
}
}
配置文件 applicationContext.xml:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 切面类,在aop的配置中会用到此id -->
<bean id="securityHandler" class="com.cos.SecurityHandler"/>
<bean id="userManagerImpl" class="com.cos.UserManagerImpl"/>
<!-- spring AOP 配置 -->
<aop:config>
<!-- 配置切面类,ref为切面类的引用,在上面应该已经配置完毕。id任意 -->
<aop:aspect id="aspectid" ref="securityHandler">
<!-- 定义切入点 expression为配置表达式 -->
<aop:pointcut id="pointcutid" expression="execution(* com.cos.UserManagerImpl.create*(..))"/>
<!-- 定义关注点 method为切面类里的方法,pointcut-ref为pointcut的id -->
<aop:before method="checkSecurity" pointcut-ref="pointcutid"/>
</aop:aspect>
</aop:config>
</beans>
测试类 Client.java:
package com.cos;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager) beanFactory.getBean("userManagerImpl");
userManager.createUser();
userManager.updateUser();
userManager.readUser();
}
}
输出结果:
----------阿斯多夫--------
-----------------createUser-------------------
-----------------updateUser-------------------
-----------------readUser-------------------
注:
目标对象UserManagerImpl一定要实现一个接口,原因是spring 的AOP用的就是jdk的动态代理机制,而jdk的动态代理机制要求目标对象一定要实现一个接口,这样代理对象也实现同样的这个接口,便完成了代理功能。
切面类的关注点方法可以添加一个参数 JoinPoint,此参数的实例可以获得目标方法的参数和方法名等:
package com.cos;
import org.aspectj.lang.JoinPoint;
//切面类
public class SecurityHandler{
public void checkSecurity(JoinPoint joinPoint){
System.out.println("method====="+joinPoint.getSignature().getName());
for(Object o : joinPoint.getArgs()){
System.out.println(o.getClass().getName());
}
System.out.println("----------阿斯多夫--------");
}
}
分享到:
相关推荐
使用Spring配置文件实现AOP
Spring之AOP配置文件详解.txt Spring之AOP配置文件详解.txt 帮助你更快张握Java的学习!
spring 配置文件 归类 IOC AOPspring 配置文件 归类 IOC AOP
一个基于配置文件的Spring AOP的实现。实现了前置通知,后置通知,以及拦截器的功能,配置中有详细的注释。
spring-aop标签和配置文件两种方式实例,spring要求2.5及以上
NULL 博文链接:https://coolszy.iteye.com/blog/541997
spring aop demo 通过注解和配置文件的两种实现方式 包含jar包 下载直接运行
包含了spring的ioc,aop,任务调度,cxf、jws示例以及邮件发送跟任务调度。里面包含了需要用到的jar包以及xml配置文件。还有示例代码。很方便学习
博文链接:https://shaqiang32.iteye.com/blog/201915
spring框架使用xml配置文件实现aop
配置可以通过xml文件来进行,大概有四种方式: 1. 配置ProxyFactoryBean,显式地设置advisors, advice, target等(基于代理的AOP ) 2. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从...
NULL 博文链接:https://whp0731.iteye.com/blog/357015
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
从Spring5-AOP的概念->底层原理->相关术语->AOP操作(AspectJ注解)->AOP操作(AspectJ配置文件)说明AOP的相关笔记
NULL 博文链接:https://baobeituping.iteye.com/blog/964399
spring相关配置资料,几乎在里面。包括spring-aop-3.1等文件。
通过修改相关配置文件位置,和导入的架包,终于修复了一些错误,实现了spring和struts2的完美结合,并使用了spring的aop和日志功能,本人亲自测试过,没问题,方便下载使用
@Component("userService")等价于在spring配置文件中定义一个<bean id="userService"/> @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....