Spring框架中的AOP技术----注解方式
利用AOP技术注解的方式对功能进行增强
CustomerDao接口
package com.alphajuns.demo1;
public interface CustomerDao {
public void save();
public void update();
}
CustomerDaoImpl实现类
package com.alphajuns.demo1;
public class CustomerDaoImpl implements CustomerDao {
@Override
public void save() {
System.out.println("保存客户...");
}
@Override
public void update() {
System.out.println("更新客户...");
}
}
切面类
package com.alphajuns.demo1; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; /*
* 注解方式的切面类
*/
@Aspect
public class MyAspectAnno { /*
* 前置通知
*/
@Before(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
public void log() {
System.out.println("记录日志...");
} /*
* 后置通知
*/
/*@After(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")*/
@After(value="MyAspectAnno.fn()")
public void after() {
System.out.println("更新日志...");
} @Around(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
public void around(ProceedingJoinPoint joinPoint) {
System.out.println("环绕通知1...");
try {
joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("环绕通知2...");
} /*
* 自定义切入点
*/
@Pointcut(value="execution(public * com.alphajuns.demo1.CustomerDaoImpl.save())")
public void fn(){}; }
测试类
package com.alphajuns.demo1; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 { @Resource(name="customerDao")
private CustomerDao customerDao; @Test
public void run1() {
customerDao.save();
} }
配置文件
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启自动代理 -->
<aop:aspectj-autoproxy/> <!-- 创建目标对象 -->
<bean id="customerDao" class="com.alphajuns.demo1.CustomerDaoImpl"/> <!-- 创建切面类 -->
<bean id="myAspectAnno" class="com.alphajuns.demo1.MyAspectAnno"/> </beans>
Spring框架中的AOP技术----注解方式的更多相关文章
- Spring框架中的AOP技术----配置文件方式
1.AOP概述 AOP技术即Aspect Oriented Programming的缩写,译为面向切面编程.AOP是OOP的一种延续,利用AOP技术可以对业务逻辑的各个部分进行隔离,从使得业务逻辑各部 ...
- spring框架中的aop技术
1. 什么是AOP, 面向切面编程 AOP为Aspect Oriented Programming的缩写, 意为:面向切面编程,主要是使各部分之间的耦合度降低, 提高程序的可重用性, 同时提高了开发的 ...
- Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束
(aspect oriented programming面向切面编程) 首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
- Spring框架中的aop操作之二 通过配置文件实现增强
aop表达式写法 配置文件代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...
- Spring Security框架中踢人下线技术探索
1.背景 在某次项目的开发中,使用到了Spring Security权限框架进行后端权限开发的权限校验,底层集成Spring Session组件,非常方便的集成Redis进行分布式Session的会话 ...
- spring aop 使用注解方式总结
spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...
- spring框架中的@Import注解
spring框架中的@Import注解 Spring框架中的@Import注解 在之前的文章中,作者介绍了Spring JavaConfig. 这是除了使用传统的XML文件之外,spring带来的新的 ...
- Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
随机推荐
- Java集合-Map接口相关操作方法
Map接口不是Collection接口的继承.Map接口用于维护键/值对(key/value pairs). 该接口描述了从不重复的键到值的映射. (1) 添加.删除操作: Object put(Ob ...
- 从源代码制作iDempiere Server安装软件(Ubuntu Desktop 12.04 LTS 64位)
怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ 在Eclipse中寻找org.adempiere.server-feature项目 右击 ...
- python生成.exe
python生成.exe 1.在Anaconda Prompt终端输入pip install pyinstaller 2.输入python -m pip install pypiwin32 pytho ...
- LaTeX 在编译时出现 File ended while scanning use of \@writefile错误
LaTeX -在修改论文过程中,重新编译时.出现了File ended while scanning use of \@writefile错误,如以下所示: 问题出现的原因: 因为aux文件没有完整输 ...
- 消息队列 概念 配合SpringBoot使用Demo
转http://www.jianshu.com/p/048e954dab40 概念: 分布式消息队列 ‘分布式消息队列’包含两个概念 一是‘消息队列’,二是‘分布式’ 那么就先看下消息队列的概念,和为 ...
- VPS性能测试
1 cpu硬件参数 cat /proc/cpuinfo 我们可以看到CPU的型号.物理CPU个数(显示0)表示只有1个只有1个物理处理器.CPU核心数(cpu cores)等参数,至少我们需要比较商家 ...
- SQL Like中的逗号分隔符
SQL Like中的逗号分隔符 在与数据库交互的过程中,我们经常需要把一串ID组成的字符串当作参数传给存储过程获取数据.很多时候我们希望把这个字符串转成集合以方便用于in操作. 有两种方式可以方便 ...
- bootstrip可视化布局
http://www.w3cschool.cc/try/bootstrap/layoutit/
- 关于linux PPA源问题
添加PPA: 1.首先进入ubuntu系统,system—>administration—>update manager—>setting,在软件源界面,点击other softwa ...
- log4j容器初始化探究
Log4j容器初始化探究 Log4j第一步就是初始化Logger容器Repository,这一章我们来探究Logger容器,从别从独立应用以及servlet容器下启动初始化两方面探究. 1 独立应用 ...