一、目录结构

二、代码

1、SpringConfiguration

 1 package cn.bijian.config;
 2 
 3 import org.springframework.context.annotation.ComponentScan;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 6 
 7 @Configuration
 8 @ComponentScan(basePackages = "cn.bijian")
 9 @EnableAspectJAutoProxy
10 public class SpringConfiguration {
11 
12 }

2、Account

 1 package cn.bijian.model;
 2 
 3 public class Account {
 4     private Integer id;
 5     private String name;
 6     private Double money;
 7 
 8     public Integer getId() {
 9         return id;
10     }
11 
12     public void setId(Integer id) {
13         this.id = id;
14     }
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public Double getMoney() {
25         return money;
26     }
27 
28     public void setMoney(Double money) {
29         this.money = money;
30     }
31 
32     @Override
33     public String toString() {
34         return "Account{" +
35                 "id=" + id +
36                 ", name='" + name + '\'' +
37                 ", money=" + money +
38                 '}';
39     }
40 }

3、AccountService

 1 package cn.bijian.service;
 2 
 3 
 4 public interface AccountService {
 5 
 6     void saveAccount();
 7 
 8     void updateAccount(int i);
 9 
10     int deleteAccount();
11 }

4、AccountServiceImpl

 1 package cn.bijian.service.impl;
 2 
 3 import cn.bijian.service.AccountService;
 4 import org.springframework.stereotype.Service;
 5 
 6 @Service("accountService")
 7 public class AccountServiceImpl implements AccountService {
 8     @Override
 9     public void saveAccount() {
10         System.out.println("执行了保存");
11     }
12 
13     @Override
14     public void updateAccount(int i) {
15         System.out.println("执行了更新"+i);
16     }
17 
18     @Override
19     public int deleteAccount() {
20         System.out.println("执行了删除");
21         return 0;
22     }
23 }

5、Logger

 1 package cn.bijian.utils;
 2 
 3 
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.annotation.*;
 6 import org.springframework.stereotype.Component;
 7 
 8 @Component("logger")
 9 @Aspect //当前类是一个切面类
10 /*
11     注解前置、后置、异常、最终有顺序问题,而环绕通知没有这个问题!
12  */
13 public class Logger {
14     @Pointcut("execution(* cn.bijian.service.impl.*.*(..))")
15     private void pt(){
16 
17     }
18 
19     @Before("pt()")
20     public void beforePrintLog(){
21         System.out.println("前置:开始记录日志!!!");
22     }
23 
24     @AfterReturning("pt()")
25     public void afterReturningPrintLog(){
26         System.out.println("后置:开始记录日志!!!");
27     }
28 
29     @AfterThrowing("pt()")
30     public void afterThrowingPrintLog(){
31         System.out.println("异常:开始记录日志!!!");
32     }
33 
34     @After("pt()")
35     public void afterPrintLog(){
36         System.out.println("最终:开始记录日志!!!");
37     }
38 
39 //    @Around("pt()")
40     public Object aroundPrintLog(ProceedingJoinPoint pjp){
41         Object rtValue = null;
42         try {
43             Object[] args = pjp.getArgs();
44             System.out.println("前置:开始记录日志!!!");
45             rtValue = pjp.proceed(args);    //调用业务层方法(切入点方法)
46             System.out.println("后置:开始记录日志!!!");
47             return rtValue;
48         }catch (Throwable e){
49             System.out.println("异常:开始记录日志!!!");
50             throw new RuntimeException(e);
51         }finally {
52             System.out.println("最终:开始记录日志!!!");
53             System.out.println("环绕:开始记录日志!!!");
54         }
55     }
56 }

6、bean.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         https://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop
 9         https://www.springframework.org/schema/aop/spring-aop.xsd
10         http://www.springframework.org/schema/context
11         https://www.springframework.org/schema/context/spring-context.xsd">
12 
13 <!--    <context:component-scan base-package="cn.bijian"></context:component-scan>-->
14     <!--开启注解AOP的支持-->
15 <!--    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>-->
16 
17 
18 <!--    <bean id="accountService" class="cn.bijian.service.impl.AccountServiceImpl"></bean>-->
19 <!--    <bean id="logger" class="cn.bijian.utils.Logger"></bean>-->
20 <!--    &lt;!&ndash;AOP配置&ndash;&gt;-->
21 <!--    <aop:config>-->
22 <!--        &lt;!&ndash;pointcut:对业务层哪些方法进行增强  访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)&ndash;&gt;-->
23 <!--        <aop:pointcut id="pt" expression="execution(* cn.bijian.service.impl.*.*(..))"/>-->
24 <!--        &lt;!&ndash;配置切面&ndash;&gt;-->
25 <!--        <aop:aspect id="logAdvice" ref="logger">-->
26 <!--            &lt;!&ndash;通知方法和切入点方法关联&ndash;&gt;-->
27 <!--            <aop:before method="beforePrintLog" pointcut-ref="pt"></aop:before>-->
28 <!--            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt"></aop:after-returning>-->
29 <!--            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt"></aop:after-throwing>-->
30 <!--            <aop:after method="afterPrintLog" pointcut-ref="pt"></aop:after>-->
31 <!--&lt;!&ndash;            <aop:around method="aroundPrintLog" pointcut-ref="pt"></aop:around>&ndash;&gt;-->
32 <!--        </aop:aspect>-->
33 <!--    </aop:config>-->
34 
35 </beans>

7、AccountServiceTest

 1 package cn.bijian;
 2 
 3 import cn.bijian.config.SpringConfiguration;
 4 import cn.bijian.service.AccountService;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.test.context.ContextConfiguration;
 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 
11 @RunWith(SpringJUnit4ClassRunner.class)
12 //@ContextConfiguration(locations = "classpath:bean.xml")
13 @ContextConfiguration(classes = SpringConfiguration.class)
14 public class AccountServiceTest {
15     @Autowired
16     private AccountService accountService;
17 
18     @Test
19     public void testSaveAccount(){
20         accountService.saveAccount();
21     }
22 }

8、pom.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4   <modelVersion>4.0.0</modelVersion>
  5 
  6   <groupId>cn.bijian</groupId>
  7   <artifactId>Spring_review_4</artifactId>
  8   <version>1.0-SNAPSHOT</version>
  9 
 10   <properties>
 11     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 12     <maven.compiler.source>1.7</maven.compiler.source>
 13     <maven.compiler.target>1.7</maven.compiler.target>
 14   </properties>
 15 
 16   <dependencies>
 17     <dependency>
 18       <groupId>org.mybatis</groupId>
 19       <artifactId>mybatis</artifactId>
 20       <version>3.5.6</version>
 21     </dependency>
 22 
 23     <dependency>
 24       <groupId>mysql</groupId>
 25       <artifactId>mysql-connector-java</artifactId>
 26       <version>8.0.21</version>
 27     </dependency>
 28 
 29     <dependency>
 30       <groupId>log4j</groupId>
 31       <artifactId>log4j</artifactId>
 32       <version>1.2.17</version>
 33     </dependency>
 34 
 35     <dependency>
 36       <groupId>junit</groupId>
 37       <artifactId>junit</artifactId>
 38       <version>4.12</version>
 39       <scope>test</scope>
 40     </dependency>
 41 
 42     <dependency>
 43       <groupId>org.apache.tomcat.maven</groupId>
 44       <artifactId>tomcat7-maven-plugin</artifactId>
 45       <version>2.2</version>
 46     </dependency>
 47 
 48     <dependency>
 49       <groupId>org.slf4j</groupId>
 50       <artifactId>slf4j-log4j12</artifactId>
 51       <version>1.6.6</version>
 52     </dependency>
 53 
 54     <dependency>
 55       <groupId>dom4j</groupId>
 56       <artifactId>dom4j</artifactId>
 57       <version>1.6.1</version>
 58     </dependency>
 59 
 60     <dependency>
 61       <groupId>jaxen</groupId>
 62       <artifactId>jaxen</artifactId>
 63       <version>1.1.6</version>
 64     </dependency>
 65 
 66     <dependency>
 67       <groupId>org.springframework</groupId>
 68       <artifactId>spring-context</artifactId>
 69       <version>5.0.3.RELEASE</version>
 70     </dependency>
 71 
 72     <dependency>
 73       <groupId>c3p0</groupId>
 74       <artifactId>c3p0</artifactId>
 75       <version>0.9.1.2</version>
 76     </dependency>
 77 
 78     <dependency>
 79       <groupId>commons-dbutils</groupId>
 80       <artifactId>commons-dbutils</artifactId>
 81       <version>1.7</version>
 82     </dependency>
 83 
 84     <dependency>
 85       <groupId>org.springframework</groupId>
 86       <artifactId>spring-test</artifactId>
 87       <version>5.0.2.RELEASE</version>
 88     </dependency>
 89 
 90     <dependency>
 91       <groupId>org.springframework</groupId>
 92       <artifactId>spring-aop</artifactId>
 93       <version>5.0.3.RELEASE</version>
 94     </dependency>
 95 
 96     <dependency>
 97       <groupId>org.springframework</groupId>
 98       <artifactId>spring-core</artifactId>
 99       <version>5.0.3.RELEASE</version>
100     </dependency>
101 
102     <dependency>
103       <groupId>org.aspectj</groupId>
104       <artifactId>aspectjweaver</artifactId>
105       <version>1.8.9</version>
106     </dependency>
107 
108   </dependencies>
109 
110 
111 </project>

 

posted on 2021-11-01 16:46  晨曦生辉耀匕尖  阅读(21)  评论(0编辑  收藏  举报