2020-02-09
描述
spring aop练习
今日之技术博客
Spring
Spring是高度模块化的,可以单独使用Spring的某个库而不需要依赖Spring的其他库。比如,使用Spring Context库不需要依赖Spring Persistence或者Spring MVC库。
spring Maven依赖
依赖 | 介绍 | 依赖于 |
---|---|---|
spring-core | 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 | commons-collections.jar |
spring-beans | 这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,引入spring-core.jar及spring- beans.jar文件就可以了。 | spring-core.jar,cglib-nodep-2.1_3.jar |
spring-aop | 这个jar文件包含在应用中使用Spring的 AOP特性时所需的类。使用基于AOP的Spring特性,如声明型事务管理(Declarative Transaction Management),也要在应用里包含这个jar包。 | spring-core.jar,spring-beans.jar,cglib-nodep-2.1_3.jar,aopalliance.jar |
spring-context | 这个jar文件为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类,以及校验Validation方面的相关类。 | spring-core.jar,spring-beans.jar,spring-aop.jar,commons-collections.jar,aopalliance.jar |
spring-dao | 这个jar文件包含Spring DAO、Spring Transaction进行数据访问的所有类。为了使用声明型事务支持,还需在自己的应用里包含spring-aop.jar。 | spring-core.jar,spring-beans.jar,spring-aop.jar,spring-context.jar |
spring-hibernate | 由名字就可以知道它的用途,这个jar文件包含Spring对Hibernate 2及Hibernate 3进行封装的所有类。 | spring-core.jar,spring-beans.jar,spring-aop.jar,spring- dao.jar,spring-jdbc.jar,spring-orm.jar,spring-web.jar,spring-webmvc.jar |
spring-jdbc | 这个jar文件包含对Spring对 JDBC数据访问进行封装的所有类。 | spring-core.jar,spring-beans.jar,spring-dao.jar |
spring-orm | 这个jar文件包含Spring对 DAO特性集进行了扩展,使其支持 iBATIS、JDO、OJB、TopLink,因为Hibernate已经独立成包了,现在不包含在这个包里了。这个jar文件里大部分的类都要依赖 spring-dao.jar里的类,用这个包时你需要同时包含spring-dao.jar包。 | spring-core.jar,spring-beans.jar,spring-aop.jar,spring- dao.jar,spring-jdbc.jar,spring-web.jar,spring-webmvc.jar |
spring-remoting | 这个jar文件包含支持EJB、JMS、远程调用Remoting(RMI、 Hessian、Burlap、Http Invoker、JAX-RPC)方面的类。 | spring-core.jar,spring-beans.jar,spring-aop.jar,spring- dao.jar,spring-context.jar,spring-web.jar,spring-webmvc.jar |
spring-support | 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 | ing-core.jar,spring-beans.jar,spring-aop.jar,spring-dao.jar,spring-context.jar,spring-jdbc.jar |
spring-web | 这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 | spring-core.jar,spring-beans.jar,spring-context.jar |
spring-webmvc | 这个jar文件包含Spring MVC框架相关的所有类。包含国际化、标签、Theme、视图展现的FreeMarker、JasperReports、Tiles、Velocity、 XSLT相关类。当然,如果你的应用使用了独立的MVC框架,则无需这个JAR文件里的任何类。 | spring-core.jar,spring-beans.jar,spring-context.jar,spring-web.jar |
spring-mock | 这个jar文件包含Spring一整套mock类来辅助应用的测试。Spring测试套件使用了其中大量mock类,这样测试就更加简单。模拟HttpServletRequest和HttpServletResponse类在Web应用单元测试是很方便的。 | spring-core.jar,spring-beans.jar,spring-dao.jar,spring-context.jar,spring-jdbc.jar |
Spring test | 测试 |
AOP 练习
引入依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.2.3.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
方式一
配置文件
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注册bean--> <bean id="service" class="chang.demo1.ServiceImpl" /> <bean id="log" class="chang.log1.Log"/> <!-- 实现了MethodBeforeAdvice接口 --> <bean id="afterLog" class="chang.log1.AfterLog"/> <!--实现了AfterReturningAdvice接口--> <!--配置AOP --> <aop:config> <aop:pointcut id="pointcut" expression="execution(* chang.demo1.ServiceImpl.*(..))"/> <aop:advisor advice-ref="log" pointcut-ref="pointcut" /> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut" /> </aop:config> </beans>
方式二
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--注册bean--> <bean id="service" class="chang.demo2.ServiceImpl" /> <!-- 自定义的类 --> <bean id="diy" class="chang.log2.MyLog"/> <aop:config> <!--自定义切面--> <aop:aspect ref="diy"> <!-- 切入点 --> <aop:pointcut id="point" expression="execution(* chang.demo2.ServiceImpl.*(..))"/> <!-- 通知 --> <aop:before method="befor" pointcut-ref="point"/> <aop:after method="after" pointcut-ref="point"/> </aop:aspect> </aop:config> </beans>
方式三(注解)
配置文件
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="chang"/> <aop:aspectj-autoproxy/> </beans>
aop类
import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class Diy { @Before("execution(* chang.demo3.ServiceImpl.*(..))") public void befor(){ System.out.println("执行方法前"); } @After("execution(* chang.demo3.ServiceImpl.*(..))") public void after(){ System.out.println("执行方法后"); } }
来源:https://www.cnblogs.com/chang1024/p/12288538.html