18.0、面向切面Aop,以及19.0、注解实现Aop

18.0面向切面AOP

当我们程序写完后,用户又提出想要在完成某项功能的操作前后加一个日志内容显示,这时我们在不改动原代码的情况下,只能使用AOP面向切面技术来添加log日志,和动态代理,理念相似。

UserService.java 接口文件

public interface UserService {
    public void add ();
    public void delete ();
    public void select ();
    public void modify ();
}

UserServiceImpl.java 接口实现类文件

public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("添加了一个用户");
    }
    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }
    @Override
    public void select() {
        System.out.println("查询了一个用户");
    }

    @Override
    public void modify() {
        System.out.println("修改了一个用户");
    }
}

Log.java文件

public class Log implements MethodBeforeAdvice {
//Method:要执行的目标对象的方法
//args:参数
//target:目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"执行了"+method.getName()+"方法");
    }
}

AfterLog.java文件

public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {

        System.out.println(target.getClass().getName()+"执行了方法"+method.getName()+"返回值是"+returnValue);

    }
}

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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
    <bean id="UserService" class="spring.Springaop.serviceImpl.UserServiceImpl" />
    <bean id="Log" class="spring.Springaop.Log" />
    <bean id="AfterLog" class="spring.Springaop.AfterLog" />
<!--方式一:采用原生SpringAIP接口-->
<!--配置aop:需要导入aop的约束-->
    <aop:config>
<!--切入点:execution需要执行的位置(* * * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* hkl.com.ipml.serviceImpl.*(..))" />

        <!--执行环绕增加-->
        <aop:advisor advice-ref="Log" pointcut-ref="pointcut" />
        <aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut" />
    </aop:config>

</beans>

Test.java测试文件

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationConfig.xml");
        //动态代理的是接口,不是接口的实现类
UserService userService = (UserService) context.getBean("UserService");
        userService.add();
    }
}

执行结果如下图所示:

 

18.1、面向切面AOP 2

第二种实现AOP的方式:

Log2.java文件

public class Log2 {
Public void before() {
System.out.println(“在方法执行前执行”);
}

Public void after() {
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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
    <bean id="UserService" class="spring.Springaop.serviceImpl.UserServiceImpl" />
    <bean id="Log" class="spring.Springaop.Log" />
    <bean id="AfterLog" class="spring.Springaop.AfterLog" />
<!--方式二:采用自定义类-->
    <bean id="Log2" class="spring.Springaop.Log2" />
    <aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref=”Log2”>
<!--切入点-->
       <aop:pointcut id="point" expression="execution(* hkl.com.ipml.serviceImpl.*(..))" />
        <!--通知,执行环绕增加-->
        <aop:before advice-ref="before" pointcut-ref="point" />
        <aop:after advice-ref="after" pointcut-ref="point" />
<aop:aspect>
    </aop:config>

</beans>

19.0、用注解实现面向切面AOP

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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="UserService" class="spring.Springaop.serviceImpl.UserServiceImpl" />
    <bean id="Log" class="spring.Springaop.Log" />
    <bean id="AfterLog" class="spring.Springaop.AfterLog" />


    <bean id="UserImpl" class="spring.aop2.UserImpl" />
    <bean id="Pointcutproxy" class="spring.aop2.Pointcutproxy" />
    <aop:aspectj-autoproxy />

</beans>

Pointcutproxy.java文件

@Aspect
public class Pointcutproxy {
    @Before("execution(* spring.aop2.UserImpl.*(..))")
    public void before() {
        System.out.println("======方法执行前======");
    }

    @After("execution(* spring.aop2.UserImpl.*(..))")
    public void after() {
        System.out.println("======方法执行后======");
    }

    @Around("execution(* spring.aop2.UserImpl.*(..))")
    public void around() {
        System.out.println("======执行环绕增强======");
    }
}

测试文件test.java

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationConfig.xml");
        User u = (User) context.getBean("UserImpl");
        u.add();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值