一、什么是AOP
1)面向切面编程(方面),利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
(2)通俗描述:不通过修改源代码方式,在主干功能里面添加新功能
二、AOP(底层原理)
1、有接口情况,使用JDK动态代理
public interface UsersDao {
public int addNum(int a,int b);
public String update(String id);
}
public class UsersDaoImpl implements UsersDao {
@Override
public int addNum(int a, int b) {
return a + b;
}
@Override
public String update(String id) {
return id;
}
}
public class JDKProxy {
//创建接口实现类代理对象
public static void main(String[] args) {
Class[] interfaces = {UsersDao.class};
UsersDao usersDao = (UsersDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UsersDaoProxy(new UsersDaoImpl()));
int add = usersDao.addNum(1,2);
System.out.println(add);
}
}
//创建代理对象
class UsersDaoProxy implements InvocationHandler {
//1 把创建的是谁的代理对象,把谁传递过来
// 有参数构造传递
private Object object;
public UsersDaoProxy(Object object){
this.object = object;
}
//增强逻辑
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//方法之前
System.out.println("方法之前执行"+method.getName() +":传递的参数"+ Arrays.toString(args));
//被增强的方法执行
Object invoke = method.invoke(object, args);
//方法之后
System.out.println("方法之后执行" + invoke);
return invoke;
}
}
2、?没有接口情况,使用CGLIB动态代理
三、AOP操作
(1)AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spirng框架一起使用,进行AOP操作 ?
2、基于AspectJ实现AOP操作
(1)基于xml配置文件实现
/**
* 被增强类
*/
public class Books {
public void buy(){
System.out.println("buy.......");
}
}
/**
* 增强类
*/
public class BooksProxy {
public void before(){
System.out.println("before.........");
}
}
<?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="books" class="com.cjc.spring.aopxml.Books"></bean>
<bean id="booksProxy" class="com.cjc.spring.aopxml.BooksProxy"></bean>
<!--配置aop的增强-->
<aop:config>
<!--切入点-->
<aop:pointcut id="p" expression="execution(* com.cjc.spring.aopxml.Books.buy(..))"/>
<!--切面-->
<aop:aspect ref="booksProxy">
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>
</beans>
public class aopxmlTest {
@Test
public void Test01(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Books books = context.getBean("books", Books.class);
books.buy();
}
}
(2)基于注解方式实现(使用)?
/**
* 被增强的类
*/
@Component
public class Users {
public void add(){
System.out.println("add......");
}
}
/**
* 增强的类
*/
@Component
//生产代理对象
@Aspect
public class UsersProxy {
//相同切入点提取
@Pointcut(value = "execution(* com.cjc.spring.aop.Users.add(..))")
public void point(){
}
//前置通知
//@Before注解表示作为前置通知
@Before(value = "point()")
public void before(){
System.out.println("before........");
}
//后置通知
@AfterReturning(value = "point()")
public void afterReturning(){
System.out.println("afterReturning........");
}
//最终通知
@After(value = "point()")
public void after(){
System.out.println("after........");
}
//异常通知
@AfterThrowing(value = "point()")
public void afterThrowing(){
System.out.println("afterThrowing........");
}
//环绕通知
@Around(value = "point()")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前........");
proceedingJoinPoint.proceed();
System.out.println("环绕之后........");
}
}
<?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">
<!--开启组件扫描-->
<context:component-scan base-package="com.cjc.spring.aop"></context:component-scan>
<!--开启Aspect生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
@Configuration
@ComponentScan(basePackages = {"com.cjc"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AopConfig {
}
|