其他分享
首页 > 其他分享> > Spring中的Aop方法增强注解

Spring中的Aop方法增强注解

作者:互联网

Spring中的Aop方法增强注解

Pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Spring</artifactId>
        <groupId>com.msb</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Demo1</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <!--spring切面包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!--aop联盟包-->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <!--Apache Commons日志包-->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

</project>
aspect切面文件
package com.msb.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class aspect {
    //定义公工切点,加入方法
    /*这里是一个公共的切入点*/
    @Pointcut("execution(* com.msb.aspect.service.impl.UserServiceImpl.add*(..))")//aspect下的所以类名所有方法
    public void  addPointCut(){
    }
    /*前置通知,在方法执行前执行*/
    @Before("addPointCut()")
    public void methodBefore(JoinPoint joinPoint){
        System.out.println("@Before前置通知");
    }
    /*后置通知,在方法执行前执行*/
    @After("addPointCut()")
    public void methodAfter(JoinPoint joinPoint){
        System.out.println("@After后置通知");
    }
    @Around("addPointCut()")
    public Object methodround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("Around方法前执行@Around(\"addPointCut()\")");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("Around方法后执行@Around(\"addPointCut()\")");
        return proceed;
    }
    @AfterThrowing(value = "addPointCut()",throwing = "ex")
    public void methodAfterThrowing(Exception ex){
        System.out.println("AfterThrowing异常通知");
    }
    @AfterReturning(value = "addPointCut()",returning = "res")
    public void methodAfterReturning (JoinPoint joinPoint, Object res){
        System.out.println("A
                           fterReturning无异常产生方法加强·通知");
    }
}
多个接口
package com.msb.aspect.dao;
import org.springframework.stereotype.Controller;
public interface EmpDap {
    int addEmp(Integer empno,String ename,String job);
}

package com.msb.aspect.dao;
import org.springframework.stereotype.Controller;
public interface UserDao {
    int addUser(Integer userid,String username);
}

package com.msb.aspect.service;
public interface EmpService {
    int addUser(Integer userid,String username);
}

package com.msb.aspect.service;
public interface UserService {
    int addEmp(Integer empno,String ename,String job);
}

接口对应的实现类
package com.msb.aspect.dao.impl;

import com.msb.aspect.dao.EmpDap;
import org.springframework.stereotype.Repository;

@Repository//这个注解是为了加载EmpDapimpl的相关属性和方法
public class EmpDapimpl implements EmpDap {
    @Override
    public int addEmp(Integer empno, String ename, String job) {
        System.out.println("EmpDapimpl实现EmpDap调用方法addEmp");
        return 1;
    }
}

package com.msb.aspect.dao.impl;
import com.msb.aspect.dao.UserDao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public int addUser(Integer userid, String username) {
        System.out.println("UserDaoImpl实现UserDao调用方法addUser");
        return 1;
    }
}

package com.msb.aspect.service.impl;
import com.msb.aspect.dao.EmpDap;
import com.msb.aspect.dao.UserDao;
import com.msb.aspect.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//service层用@Service注解
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private UserDao userDao;//这里Autowired会自动寻找userDao相关的对象属性和方法
    @Override
    public int addUser(Integer userid, String username) {
        System.out.println("EmpServiceImpl实现EmpService调用方法addUser");
        int rows = userDao.addUser(userid, username);
        return rows;
    }
}

package com.msb.aspect.service.impl;
import com.msb.aspect.dao.EmpDap;
import com.msb.aspect.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.SQLOutput;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private EmpDap empDap;
    @Override
    public int addEmp(Integer empno, String ename, String job) {
        System.out.println("UserServiceImpl实现UserService调用方法addEmp");
        int rows = empDap.addEmp(empno, ename, job);
        return rows;
    }
}

标签:Spring,org,Aop,msb,aspect,注解,import,com,public
来源: https://www.cnblogs.com/188221creat/p/16381541.html