

SpringAOP中,通过Advice定义横切逻辑,Spring中支持5中类型的Advice
即AOP在不改变原有代码的情况下,去增加新的功能

即AOP在不改变原有代码的情况下,去增加新的功能
【重点】使用AOP织入,需要在pom.xml导入一个依赖包
- <dependencies>
- <dependency>
- <groupId>org.aspectjgroupId>
- <artifactId>aspectjweaverartifactId>
- <version>1.9.9version>
- dependency>
- dependencies>
1.接口
- public interface UserService {
- public void insert();
- public void delete();
- public void update();
- public void select();
- }
2.接口实现
- public class UserServiceImpl implements UserService{
- @Override
- public void insert() {
- System.out.println("增加了一个用户");
- }
-
- @Override
- public void delete() {
- System.out.println("删除了一个用户");
-
- }
-
- @Override
- public void update() {
- System.out.println("修改了一个用户");
-
- }
-
- @Override
- public void select() {
- System.out.println("查看了一个用户");
- }
- }
- import org.springframework.aop.AfterReturningAdvice;
- import java.lang.reflect.Method;
-
- public class AfterLog implements AfterReturningAdvice {
-
- //returnValue(o):返回值
- //
- @Override
- public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
- System.out.println("执行了" + method.getName() + "方法,返回结果为" + returnValue);
- }
- }
小结:
- import org.springframework.aop.MethodBeforeAdvice;
- import java.lang.reflect.Method;
-
- public class BeforeLog implements MethodBeforeAdvice {
-
-
- //method:要执行的参数目标对象的方法
- //objects(ars):参数
- //target(o):目标对象
- @Override
- public void before(Method method, Object[] args, Object target) throws Throwable {
- System.out.println(target.getClass().getName() +"的"+method.getName()+"被执行了");
- }
- }
小结:
- "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.yuan.service.UserServiceImpl"/>
- <bean id="afterLog" class="com.yuan.log.AfterLog"/>
- <bean id="beforeLog" class="com.yuan.log.BeforeLog"/>
-
- <aop:config>
- <aop:pointcut id="pointcut" expression="execution(* com.yuan.service.UserServiceImpl.*(..))"/>
- <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
- <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
- aop:config>
- beans>
- import com.yuan.service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- //动态代理 代理的是接口
- UserService userService = (UserService) context.getBean("userService");
- userService.insert();
- userService.select();
- }
- }

- public class DiyPointCut {
- public void before(){
- System.out.println("======方法执行前======");
- }
-
- public void after(){
- System.out.println("======方法执行后======");
- }
- }
- "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.yuan.service.UserServiceImpl"/>
- <bean id="afterLog" class="com.yuan.log.AfterLog"/>
- <bean id="beforeLog" class="com.yuan.log.BeforeLog"/>
-
-
- <bean id="diy" class="com.yuan.diy.DiyPointCut"/>
- <aop:config>
- <aop:aspect ref="diy">
- <aop:pointcut id="point" expression="execution(* com.yuan.service.UserServiceImpl.*(..))"/>
- <aop:before method="before" pointcut-ref="point"/>
- <aop:after method="after" pointcut-ref="point"/>
- <aop:around method="around" pointcut-ref="point"/>
- aop:aspect>
- aop:config>
- beans>

- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
-
- //方式三:使用注解方式实现AOP
- @Aspect //标注这个类是一个切面
- public class AnnotationPointCut {
-
- @Before("execution(* com.yuan.service.UserServiceImpl.*(..))")
- public void before(){
- System.out.println("======方法执行前======");
- }
-
- @After("execution(* com.yuan.service.UserServiceImpl.*(..))")
- public void after(){
- System.out.println("======方法执行后======");
- }
-
- //在环绕增强中,我们给定一个参数,代表我们要获取处理切入的点
- @Around("execution(* com.yuan.service.UserServiceImpl.*(..))")
- public void around(ProceedingJoinPoint jp) throws Throwable {
- System.out.println("环绕前");
- Object proceed = jp.proceed();
-
- System.out.println("环绕后");
-
- }
- }
- "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.yuan.service.UserServiceImpl"/>
- <bean id="afterLog" class="com.yuan.log.AfterLog"/>
- <bean id="beforeLog" class="com.yuan.log.BeforeLog"/>
-
-
- <bean id="annotationPointCut" class="com.yuan.diy.AnnotationPointCut"/>
- <aop:aspectj-autoproxy proxy-target-class="false"/>
-
- beans>

小结:

expression="execution(* com.yuan.service.ServiceImpl..*.*(..))