天天看點

Spring核心之AOP(Aspect Oriented Programming)面向切面程式設計的三種實作方式

該文以maven工程、Spring內建Junit為開發測試環境,記錄AOP三種實作方式:

1、純配置檔案; 2、配置檔案+注解; 3、純注解。

maven工程導入相關坐标

<properties>
        <spring.version>5.0.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring內建測試-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--aop織入-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
           

1:純配置檔案

Spring核心配置檔案applicationContext.xml中配置

<!--切點類-->
    <bean id="target" class="xxx.xxx.target.impl.TargetImpl"></bean>
    <!--通知類-->
    <bean id="myAspect" class="xxx.xxx.aspect.MyAspect"></bean>
    <!--配置切面:切點+通知-->
    <aop:config>
        <!--配置通知類-->
        <aop:aspect ref="myAspect">
            <!--配置增強方法、切點:該方法為前值增強-->
            <aop:before method="before" pointcut="execution(* xxx.xxx.target.impl.*.*(..))"></aop:before>
        </aop:aspect>
    </aop:config>
           

通知/增強類

public class MyAspect {
    //增強方法
    public void before() {
        System.out.println("before running.......");
    }
}
           

目标類(需要增強的類)

public class TargetImpl implements Target {
    //切點
    public void method() {
        System.out.println(" target running.......");
    }
}

           

測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest {
    @Autowired
    private Target target;
    @Test
    public void test() {
        target.method();
    }
}

           

2:配置檔案+注解兩摻

Spring核心配置檔案applicationContext.xml配置

<!--開啟包掃描-->
    <context:component-scan base-package="xxx.xxx"></context:component-scan>
    <!--開啟aop自動代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
           

通知/增強類

@Component
@Aspect
public class MyAspect {
    //增強方法
    @Before("execution(* xxx.xxx.target.impl.*.*(..))")
    public void before() {
        System.out.println("before running.......");
    }
}

           

目标類(需要增強的類)

@Component
public class TargetImpl implements Target {
    //切點
    public void method() {
        System.out.println(" target running.......");
    }
}

           

測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest {
    @Autowired
    private Target target;
    @Test
    public void test() {
        target.method();
    }
}
           

3純注解

配置類

@Configuration
@ComponentScan("xxx.xxx")
public class SpringConfig {
}
           

通知/增強類

@Component
@Aspect
public class MyAspect {
    //增強方法
    @Before("execution(* xxx.xxx.target.impl.*.*(..))")
    public void before() {
        System.out.println("before running.......");
    }
}
           

目标類(需要增強的類)

@Component
public class TargetImpl implements Target {
    //切點
    public void method() {
        System.out.println(" target running.......");
    }
}
           

測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfig.class})
@EnableAspectJAutoProxy
public class SpringTest {
    @Autowired
    private Target target;
    @Test
    public void test() {
        target.method();
    }
}
           
如文中有不合理之處,請各位批評指正。