Spring中AOP最简单实例-@注解形式

                      Spring中AOP最简单实例-@注解形式

一、项目结构

二、基于maven(引入Jar包)

在引入pom文件时,需要注意:

第一,因为使用的是结合Spring的,要引入Spring的pom。统一版本为5.1.5.RELEASE。

<properties>
    <spring.version>5.1.5.RELEASE</spring.version>
</properties>
spring相关Jar包引入
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

第三,用到测试类,引入junit 测试包

aspect相关Jar包
         <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>

第四,用到日志,加入log4j

日志相关Jar
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

三、实体类(需要被切面切的类)--HelloWorld

package com.isoftstone.mcb;

import org.apache.log4j.Logger;

public class HelloWorld {

    private final static Logger logger = Logger.getLogger(HelloWorld.class);

    private String name;

    public String getName() {
        logger.info("我的名字是 "+ name);
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void printException(){
        logger.info("我扔出了一个IllegalArgumentException异常");
        throw new IllegalArgumentException();
    }

}

四、切面类(切入到别人的类)--注解类--AspectAop

package com.isoftstone.mcb;

import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.*;

@Aspect
public class AspectAop {
    private final static Logger logger = Logger.getLogger(HelloWorld.class);
    @Pointcut("execution(* com.isoftstone.mcb.HelloWorld.*(..))")
    private void aspectAll(){
    }
    @Before("aspectAll()")
    public void beforeAdvice(){
        logger.info("我是 beforeAdvice ");
    }
    @After("aspectAll()")
    public void afterAdvice(){
        logger.info("我是 afterAdvice ");
    }
    @AfterReturning(pointcut = "aspectAll()", returning = "returnValue")
    public void afterReturning(Object returnValue) {
        logger.info("afterReturning,返回值"+ returnValue);
    }
    @AfterThrowing(pointcut = "aspectAll()", throwing = "ex")
    public void afterThrowing(Exception ex) {
        logger.info("我捕获了一个异常信息 "+ ex.toString());
    }
}

五、配置类(Spring管理)--注解类--SpringConfig

package com.isoftstone.config;

import com.isoftstone.mcb.AspectAop;
import com.isoftstone.mcb.HelloWorld;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
public class SpringConfig {
    @Bean
    public HelloWorld helloWorld(){
        HelloWorld helloWorld  = new HelloWorld();
        helloWorld.setName("lisi");
        return helloWorld;
    }
    @Bean
    public AspectAop aspectAop() {
        return new AspectAop();
    }
}

六、log4j配置输出到控制台-log4j.properties

# Define the root logger with appender file
log4j.rootLogger = info,stdout,D,E

### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

七、测试类--AopTest

package com.isoftstone;

import com.isoftstone.config.SpringConfig;
import com.isoftstone.mcb.HelloWorld;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AopTest {
    @Test
    public void testAop() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        HelloWorld helloWorld = applicationContext.getBean(HelloWorld.class);
        helloWorld.getName();
        helloWorld.printException();
    }

}

八、测试结果

log4j:ERROR Could not instantiate appender named "E".
我是 beforeAdvice 
我的名字是 lisi
我是 afterAdvice 
afterReturning,返回值lisi
我是 beforeAdvice 
我扔出了一个IllegalArgumentException异常
我是 afterAdvice 
我捕获了一个异常信息 java.lang.IllegalArgumentException

java.lang.IllegalArgumentException

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JAVA和人工智能

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值