spring AOP简单入门
AOP(aspect oriented programming)面向切面编程。
大致意思是在方法的执行过程中织入其他要执行的方法。
项目结构图

先介绍一下通过代理的方式实现aop,几个文件和上一篇一样,再贴一遍方便观看
package com.ouc.wkp.model;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
User.java
package com.ouc.wkp.dao;
import com.ouc.wkp.model.User;
public interface UserDAO {
public void save(User user);
public void delete();
}
UserDAO.java
package com.ouc.wkp.dao.impl; import org.springframework.stereotype.Component; import com.ouc.wkp.dao.UserDAO;
import com.ouc.wkp.model.User; @Component("u")
public class UserDAOImpl implements UserDAO { @Override
public void save(User user) {
// Hibernate
// JDBC
// XML
// NetWork
System.out.println("user saved!");
// throw new RuntimeException();
} @Override
public void delete() {
// TODO Auto-generated method stub } }
UserDAOImpl.java
package com.ouc.wkp.service; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import com.ouc.wkp.dao.UserDAO;
import com.ouc.wkp.model.User; @Component("userService")
public class UserService { private UserDAO userDAO; @PostConstruct
public void init() {
System.out.println("init");
} public void add(User user) {
userDAO.save(user);
} public UserDAO getUserDAO() {
return userDAO;
} // @Autowired @Qualifier("u")
@Resource(name = "u")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
} @PreDestroy
public void destroy() {
System.out.println("destroy");
}
}
UserService.java
然后是一个代理类,继承自InvocationHandler
package com.ouc.wkp.aop; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; public class LogIntercepter implements InvocationHandler{
private Object target; public Object getTarget() {
return target;
} public void setTarget(Object target) {
this.target = target;
} public void beforeMethod(Method m){
System.out.println(m.getName()+" start");
} public void afterMethod(Method m){
System.out.println(m.getName()+" end");
} public Object invoke(Object proxy,Method m,Object[] args) throws Throwable{
beforeMethod(m);
m.invoke(target, args);
afterMethod(m);
return null;
}
}
LogIntercepter.java
然后再测试类中进行如下调用
@Test
public void testProxy() {
UserDAO userDAO = new UserDAOImpl();
LogIntercepter li = new LogIntercepter();
li.setTarget(userDAO);
UserDAO userDAOProxy = (UserDAO) Proxy.newProxyInstance(userDAO
.getClass().getClassLoader(), userDAO.getClass()
.getInterfaces(), li);
System.out.println(userDAOProxy.getClass());
userDAOProxy.delete();
userDAOProxy.save(new User());
}
UserServiceTest.java
运行结果

然后介绍通过xml配置文件实现aop
使用aop需要一下
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop -->
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy />
还需要这个开启对com.ouc.wkp包下面的扫描
<context:component-scan base-package="com.ouc.wkp"></context:component-scan>
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:annotation-config />
<!-- 使用注解需要下面四条 -->
<!-- xmlns:context="http://www.springframework.org/schema/context" -->
<!-- http://www.springframework.org/schema/context -->
<!-- http://www.springframework.org/schema/context/spring-context-3.1.xsd"> -->
<!-- <context:annotation-config /> --> <!-- 使用aop需要一下 -->
<!-- xmlns:aop="http://www.springframework.org/schema/aop" -->
<!-- http://www.springframework.org/schema/aop -->
<!-- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> -->
<!-- <aop:aspectj-autoproxy /> --> <!-- 扫描 -->
<context:component-scan base-package="com.ouc.wkp"></context:component-scan> <aop:aspectj-autoproxy /> <bean id="logIntercepter2" class="com.ouc.wkp.aop.LogIntercepter2"></bean>
<aop:config>
<!-- <aop:pointcut expression="execution(public * com.ouc.wkp.service..*.add(..))"
id="servicePointcut"/> -->
<!-- <aop:aspect id="logAspect" ref="logIntercepter"> -->
<!-- <aop:before method="before" pointcut="servicePointcut"/> -->
<!-- </aop:aspect> -->
<aop:aspect id="logAspect" ref="logIntercepter2">
<aop:before method="before"
pointcut="execution(public * com.ouc.wkp.service..*.add(..))" />
</aop:aspect>
</aop:config>
</beans>
beans.xml
<bean id="logIntercepter2" class="com.ouc.wkp.aop.LogIntercepter2"></bean>
<aop:config>
<!-- <aop:pointcut expression="execution(public * com.ouc.wkp.service..*.add(..))"
id="servicePointcut"/> -->
<!-- <aop:aspect id="logAspect" ref="logIntercepter"> -->
<!-- <aop:before method="before" pointcut="servicePointcut"/> -->
<!-- </aop:aspect> -->
<aop:aspect id="logAspect" ref="logIntercepter2">
<aop:before method="before"
pointcut="execution(public * com.ouc.wkp.service..*.add(..))" />
</aop:aspect>
</aop:config>
看下面这段 第一行声明了一个代理切入类(表达不准确)
<aop:pointcut expression="execution(public * com.ouc.wkp.service..*.add(..))"
id="servicePointcut"/>标识定义一个切入点pointcut。
<aop:before method="before" pointcut="servicePointcut"/>表示before织入这个切入点。
execution(public * com.ouc.wkp.service..*.add(..))代表com.ouc.wkp.service包下的任意包的任意类的add方法。
然后我们看代理切入类
package com.ouc.wkp.aop; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogIntercepter2 {
// @Pointcut("execution(public * com.ouc.wkp.service..*.add(..))")
public void myMethod(){}; // @Before("myMethod()")
public void before() {
System.out.println("method before");
} // @AfterReturning("execution(public * com.ouc.wkp.dao..*.*(..))")
public void afterReturning(){
System.out.println("method after returning");
} // @AfterThrowing("myMethod()")
public void afterThrowing(){
System.out.println("method after throwing");
} // @Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
}
}
LogIntercepter2.java
类的开头需要加上
@Aspect
@Component
测试方法
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"beans.xml"); UserService service = (UserService) ctx.getBean("userService");
User user = new User();
user.setUsername("qqq");
user.setPassword("123ppp");
//是一个代理
System.out.println(service.getClass());
service.add(user); ctx.destroy(); }
UserServiceTest.java
运行结果

我们看到我们获得的userservice实质上是一个代理类
最后介绍通过注解实现aop,LogIntercepter2的代码再贴一遍.
beans.xml里面的<aop:config>可以注释掉
package com.ouc.wkp.aop; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogIntercepter2 {
@Pointcut("execution(public * com.ouc.wkp.service..*.add(..))")
public void myMethod(){}; @Before("myMethod()")
public void before() {
System.out.println("method before");
} @AfterReturning("execution(public * com.ouc.wkp.dao..*.*(..))")
public void afterReturning(){
System.out.println("method after returning");
} @AfterThrowing("myMethod()")
public void afterThrowing(){
System.out.println("method after throwing");
} @Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
}
}
LogIntercepter2.java
@Pointcut("execution(public * com.ouc.wkp.service..*.add(..))")
public void myMethod(){};表示定义一个切入点
@Before("myMethod()")表示在方法执行前执行注解下面的方法 myMethod()为上面定义的切入点
后面的注解见名知意
运行结果

感觉还是很神奇的 以前一直不理解什么是面向切面。现在简单了解了一下觉得很有趣,但是还是无法用语言准确表达出来= =
下一篇介绍spring整合hibernate
spring AOP简单入门的更多相关文章
- Spring AOP 简单入门笔记 (转)
分享一个自己写的最为简单的Spring AOP的应用,其实,本人也是学习Spring不久,只是把一些个人的理解分享下,供参考.可能很多人刚开始不太理解到底啥是AOP,其实它也是相对 OOP来说的,类似 ...
- Spring AOP初级——入门及简单应用
在上一篇<关于日志打印的几点建议以及非最佳实践>的末尾提到了日志打印更为高级的一种方式——利用Spring AOP.在打印日志时,通常都会在业务逻辑代码中插入日志打印的语句,这实际上是 ...
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- spring security 简单入门
spring security 简单入门示例 一.概述 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架 . 其中最主要的安全操作有两 ...
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...
- Spring.Net 简单入门学习
Spring.NET IoC容器的用法. 通过简单的例子学习Spring.Net 1.先创建一个控制台程序项目. 2.添加IUserInfoDal 接口. namespace Spring.Net { ...
- Spring aop 简单示例
简单的记录一下spring aop的一个示例 基于两种配置方式: 基于xml配置 基于注解配置 这个例子是模拟对数据库的更改操作添加事物 其实并没有添加,只是简单的输出了一下记录 首先看下整个例子的目 ...
- Spring AOP 知识点入门
一.基本知识点 1.AOP概念 AOP(Aspect-Oriented Programming), 即 面向切面编程, 它与 OOP( Object-Oriented Programming, 面向对 ...
随机推荐
- android jar 第三方包
工程交叉了,做相互引用 1.单纯的代码jar 不引用res http://terryblog.blog.51cto.com/1764499/564558 1.2.连带源码一起打包出来 http:// ...
- 改进的延时函数Delay(使用MsgWaitForMultipleObjects等待消息或超时的到来)
解决上一节中延时函数占CPU使用率(达50%)的第二种方法是利用消息机制,通过API函数MsgWaitForMultipleObjects等待消息或超时的到来,从而避免使用循环检测使CPU占用率过高. ...
- C语言入门(9)——局部变量与全局变量
变量有效性的范围称变量的作用域.C语言中所有的量都有自己的作用域.变量说明的方式不同,其作用域也不同. C语言中的变量,按作用域范围可分为两种,即局部变量和全局变量. 局部变量 局部变量也称为内部 ...
- pwd显示链接文件的真实路径
1.pwd用于显示当前工作路径 2.pwd -P 用于显示当前的实际工作路径(主要用于链接文件) 加参数难以了理解,看个例子就明白了: 进入链接文件,pwd显示的是链接文件所在的路径,而你不是链接文件 ...
- 算法精解(C语言描述) 第3章 读书笔记
第3章 递归 1.基本递归 假设想计算整数n的阶乘,比如4!=4×3×2×1. 迭代法:循环遍历其中的每一个数,然后与它之前的数相乘作为结果再参与下一次计算.可正式定义为:n! = (n)(n-1)( ...
- select实现选中跳转
select选择后直接跳转到其他网站的三种方式 第一种: ************************** <html> <head> <meta http- ...
- 查询离指定日期最近的一条数据(oracle)
select * from ( Select * from t_currency_rate where f_orig_curr='USD' and f_dest_curr='RMB ...
- js添加删除元素
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- IO库 8.3
题目:什么情况下,下面的while循环会终止? while(cin >> i) /* ... */ 解答:当读取发生错误时上述while循环会终止.比如i是整形,却输入非整形的数:输入文件 ...
- B - 确定比赛名次
B - 确定比赛名次 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...