那些年、一起追过的Spring--(5)----AOP

了解: (在概念上和原理上充分理解)

参考视频:http://www.jikexueyuan.com/course/665_3.html?ss=1
1. 什么是AOP
2. AOP的存在价值
3. AOP的原理剖析
4. AOP的关键概念
5. AOP的通俗理解

AOP存在的价值

AOP专门用于处理系统中分布于各个模块中的交叉关注点的问,在JavaEE应用中,常常通过AOP来处理一些具有横切性质的系统级服务,如事务管理,安全检查,缓存,对象池管理等,AOP已经成为了一种非常常用的解决方案;

AOP的原理剖析
AOP代理其实是有AOP框架动态生成的一个对象,该对象可作为目标对象使用,

AOP的关键概念
切面:–Aspect
连接点:–Join Point
通知:–Advice(before,after)
切入点:– Point Cut
引入: – Introduction
目标对象: – Target Object
AOP代理: – AOP Proxy
织入: – Weaving

AOP通俗理解
一个组件A,不关心其他常用的服务组件B,但是这个组件A使用组件B的时候,不是组件A自身去调用,而是通过配置等其他方式,比如Spring中可以通过xml配置文件。这样就使的A压根就不需要知道服务组件B是怎样的,爱存在不存在,爱怎么存在都与A无关。A只关心自己的业务逻辑,具体A使用B的时候,配置文件去做,与具体的A组件无关。


核心概念:

切面,Aspect:其实就是我们需要定义的AOP类,它对功能类似的代码进行封装。


具体实现–XML方式:

导包:
这里写图片描述

实现代码:

package com.spring.aop;

import java.util.Date;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LogAop {

    @Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))")
    public void logBefore(){
        Date date=new Date();
        System.out.println("在方法执行之前执行日志------");
    }

}
package com.spring.dao;

public interface StudentDao {
 public void insert();
 public void update();
}
package com.spring.dao.impl;

import com.spring.dao.StudentDao;

public class StudentDaoImpl implements StudentDao {

 @Override
 public void insert() {
    System.out.println("method insert");
 }

@Override
public void update() {
    System.out.println("update method!");

}

}
package com.spring.services;

import com.spring.dao.StudentDao;

public class StudentServices {
 private StudentDao dao;

 public void setDao(StudentDao dao) {
    this.dao = dao;
 }

 public void insert() {
    dao.insert();
 }

 public void update(){
     dao.update();
 }
}
package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.services.StudentServices;

public class test01 {

    public static void main(String[] args) {

        ApplicationContext context=new ClassPathXmlApplicationContext("bean-aop-annotation.xml");
        StudentServices ss=(StudentServices) context.getBean("studentServices");
        ss.insert();
        ss.update();
    }

}

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:p="http://www.springframework.org/schema/p"
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.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean id="studentServices" class="com.spring.services.StudentServices">
    <property name="dao" ref="studentDao"></property>
</bean>

    <bean id="studentDao" class="com.spring.dao.impl.StudentDaoImpl">
</bean>

    <bean id="logAop" class="com.spring.aop.LogAop"></bean>
</beans>

运行结果:

在方法执行之前执行日志------
method insert
在方法执行之前执行日志------
update method!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值