1.通知(Advice)
就是你想要的功能,也就是上面说的 安全,事物,日志等。你给先定义好把,然后在想用的地方用一下。
2.连接点(JoinPoint)
这个更好解释了,就是spring允许你使用通知的地方,那可真就多了,基本每个方法的前,后(两者都有也行),或抛出异常时都可以是连接点,spring只支持方法连接点.其他如aspectJ还可以让你在构造器或属性注入时都行,不过那不是咱关注的,只要记住,和方法有关的前前后后(抛出异常),都是连接点。
上面说的连接点的基础上,来定义切入点,你的一个类里,有15个方法,那就有几十个连接点了对把,但是你并不想在所有方法附近都使用通知(使用叫织入,以后再说),你只想让其中的几个,在调用这几个方法之前,之后或者抛出异常时干点什么,那么就用切点来定义这几个方法,让切点来筛选连接点,选中那几个你想要的方法。
切面是通知和切入点的结合。现在发现了吧,没连接点什么事情,连接点就是为了让你好理解切点,搞出来的,明白这个概念就行了。通知说明了干什么和什么时候干(什么时候通过方法名中的before,after,around等就能知道),而切入点说明了在哪干(指定到底是哪个方法),这就是一个完整的切面定义。
5.引入(introduction)
允许我们向现有的类添加新方法属性。这不就是把切面(也就是新方法属性:通知定义的)用到目标类中吗
引入中所提到的目标类,也就是要被通知的对象,也就是真正的业务逻辑,他可以在毫不知情的情况下,被咱们织入切面。而自己专注于业务本身的逻辑。
怎么实现整套aop机制的,都是通过代理,这个一会给细说。
把切面应用到目标对象来创建新的代理对象的过程。有3种方式,spring采用的是运行时,为什么是运行时,后面解释。
Spring AOP的XML实现方式
首先我们定义一个要切入的类,其中有两个方法声明了在某个方法后执行还是在方法前执行
package src.injection.constructor;
import org.springframework.stereotype.Component;
@Component(value="aop")
public class aop {
private void alert() {
// TODO Auto-generated method stub
System.out.println("执行后输出");
}
private void before() {
// TODO Auto-generated method stub
System.out.println("执行前输出");
}
}
然后有一个user实体类,我们要对这个实体类中的get方法进行切入,为了更加明显的显示出aop的作用我们在username的get方法中加入System.out.println("111");之后运行就能明显的看到执行的顺序
package src.injection.constructor;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
public class User {
private String userid;
private List<String> list;
private String username;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
System.out.println("111");
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
XML配置
注意<beans>中要加入一个xmlns:aop = "http://www.springframework.org/schema/aop",否则找不到该标签
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- <context:annotation-config/>
<context:component-scan base-package="src.injection.constructor"></context:component-scan> -->
<bean class="src.injection.constructor.User" id="user">
<property name="username" value="username"></property>
</bean>
<bean class="src.injection.constructor.aop" id="aop"></bean>
<aop:config>
<aop:aspect id = "log" ref = "aop">
<aop:pointcut id = "selectAll" expression = "execution(* get*())"/>
<aop:before pointcut-ref = "selectAll" method = "before"/>
<aop:after pointcut-ref = "selectAll" method = "alert"/>
</aop:aspect>
</aop:config>
</beans>
测试类
package src.injection.constructor;
import java.lang.reflect.Type;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) throws ClassNotFoundException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
User user = (User)context.getBean("user");
System.out.println(user.getUsername());
}
}
运行结果