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.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class Audience { @Pointcut("execution(** concert.Performance.perform(..))") public void performance() {} @Before("performance()") public void silenceCellPhones() { System.out.println("Silencing cell phones"); } @Before("performance()") public void takeSeats() { System.out.println("Taking seats"); } @AfterReturning("performance()") public void applause() { System.out.println("CLAP CLAP CLAP!!!"); } @AfterThrowing("performance()") public void demandRefund() { System.out.println("Demanding a refund"); } @Around("performance()") public void watchPerformance(ProceedingJoinPoint jp) { try { System.out.println("Silencing cell phones"); System.out.println("Taking seats"); jp.proceed(); System.out.println("CLAP CLAP CLAP!!!"); } catch (Throwable e) { System.out.println("Demanding a refund"); } } }
配置文件
ConcertConfig.java
package concert; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration @EnableAspectJAutoProxy @ComponentScan public class ConcertConfig { @Bean public Audience audience() { return new Audience(); } @Bean public Performance perform() { return new PerformanceImpl(); } }
切点
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"); } }
测试类
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; import concert.ConcertConfig; import concert.Performance; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=ConcertConfig.class) public class PerformTest { @Autowired private Performance perform; @Test public void play() { perform.perform(); assertNotNull(perform); } }
相关推荐
Spring-aop面向切面编程实例 日志系统,权限控制等.
写了SpringAop的两种代理的实现,jdk动态代理和cglib代理的代码,及测试。
Spring-AOP java2ee中的Spring框架的面向切面示例 简单明了 一学就会
Spring4AOP 面向切面编程实例之方法拦截实例 一下利用Spring4的最后一个版本Spring4.3.9,实现简单的方法拦截实例。 Eclipse 建立java工程,导入必要的jar包,工程目录如下:
NULL 博文链接:https://zyssnh.iteye.com/blog/1866867
学习Spring开发的AOP面向切面编程时所需要的jar包,包括com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
NULL 博文链接:https://cdxs2.iteye.com/blog/1942254
编程语言+JAVAspring+AOP编程+面向切面**:这是一个关于JAVAspring编程语言的AOP编程的面向切面的资源,适合有一定JAVAspring基础的开发者。它介绍了JAVAspring的AOP编程的概念、原理和作用,以及如何使用JAVAspring...
2、面向切面编程AOP 在实现过程中,我尽量贴近实际开发与场景,麻雀虽小,五脏俱全,个人感觉这个项目是web框架搭建的一个极简之道实践。 项目中可能还存在很多的问题,希望大家不吝赐教,谢谢。
spring-aop面向切面系统日志案例
此文档主要是讲述Spring中很关键的一个知识点,面向切面编程。
在使用spring的aop功能时,这两个jar是必须的,否则会报错,如下: Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException at java.net....
该压缩包包含--学习笔记(05):轻松搞定Spring全家桶(初识篇)-面向切片编程AOP:--系列博客中新需要的三个jar包。包括spring-aspects-5.2.7.RELEASE.jar和spring-aop-5.2.7.RELEASE.jar和...
AOP 的全称是“Aspect Oriented Programming”,即面向切面编程,它将业务逻辑的各个部分进行隔离,使开发人员在编写业务逻辑时可以专心于核心业务,从而提高了开发效率。AOP 采取横向抽取机制,取代了传统纵向继承...
Spring框架资料,Ioc容器, AOP面向切面编程 ,声明式事务 ,Spring5新特性
aspectjrt-1.5.2.jar aspectjweaver-1.9.1.jar spring-aop-5.0.8.RELEASE.jar spring-aspects-5.0.8.RELEASE.jar
Spring面向切面编程(AOP) 16 Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架。Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用。Spring 框架目标是简化Java企业级...
本项目是提供给java新手...本项目演示了java的 AOP 面向切面 的开发。 本项目演示了 前置切面,环绕切面, 后置异常切面 的开发。 本项目的技术栈是 Maven+Spring 如果您是技术高手,请不要购买,这个资源不适合您。
AOP(Aspect Oriented Programming):面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。 AOP是对OOP的补充,是软件开发中的一个热点,也是Spring框架中的一个重要内容。 利用AOP...
Spring框架是一个开放源代码的J2EE应用程序框架,是对bean的生命周期进行管理的轻量...Spring框架主要由七部分组成,分别是Spring Core,Spring AOP,Spring ORM,Spring DAO,Spring Context,Spring Web和Spring Web MVC。