话不多说上代码

项目架构图及Lib包如下:

第二步创建业务类接口

package cn.happy.day01.entity;
/**
* 1.业务接口
* @author Happy
*
*/
public interface ISomeService {
//1.1 执行事务
public void doTransaction();
//1.2 书写日志
public String doLog();
}

第三步实现接口重写接口方法

package cn.happy.day01.entity;

public class SomeServiceImpl implements ISomeService {

    @Override
public void doTransaction() {
System.out.println("开启事务");
} @Override
public String doLog() {
System.out.println("书写日志");
return "abc";
} }

第四步创建aop包定义增强类

package cn.happy.day01.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; @Aspect //该类为切面
public class MyAspect {
//前置通知
@Before(value="execution(public * *(..))")
public void myBefore(){
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:aop="http://www.springframework.org/schema/aop"
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"> <!-- 目标对象 -->
<bean id="someService" class="cn.happy.day01.entity.SomeServiceImpl"></bean> <!-- 切面: -->
<bean id="myAspect" class="cn.happy.day01.aop.MyAspect"></bean> <aop:aspectj-autoproxy/>
</beans>

最后我们写测试类测试

package cn.happy.day01.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.happy.day01.entity.ISomeService;
public class Spring_01Test {
@Test
public void testOne(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("cn/happy/day01/applicationContext.xml");
ISomeService service = (ISomeService)ctx.getBean("someService");
service.doTransaction();
String result = service.doLog();
System.out.println(result);
}
}

ps:

1.切入点表达式
execution(【modifiers-pattern?】 访问修饰符
ret-type-pattern 返回值类型
【declaring-type-pattern?】 全限定性类名
name-pattern(param-pattern) 方法名(参数名)
【throws-pattern?】) 抛出异常类型

切入点表达式要匹配的对象就是目标方法的方法名。所以,execution表达式中明显就是方法的签名。注意:表达式中加[]的部分表示可省略部分,各部分间用空格分开。在其中可以使用以下符号:
符号 意义
* 0至多个任意字符
.. 用在方法参数中,表示任意多个参数
用在包名后,表示当前包及其子包路径
+ 用在类名后,表示当前类及其子类
用在接口后,表示当前接口及其实现类
案例:
execution(public * *(..)) 指定切入点为:任意公共方法
execution(* set*(..)) 指定切入点为:任何一个以"set"开始的方法

ok就这样了试试吧

最新文章

  1. 安装pillow错误的解决方案
  2. Redundant Paths-POJ3177(并查集+双连通分量)
  3. 使用cnpm搭建企业内部私有NPM仓库
  4. php开启新的进程或者线程
  5. Linq 取差集 交集等
  6. MYSQL预处理传参不区分大小写解决办法
  7. 读书笔记——Java IO
  8. C语言范例学习06-上
  9. 爬虫(requests)
  10. How To Size Your Apache Flink&#174; Cluster: A Back-of-the-Envelope Calculation
  11. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
  12. MySQL中group by , sum , case when then 的使用
  13. HDU1211 密文解锁 【扩展欧几里得】【逆元】
  14. Nginx单向认证的安装配置
  15. ibatis.net:第七天,QueryWithRowDelegate
  16. java设置配置session过期时间的方法
  17. .split(&quot;,&quot;, -1);和.split(&quot;,&quot;)的区别
  18. 开源入侵检测系统SELKS系统搭建
  19. Vue如何引入远程JS文件
  20. MVC模式:python案例

热门文章

  1. MYSQL5.7版本sql_mode=only_full_group_by问题
  2. Apache、NGINX支持中文URL
  3. [划分树] POJ 2104 K-th Number
  4. C++变量初始化问题
  5. Bootstrap&lt;基础十四&gt; 按钮下拉菜单
  6. I/O Directory类
  7. 黑马程序员——C语言基础 函数
  8. VS2012 调试时 局部变量显示不全的问题解决
  9. 【转】File类应用 - FilenameFilter 和 FileFilter
  10. Mac上安装与更新Ruby,Rails运行环境