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 传感器使用 Compass指南针的实现功能
以下是指南针通过方向传感器而旋转实现. CompassDemo.java: package com.example.activity; import android.app.Activity; imp ...
- uva 215 hdu 1455 uvalive5522 poj 1011 sticks
//这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...
- 深入理解java虚拟机---读后笔记(垃圾回收)
运行时数据区,主要包括方法区.虚拟机栈.本地方法栈.堆.程序计数器,该部分内存都是线程隔离的. 然后和其交互的有执行引擎.本地库接口,此部分线程之间是可以共享的. 1. 引用计数算法 给对象添加一个引 ...
- openstack 的 policy 问题。
想写nova的policy的实现, 但是发现网上,有人写的很不错了. ref: http://blog.csdn.net/hackerain/article/details/8241691 但是,po ...
- MySql数据库连接池
1.传统链接(如下为示意图) 注意: (1).传统方式找DriverManager要连接,数目是有限的. (2).传统方式的close(),并没有将Connection重用,只是切断应用程序和数据库的 ...
- cf-A. Wet Shark and Odd and Even(水)
A. Wet Shark and Odd and Even time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 2014 Web开发趋势
本文翻译自:http://www.pixelstech.net/article/1401629232-Web-design-trends-for-2014 如今,已然到了Web横行的时代.越来越多的资 ...
- Android中进行流量统计
// ---------------------流量统计-------------------------------- try { PackageManager pm = getPackageMan ...
- Extjs4 操作TreeStore 处理proxyAjax 获取的数据
近期在搞extjs4 TreeStore时有一个需求 就是要处理一下后台传过来的json数据然后再显示,看api也没有找到解决的方法 ,最后看源代码在Ext.data.proxy.Server 看到这 ...
- GridView隔行样式
<AlternatingRowStyle BorderColor="#FF99CC" BorderStyle="Solid" />