AOP(面向切面编程) 什么是AOP? AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP的实现一:使用SpringAPI实现 导入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
编写业务和接口
package com.web.service;
public interface Service {
void add();
void delete();
void modify();
void query();
}
package com.web.service;
public class UserService implements Service{
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void modify() {
System.out.println("修改了一个用户");
}
@Override
public void query() {
System.out.println("查询了一个用户");
}
}
编写增强类
package com.web.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"执行了"+method.getName()+"方法");
System.out.println("返回值"+returnValue);
}
}
package com.web.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"执行了"+method.getName()+"方法");
}
}
配置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
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.web.service.UserService" />
<bean id="log" class="com.web.log.Log" />
<bean id="afterLog" class="com.web.log.AfterLog" />
<aop:config>
<aop:pointcut id="service" expression="execution(* com.web.service.Service.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="service"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="service" />
</aop:config>
</beans>
AOP实现方式二:使用自定义类
创建切入类
package com.web.log;
public class Log {
public void after(){
System.out.println("--------method after--------");
}
public void before(){
System.out.println("--------method before--------");
}
}
进行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
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="log" class="com.web.log.Log"/>
<bean id="userService" class="com.web.service.UserService"/>
<aop:config>
<aop:aspect id="log" ref="log">
<aop:pointcut id="point" expression="execution(* com.web.service.UserService.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
AOP实现方式三:使用注解实现 1.编写增强类
package com.web.point;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class Point {
@After("execution(* com.web.service.UserService.*(..))")
public void after(){
System.out.println("--------------After---------------");
}
@Before("execution(* com.web.service.UserService.*(..))")
public void before(){
System.out.println("--------------Before--------------");
}
}
2.进行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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy/>
<bean id="service" class="com.web.service.UserService"/>
<bean id="point" class="com.web.point.Point"/>
</beans>
|