1
2 1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2
aspectJ的xml版是开发中最常用的:
下面直接已案例入手,毕竟繁琐的日子不多了
案例:两个接口,俩个实现类,一个实现增强的普通类
ISomeService接口:
1
2
3
4
5
6
7
8
9
10
11
12 1package cn.dawn.day20aspectjxml;
2
3/**
4 * Created by Dawn on 2018/3/8.
5 */
6public interface ISomeService {
7 public void insert();
8 public void delete();
9 public void select();
10 public void update();
11}
12
SomeServiceImpl类,上方类的实现类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 1package cn.dawn.day20aspectjxml;
2
3
4/**
5 * Created by Dawn on 2018/3/8.
6 */
7public class SomeServiceImpl implements ISomeService {
8
9 public void insert() {
10 System.out.println("insert ok");
11 }
12
13 public void delete() {
14 System.out.println("delete ok");
15
16 }
17
18 public void select() {
19 System.out.println("select ok");
20
21 }
22
23 public void update() {
24 System.out.println("update ok");
25 }
26}
27
IBookService接口
1
2
3
4
5
6
7
8
9 1package cn.dawn.day20aspectjxml;
2
3/**
4 * Created by Dawn on 2018/3/12.
5 */
6public interface IBookService {
7 public void selectAllBooks();
8}
9
BookServiceImpl类,上面那个接口的实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1package cn.dawn.day20aspectjxml;
2
3/**
4 * Created by Dawn on 2018/3/12.
5 */
6public class BookServiceImpl implements IBookService {
7 public void selectAllBooks() {
8 System.out.println("selectbooks ok");
9
10 int a=5/0;
11 System.out.println(a);
12 }
13}
14
实现增强的普通类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 1package cn.dawn.day20aspectjxml;
2
3
4import org.aspectj.lang.JoinPoint;
5import org.aspectj.lang.ProceedingJoinPoint;
6
7/**
8 * Created by Dawn on 2018/3/12.
9 */
10public class MyAspectJ {
11 /*最后增强,无论是否出现异常都会执行*/
12 public void myAfter(){
13 System.out.println("最终增强");
14 }
15 /*后置增强*/
16 public void myAfterReturning(){
17 System.out.println("后置增强");
18 }
19 /*前置增强*/
20 public void myBefore(){
21 System.out.println("前置增强");
22 }
23 /*前置增强,这种写法可以一会调用出切点表达式,在console打印出来:当然xml文件中另外需要配置一道*/
24 public void myBefore1(JoinPoint jp){
25 System.out.println("前置通知方法jp = " + jp);
26 }
27 /*异常增强*/
28 public void myAfterThrowing(){
29 System.out.println("异常增强");
30 }
31 /*环绕增强*/
32 public void myAround(ProceedingJoinPoint pjp) throws Throwable {
33 System.out.println("环绕增强前");
34 pjp.proceed();
35 System.out.println("环绕增强后");
36 }
37
38}
39
xml中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/aop
9 http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10 <!--要增强的对象-->
11 <bean id="service" class="cn.dawn.day20aspectjxml.SomeServiceImpl"></bean>
12 <bean id="bookservice" class="cn.dawn.day20aspectjxml.BookServiceImpl"></bean>
13 <!--增强的内容-->
14 <bean id="MyAspectJ" class="cn.dawn.day20aspectjxml.MyAspectJ"></bean>
15 <!--aspectjxml的自动代理-->
16 <aop:config>
17 <aop:pointcut id="mypointcut1" expression="execution(* *..day20aspectjxml.*.select(..))"></aop:pointcut>
18 <aop:pointcut id="mypointcut2" expression="execution(* *..day20aspectjxml.*.update(..))"></aop:pointcut>
19 <aop:pointcut id="mypointcut3" expression="execution(* *..day20aspectjxml.*.select*(..))"></aop:pointcut>
20 <aop:pointcut id="mypointcut4" expression="execution(* *..day20aspectjxml.*.delete(..))"></aop:pointcut>
21 <aop:aspect ref="MyAspectJ">
22 <aop:before method="myBefore" pointcut-ref="mypointcut2"></aop:before>
23 <aop:before method="myBefore1(org.aspectj.lang.JoinPoint)" pointcut-ref="mypointcut2"></aop:before>
24 <aop:after method="myAfter" pointcut-ref="mypointcut1"></aop:after>
25 <aop:around method="myAround(org.aspectj.lang.ProceedingJoinPoint)" pointcut-ref="mypointcut4"></aop:around>
26 <aop:after-throwing method="myAfterThrowing" pointcut-ref="mypointcut3"></aop:after-throwing>
27 <aop:after-returning method="myAfterReturning" pointcut-ref="mypointcut1"></aop:after-returning>
28 </aop:aspect>
29 </aop:config>
30
31</beans>
32
这儿的method方法中加了(参数)会报红,不需理会,一会执行没有错误就行
写法的格式就是这样
单测:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 1package cn.dawn.day20aspectjxml;
2
3
4import org.junit.Test;
5import org.springframework.context.ApplicationContext;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8/**
9 * Created by Dawn on 2018/3/3.
10 */
11public class test20180312 {
12 @Test
13 /*aop代理工厂bean异常增强*/
14 public void t01(){
15 ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day20aspectjxml.xml");
16 ISomeService service = (ISomeService) context.getBean("service");
17 IBookService bookservice = (IBookService) context.getBean("bookservice");
18
19 try {
20 bookservice.selectAllBooks();
21 }catch(Exception e){
22 e.printStackTrace();
23 }
24 System.out.println("================观察==================");
25 service.update();
26 System.out.println("================观察==================");
27 service.delete();
28 System.out.println("================观察==================");
29 service.select();
30 System.out.println("================观察==================");
31 }
32}
33
繁琐的东西结束了
我记得我的老师说过一句话,java核心,java能不死,能辉煌这么多年的原因就是Spring,学会Spring可以赚3000,如果你搞java不会Spring,你连3000都赚不到
其中IOC(控制反转)值1000,AOP(面向切面编程)值2000,至此,3000块的东西讲的差不多了,aop也结束了,
下面我还会继续更新博客,Spring的事物,JDBCTemplate,以及整合MyBatis,然后此Spring部分也就差不多完结了,以后有时间再补充更多关于Spring的知识点