Cool
Cool
Published on 2021-07-12 / 22 Visits
0
0

springAop 纯注解

基于spring xml的纯注解

maven 依赖

    <!--sring核心一带四-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
    </dependency>
    <!--测试一定要带-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.6</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.8.RELEASE</version>
    </dependency>

{alert type="success"} 以下二者选其一 {/alert}

{tabs} {tabs-pane label="基于xml的纯注解"}

applicationContext.xml spring配置文件 当使用java类作为spring配置文件的时候就不需要这个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"
       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.liang"></context:component-scan>
    <!--&lt;!&ndash; 开启Spring对AOP的支持 &ndash;&gt;-->
    <aop:aspectj-autoproxy/>
</beans>

{/tabs-pane} {tabs-pane label="基于java类的纯注解"}

spring无需xml的配置类

package com.liang;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan(basePackages="com.liang")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}

{/tabs-pane} {/tabs}

接口

package com.liang;

//业务层接口
public interface CustomerService {
	//保存客户
	public void saveCustomer();
	//修改客户
	public void updateCustomer(Integer id);
}

接口实现类

package com.liang;

import org.springframework.stereotype.Service;

//业务层实现类
@Service
public class CustomerServiceImpl implements CustomerService {
    @Override
    public void saveCustomer() {
        System.out.println("调用持久层,执行保存客户!");
    }

    @Override
    public void updateCustomer(Integer id) {
        System.out.println("调用持久层,执行修改客户:" + id);
    }
}

切面 增强通知类

package com.liang;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//一个记录日志的类
@Component("logger")
@Aspect
public class MyLog {
    @Before("execution(public void com.liang.CustomerServiceImpl.saveCustomer())")
    //@Around("execution(public void com.liang.CustomerServiceImpl.saveCustomer())")
    //@After("execution(* com.liang..*.*(..))")
    public void beforePrintLog() {
        System.out.println("前置通知:MyLog类中的pringLog方法开始记录日志了!!!");
    }
}

单元测试类

package com.liang;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
//基于xml
//@ContextConfiguration(locations = "classpath:applicationContext.xml")
//基于存注解
@ContextConfiguration(classes = SpringConfiguration.class)
@RunWith(SpringRunner.class)
public class Aoptest {
    @Autowired
    private CustomerService customerService;
    @Test
    public void aVoid() {
        customerService.saveCustomer();
    }
}

Comment