原创转载请注明出处:http://agilestyle.iteye.com/admin/blogs/2325718
Project Directory
Maven Dependency
<?xml version="1.0" encoding="UTF-8"?> <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.fool.springaop</groupId> <artifactId>springaop</artifactId> <name>springaop</name> <packaging>war</packaging> <version>1.0.0-BUILD-SNAPSHOT</version> <properties> <org.springframework-version>4.2.8.RELEASE</org.springframework-version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!-- @Inject --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.10</version> <configuration> <additionalProjectnatures> <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> </additionalProjectnatures> <additionalBuildcommands> <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> </additionalBuildcommands> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <httpConnector> <port>8888</port> </httpConnector> <webApp> <contextPath>/springaop</contextPath> </webApp> </configuration> </plugin> </plugins> </build> </project>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
root-context.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> </beans>
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables Spring AOP --> <aop:aspectj-autoproxy /> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="org.fool.springaop" /> </beans:beans>
IUserService.java
package org.fool.springaop.service; public interface IUserService { void addUser(); String addUserReturnValue(); void addUserThrowException() throws Exception; void addUserAround(String name); }
UserService.java
package org.fool.springaop.service.impl; import org.fool.springaop.service.IUserService; import org.springframework.stereotype.Service; @Service public class UserService implements IUserService { @Override public void addUser() { System.out.println("addUser() invoked..."); } @Override public String addUserReturnValue() { System.out.println("addUserReturnValue() invoked..."); return "200"; } @Override public void addUserThrowException() throws Exception { System.out.println("addUserThrowException() invoked..."); throw new Exception(); } @Override public void addUserAround(String name) { System.out.println("addUserAround() invoked..., args: " + name); } }
LoggingAspect.java
package org.fool.springaop.aspect; import java.util.Arrays; import java.util.Date; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; 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.springframework.stereotype.Component; @Component @Aspect public class LoggingAspect { @Before("execution(* org.fool.springaop.service.IUserService.addUser(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("@Before invoked!!!"); System.out.println("@Before: " + joinPoint.getSignature().getName()); System.out.println("******"); } @After("execution(* org.fool.springaop.service.IUserService.addUser(..))") public void logAfter(JoinPoint joinPoint) { System.out.println("@After invoked!!!"); System.out.println("@After: " + joinPoint.getSignature().getName()); System.out.println("******"); } @AfterReturning(value="execution(* org.fool.springaop.service.IUserService.addUserReturnValue(..))", returning="result") public void logAfterReturning(JoinPoint joinPoint, Object result) { System.out.println("@AfterReturning invoked!!!"); System.out.println("@AfterReturning: " + joinPoint.getSignature().getName()); System.out.println("@AfterRetruning method returned value is: " + result); System.out.println("******"); } /* * 1.环绕方法通知,环绕方法通知要注意必须给出调用之后的返回值,否则被代理的方法会停止调用并返回null,除非你真的打算这么做。 * 2.只有环绕通知才可以使用JoinPoint的子类ProceedingJoinPoint,各连接点类型可以调用代理的方法,并获取、改变返回值。 */ @Around("execution(* org.fool.springaop.service.IUserService.addUserAround(..))") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("@Around start: " + new Date()); System.out.println("@Around: " + joinPoint.getSignature().getName()); System.out.println("@Around argument: " + Arrays.toString(joinPoint.getArgs())); Object obj = joinPoint.proceed(joinPoint.getArgs()); System.out.println("@Around end: " + new Date()); System.out.println("******"); return obj; } @AfterThrowing(value="execution(* org.fool.springaop.service.IUserService.addUserThrowException(..))", throwing="ex") public void logAfterThrowing(JoinPoint joinPoint, Throwable ex) { System.out.println("@AfterThrowing invoked!!!"); System.out.println("@AfterThrowing: " + joinPoint.getSignature().getName()); System.out.println("@AfterThrowing exception: " + ex); System.out.println("******"); } }
AopTest.java
package org.fool.springaop.test; import org.fool.springaop.service.IUserService; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class AopTest { private AbstractApplicationContext ctx; private IUserService userService; @Before public void setUp() throws Exception { ctx = new FileSystemXmlApplicationContext("/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"); userService = ctx.getBean(IUserService.class); } @After public void tearDown() throws Exception { ctx.close(); } @Test public void testBeforeAndAfter() throws Exception { userService.addUser(); } @Test public void testAfterReturning() throws Exception { userService.addUserReturnValue(); } @Test public void testAround() throws Exception { userService.addUserAround("Around Advice"); } @Test public void testAfterThrowing() throws Exception { userService.addUserThrowException(); } }
Test
testBeforeAndAfter
testAfterReturning
testAround
testAfterThrowing
相关推荐
本实例将详细探讨如何通过注解(Annotation)来实现Spring AOP的方法拦截。 一、Spring AOP基础 Spring AOP是Spring框架的一部分,它提供了一种在运行时织入横切关注点(如日志、事务管理等)到目标对象的能力。AOP...
在本资料"Spring_Annotation_AOP"中,我们将深入探讨Spring框架如何利用注解实现AOP,以及其背后的原理和实践应用。 面向切面编程(AOP)是一种编程范式,旨在提高代码的可维护性和可重用性,通过将关注点分离,...
本示例将简要介绍如何在Spring应用中实现AOP,通过实际的代码示例帮助理解其工作原理。 首先,我们要理解AOP的核心概念。AOP是一种编程范式,它允许开发者定义“切面”(Aspects),这些切面封装了特定的关注点,如...
Spring AOP是在Spring框架的基础上实现的一种面向方面编程机制。 1. **方面(Aspect)**:这是AOP的核心概念之一,指代一个关注点的模块化,该关注点可能会横切多个对象。例如事务管理就是一个典型的横切关注点,...
2. **Spring AOP实现方式** - **代理模式**:Spring AOP使用两种代理方式,JDK动态代理和CGLIB代理。如果目标类实现了接口,Spring会使用JDK动态代理;如果没有实现接口,Spring会使用CGLIB代理生成子类。 - **JDK...
对于源码学习,可以查看Spring AOP的实现,尤其是`org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator`和`org.springframework.aop.aspectj.AspectJExpressionPointcut`这两个类,...
Spring mvc Aop+annotation实现系统日志记录功能实现的jar包asm-3.3.jar ,aspectjrt.jar , aspectjweaver.jar , cglib-nodep-2.1_3.jar , spring-aop.jar
NULL 博文链接:https://tianhei.iteye.com/blog/978969
在`springAop1`这个压缩包中,可能包含了一个简单的应用示例,展示了如何定义一个切面类,以及如何在该类中定义通知方法。例如,我们可能会看到一个名为`LoggingAspect`的类,其中包含了`@Before`注解的方法,用于在...
总结一下,通过上述步骤,我们已经在Spring Boot应用中利用Spring AOP和注解方式实现了数据脱敏。这个拦截器可以在不修改原有业务代码的情况下,确保敏感信息在响应给客户端之前得到处理,提高了应用的安全性。同时...
总结,这个"SpringAOP简单项目实现"涵盖了Spring AOP的基础知识,包括切面、通知、切入点的定义与配置,以及如何在实际项目中使用Maven进行构建和依赖管理。对于初学者来说,这是一个很好的实践案例,能够帮助他们...
SpringBoot结合AspectJ实现SpringAOP拦截指定方法的知识点涵盖了多个方面,这包括Spring AOP的基本概念、SpringBoot的应用、切点(Pointcut)与通知(Advice)的定义、自定义注解以及AspectJ的使用。以下是这些知识...
**Spring AOP实现方式** 1. **注解驱动(Annotation-based)**:使用`@Aspect`注解定义切面,`@Before`, `@After`, `@AfterReturning`, `@AfterThrowing`, `@Around`定义通知,`@Pointcut`定义切入点表达式。 2. *...
Spring AOP,全称为Aspect Oriented Programming,是Spring框架中的一个重要模块,主要负责处理系统中的...文件"5.SpringAOP_01"和"6.SpringAOP_02"很可能是课程的分阶段内容,涵盖了从基础概念到进阶实践的详细讲解。
本文将深入探讨如何结合Spring AOP与EhCache实现一个简单的缓存实例,以便优化Java应用的运行效率。 首先,让我们了解Spring AOP。Spring AOP是Spring框架的一部分,它允许我们在不修改业务代码的情况下,通过定义...
在本示例中,我们将深入探讨如何利用Spring AOP实现简单的权限验证。 首先,AOP的核心概念是切面(Aspect),它封装了横切关注点,比如日志、事务管理、权限验证等。在Spring AOP中,切面通过通知(Advice)来定义...
在提供的压缩包文件"springAOP"中,可能包含了以下内容: - **切面类(Aspect Class)**:包含切点和通知的Java类,可能使用了`@Aspect`注解。 - **目标类(Target Class)**:被AOP代理的对象,通常包含业务逻辑。...
Spring AOP主要通过两种方式实现:XML配置和注解。本实例主要探讨的是使用XML配置的方式来实现AOP。XML配置虽然相比注解方式略显繁琐,但它提供了更大的灵活性,尤其是在需要对多个类或方法应用相同通知(Advice)时...
除了上述基本注解,Spring还提供了`@Within`、`@Args`、`@Target`、`@Annotation`等更多复杂的切点表达式,可以根据实际需求进行组合,实现更精细化的切面逻辑。 总结起来,Spring AOP注解版通过简单易懂的注解,...
Spring AOP通过两种主要实现方式提供切面功能:代理模式(Proxy)和基于注解的切面(Annotation-based AOP)。代理模式下,Spring创建一个目标对象的代理,当调用目标方法时,代理会在前后添加额外的行为。而注解...