(修改完成)spring 梳理11--注解实现AOP

方法增强=加上日志 

 

 

一、使用AOP织入,需要导入一个依赖包

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.4</version>
</dependency>

 

 

 

二、application.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: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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">

    <!--扫包,com.xinzhi底下都扫扫看-->
    <context:component-scan base-package="com.xinzhi"/>

    <!--第三种方式:注解实现-->
    <aop:aspectj-autoproxy/>




</beans>
application.xml

 

1. 头文件添加aop相关,保证我们在配置文件中可以写相应的东西 

2. 注解实现aop必须添加

<!--第三种方式:注解实现-->
    <aop:aspectj-autoproxy/>

 

3. 记得开扫包

<!--扫包,com.xinzhi底下都扫扫看-->
    <context:component-scan base-package="com.xinzhi"/>

 

 

三、接口 和 类

package com.xinzhi.service;

/**
 * @author sr
 * @date 2021/1/25
 */
public interface IAdminService {
    /**
     * 添加管理员
     * @param name
     */
    void saveAdmin(String name);
}
IAdminService.java
package com.xinzhi.service;

import com.xinzhi.entity.User;

/**
 * @author sr
 * @date 2021/1/24
 */
public interface IUserService {
    /**
     * 获取用户信息
     * @param id
     * @return
     */
    void getUserInfo(int id);
}
IUserService.java
package com.xinzhi.service.impl;

import com.xinzhi.service.IAdminService;
import org.springframework.stereotype.Service;

/**
 * @author sr
 * @date 2021/1/25
 */
@Service
public class AdminServiceImpl implements IAdminService {

    @Override
    public void saveAdmin(String name) {
        System.out.println("这是Admin的添加方法");
    }
}
AdminServiceImpl.java
package com.xinzhi.service.impl;

import com.xinzhi.dao.IUserDao;
import com.xinzhi.entity.User;
import com.xinzhi.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author sr
 * @date 2021/1/24
 */
@Service
public class UserServiceImpl implements IUserService {


    @Override
    public void getUserInfo(int id) {
        System.out.println("这是userService");
    }
}
UserServiceImpl.java

 

 

 

四、LogAdivice

package com.xinzhi.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author sr
 * @date 2021/1/25
 */
@Component
@Aspect
public class LogAdvice {
    //springaop自动的5种aop这里全部列出


    /**
     *前置通知
     */
    @Before("execution(* com.xinzhi.service.impl.*.*(..))")
    public void before(){
        System.out.println("---------方法执行前before()---------");
    }


    /**
     * 后置通知
     */
    @After("execution(* com.xinzhi.service.impl.*.*(..))")
    public void after(){
        System.out.println("---------方法执行后after()---------");
    }



    /**
     * 返回后的通知
     */
    @AfterReturning("execution(* com.xinzhi.service.impl.*.*(..))")
    public void afterReturning(){
        System.out.println("---------方法返回后afterReturning()---------");
    }

    /**
     * 环绕通知
     * @param jp             切点
     * @throws Throwable
     */
    @Around("execution(* com.xinzhi.service.impl.*.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("-------环绕前-------");
        System.out.println("签名(拿到方法名):"+jp.getSignature());

        //执行目标方法proceed
        Object proceed = jp.proceed();

        System.out.println("-------环绕后------");
        System.out.println(proceed);
    }

    /**
     * 异常发生时通知
     */
    @AfterThrowing("execution(* com.xinzhi.service.impl.*.*(..))")
    public void afterThrow() {
        System.out.println("--------------有异常发生-----------------" + new Date());
    }

}

 

 

 

五、测试

 

 @Autowired
    private IUserService userService;
    @Autowired
    private IAdminService adminService;

    @Test
    public void testCreateBean(){
        //加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        userService = applicationContext.getBean(IUserService.class);
        adminService = applicationContext.getBean(IAdminService.class);

        userService.getUserInfo(3);
        System.out.println("------------分割线-------------");
        adminService.saveAdmin("孙锐");
    }

 

 

 

 

 

输出结果:

 

 

 

 

 

 

 

目录

 

 

posted @ 2021-01-24 12:56  Master_Sun  阅读(110)  评论(0编辑  收藏  举报