`

Spring 面向切面(XML)

 
阅读更多

Maven配置文件

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.jinglun</groupId>
  <artifactId>SpringInAction</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringInAction Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!-- Spring集成Junit-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>3.1.0</version>
        <scope>test</scope>
    </dependency>
    
    <!-- Spring Aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.11</version>
    </dependency>
    
    <!-- Spring MVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <!-- Spring Core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <!-- junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>SpringInAction</finalName>
  </build>
</project>

 

 

通知类:

Audience.java

package concert;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class Audience {
    
    public void silenceCellPhones() {
        System.out.println("Silencing cell phones");
    }
    
    public void takeSeats() {
        System.out.println("Taking seats");
    }
    
    public void applause() {
        System.out.println("CLAP CLAP CLAP!!!");
    }
    
    public void demandRefund() {
        System.out.println("Demanding a refund");
    }

    public void watchPerformance(ProceedingJoinPoint jp) {
        try {
            System.out.println("Arround Start");
            jp.proceed();
            System.out.println("Arround End");
        } catch (Throwable e) {
            System.out.println("Demanding a refund");
        }
    }
}

 

切点:

Performance.java

package concert;

public interface Performance {
    public void perform();
}

 

PerformanceImpl.java

package concert;

import org.springframework.stereotype.Component;

@Component
public class PerformanceImpl implements Performance{

    public void perform() {
        System.out.println("perform");
    }
}

 

XML配置:

spring-service.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-4.2.xsd 
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <context:component-scan base-package="concert" />

    <aop:aspectj-autoproxy />

    <bean id="audience" class="concert.Audience"/>

    <aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut 
                id="performance"
                expression="execution(* concert.Performance.perform(..))" />
            <aop:before
                pointcut-ref="performance"
                method="silenceCellPhones" />
            <aop:before
                pointcut-ref="performance"
                method="takeSeats" />
            <aop:after-returning
                pointcut-ref="performance"
                method="applause" />
            <aop:after-throwing
                pointcut-ref="performance"
                method="demandRefund" />
            <aop:around 
                pointcut-ref="performance"
                method="watchPerformance" />
        </aop:aspect>
    </aop:config>
</beans>

 

测试类:

PerformTest.java

package concert;

import static org.junit.Assert.assertNotNull;

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.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-service.xml"})
public class PerformTest {
    
    @Autowired
    private Performance perform;
    
    @Test
    public void play() {
        perform.perform();
        assertNotNull(perform);
    }
}

 

 

分享到:
评论

相关推荐

    spring使用动态代理面向切面编程(AOP) xml

    NULL 博文链接:https://cdxs2.iteye.com/blog/1942254

    Spring_aop_xml.zip

    spring-aop实践项目,基于xml方式的面向切面实践小项目

    Web框架编程-Spring框架编程

    快速熟悉相关内容,编写基于Spring框架技术的依赖注入和面向切面编程的相关代码,掌握Spring框架技术中依赖注入和面向切面编程的具体过程和基本要素,具备编写具有松耦合和面向切面特点的应用程序的能力。...

    Spring插件安装图解

    面向切面编程(AOP --- aspect oriented programming) 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 ...

    spring4.0.zip

    面向切面编程(AOP — aspect oriented programming) 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 ...

    java面试Spring.pdf

    AOP(Aspect-OrientedProgramming),面向切面编程 Spring AOP里面常用名词的概念: Spring容器的启动流程 Spring Bean的生命周期? Spring中bean的作用域 说一下Spring基于xml注入bean的几种方式? Spring如何解决循环...

    Spring Framewor开发手册

    2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的...

    spring4示例代码

    spring-3 演示使用动态代理模式实现面向切面编程 使用注解方式进行AOP编程及使用配置xml方式进行AOP编程 spring-4 演示了spring与JDBCTemplate配合使用 spring-5 演示了声明式事务及使用xml配置文件处理事务

    spring4.1核心包

    Spring面向切面编程,提供AOP实现。Spring Beans之上将横切关注点模块化 2. spring-aspects-4.1.1.RELEASE.jar 提供的对AspectJ框架的整合,也是A面向切面编程。 AspectJ可用于基于普通Java对象的模块化 注意:...

    spring3.0jar包

    ◆面向切面——Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑...

    精通spring--源代码

    包括控制反转容器,面向切面编程  对DA0层集成技术进行了详细阐述。包括DA0抽象支持。JDBC集成,事务集成,单元和集成测试。Hibernate集成,Java持久化API集成  深入讲解Java EE服务及技术集成。包括JNDI集成,EJB...

    AOP 切面编程的两种方式xml 和注解

    代理的生成,管理及其依赖关系都是由IOC容器负责,Spring默认使用JDK动态代理,在需要代理类而不是代理接口的时候,Spring会自动切换为使用CGLIB代理,不过现在的项目都是面向接口编程,所以JDK动态代理相对来说用的...

    spring2.5.chm帮助文档(中文版)

    2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving...

    25个经典的Spring面试问答

    Spring Framework简介:了解Spring框架的核心理念,包括依赖注入(DI)和面向切面编程(AOP)。 Spring Boot:熟悉Spring Boot,知道如何创建和管理Spring Boot项目,以及如何使用Spring Boot的自动配置功能。 ...

    spring-framework-3.1.0.RELEASE.zip

    面向切面——Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——...

    精通Spring(书签)

    包括控制反转容器,面向切面编程  对DA0层集成技术进行了详细阐述。包括DA0抽象支持。JDBC集成,事务集成,单元和集成测试。Hibernate集成,Java持久化API集成  深入讲解Java EE服务及技术集成。包括JNDI集成,EJB...

    Spring 中文API&开发文档.rar

    ◆面向切面——Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑...

    Spring AOP 的实现例子(基于XML配置实现)

    Spring AOP 的实现例子(基于XML配置实现); 具体介绍看这里:https://blog.csdn.net/qq_22078107/article/details/85865543

    用于将所有Spring xml配置转换为基于Spring java的配置的工具(高分毕设).zip

    它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以简化开发过程、提高代码的可维护性和可测试性。 2. Spring MVC框架:Spring MVC是基于Spring框架的...

    Spring的学习笔记

    二、 利用动态代理实现面向切面编程 20 第八课:Spring AOP配置选项 21 一、 AOP配置annotation方式 21 (一) 搭建annotation开发环境 21 (二) aspectJ类库 22 (三) AOP的annotation实例 22 (四) AspectJ的专业术语 ...

Global site tag (gtag.js) - Google Analytics