Spring见解4 基于注解的AOP配置

5.基于注解的AOP配置

5.1.创建工程

Spring见解4 基于注解的AOP配置_第1张图片

5.1.1.pom.xml


<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">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.bygroupId>
    <artifactId>Spring_AOP_AnnotationartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.1.8.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>5.1.8.RELEASEversion>
        dependency>
    dependencies>
project>

5.1.2.dao

@Repository
public class UserDaoImpl implements UserDao {

    @Override
    public void addUser(){
        System.out.println("insert into tb_user......");
    }
}

5.1.3.service

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public void addUser() {
        userDao.addUser();
    }
}

5.1.4.applicationContext.xml


<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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        		http://www.springframework.org/schema/context
        		http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.by">context:component-scan>
beans>

5.1.5.测试

/**
 * 模拟表现层
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
        //使用对象
        UserService userService = ac.getBean("userServiceImpl",UserService.class);
        userService.addUser();
    }
}

5.2.增强

5.2.1.applicationContext.xml


<aop:aspectj-autoproxy>aop:aspectj-autoproxy>

3.2.3.AOP配置

  1. 常用注解

    • @Aspect:把当前类声明为切面类
    • @Before:前置通知,可以指定切入点表达式
    • @AfterReturning:后置【try】通知,可以指定切入点表达式
    • @AfterThrowing:异常【catch】通知,可以指定切入点表达式
    • @After:最终【finally】通知,可以指定切入点表达式
    • @Around:环绕通知,可以指定切入点表达式
  2. 注解方式实现aop

    @Component
    @Aspect
    public class MyLogger {
    
        @Before("execution(* com.by.service.*.*(..))")
        public void before(){
            System.out.println("方法开始时间:"+new Date());
        }
    
        @After("execution(* com.by.service.*.*(..))")
        public void after(){
            System.out.println("方法结束时间:"+new Date());
        }
    }
    

你可能感兴趣的:(Spring,spring,java,后端)