注解配置业务类

使用@Component("s") 注解ProductService 类

package com.how2java.service;

import org.springframework.stereotype.Component;

@Component("s")
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
} }

注解配置切面

@Aspect 注解表示这是一个切面
@Component 表示这是一个bean,由Spring进行管理
@Around(value = "execution(* com.how2java.service.ProductService.*(..))") 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作

package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LoggerAspect { @Around(value = "execution(* com.how2java.service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}

springconfig.xml

去掉原有信息,添加如下3行

<context:component-scan base-package="com.how2java.aspect"/>
<context:component-scan base-package="com.how2java.service"/>

扫描包com.how2java.aspect和com.how2java.service,定位业务类和切面类

<aop:aspectj-autoproxy/>

找到被注解了的切面类,进行切面配置

<?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.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"> <context:component-scan base-package="com.how2java.aspect"/>
<context:component-scan base-package="com.how2java.service"/>
<aop:aspectj-autoproxy/> </beans>

测试运行

package com.how2java.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.how2java.service.ProductService; public class TestSpring { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "springconfig.xml" });
ProductService s = (ProductService) context.getBean("s");
s.doSomeService();
}
}

---------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------

在how2j 网站学习spring框架时,发现有网友留了一些问题,这里我们拿出来分享一下

1. 为什么用注解方式跑程序比用xml配置要慢?

xml文件的形式,系统只需要解析xml文件中的内容,你需要自己去管理xml文件。而注解最终要调用反射,反射很耗时间的,换来的是不需要去管理xml文件,二者各有优缺点,看情况选择合适的方式。

2. 假如一个业务有两个切面如何区分先后顺序?(比较重要

使用xml配置的时候,在定义切面时,如果通知的类型相同,可以加上order定义多个切面的执行顺序,这里有两种情况:

<aop:aspect id="logAspect2" ref="loggerAspect2" order="1">
<aop:before pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect> <aop:aspect id="logAspect3" ref="loggerAspect3" order="2">
<aop:before pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>

上面的通知类型都是before,order的数值越小越先执行!!!

<aop:aspect id="logAspect2" ref="loggerAspect2" order="1">
<aop:after pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect> <aop:aspect id="logAspect3" ref="loggerAspect3" order="2">
<aop:after pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>

上面的通知类型都是after,order的数值越大越先执行!!!

在定义切面时,如果通知的类型不同,则无论怎样定义order都无法决定多个切面的执行顺序

<!--该切面order="1",但通知是after类型-->
<aop:aspect id="logAspect2" ref="loggerAspect2" order="1">
<aop:after pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
<!--该切面order="2",但通知是before类型-->
<aop:aspect id="logAspect3" ref="loggerAspect3" order="2">
   <aop:before pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>

这种情况切面的执行顺序是按通知的类型决定的!!!

用注解的方式定义切面,执行的先后顺序其原理和xml配置一样。

3. 如何把一个切面织入两个不同的业务类中?

可在切面中加入

@Around(value = "execution(* com.java.ProductService.*(..)) || execution(* com.java.service.ProductService2.*(..))")

如果分开写,会报错,例如

@Around(value = "execution(* com.java.ProductService.*(..))"
@Around(value = "execution(* com.java.ProductService2.*(..))"

spring框架学习(四)——注解方式AOP的更多相关文章

  1. spring框架学习(二)——注解方式IOC/DI

    什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...

  2. spring框架学习(六)AOP

    AOP(Aspect-OrientedProgramming)面向方面编程,与OOP完全不同,使用AOP编程系统被分为方面或关注点,而不是OOP中的对象. AOP的引入 在OOP面向对象的使用中,无可 ...

  3. Spring框架学习之注解配置与AOP思想

         上篇我们介绍了Spring中有关高级依赖关系配置的内容,也可以调用任意方法的返回值作为属性注入的值,它解决了Spring配置文件的动态性不足的缺点.而本篇,我们将介绍Spring的又一大核心 ...

  4. spring框架学习(六)AOP事务及spring管理事务方式之Template模板

    概念 1.事务 1)事务特性:ACID 原子性 :强调事务的不可分割. 一致性 :事务的执行的前后数据的完整性保持一致. 隔离性 :一个事务执行的过程中,不应该受到其他事务的干扰. 持久性 :事务一旦 ...

  5. Spring框架学习(9)AOP技术理解与使用

    内容源自:AOP技术理解与使用 一.什么是AOP? aop技术是面向切面编程思想,作为OOP(面向对象编程)的延续思想添加到企业开发中,用于弥补OOP开发过程中的缺陷而提出的编程思想. AOP底层也是 ...

  6. 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置

    在前文 深入浅出学习Spring框架(一):通过Demo阐述IoC和DI的优势所在. 深入浅出学习Spring框架(三):AOP 详解 分别介绍了Spring的核心功能——IoC和AOP,光讲知识远远 ...

  7. [ SSH框架 ] Spring框架学习之三(AOP开发和注解的使用)

    一.Spring 使用 AspectJ 进行 AOP 的开发:注解的方式 1.1 引入相关的jar包 1.2 引入spring的配置文件 <?xml version="1.0" ...

  8. Spring框架学习总结(上)

    目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...

  9. Spring框架学习笔记(1)

    Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...

随机推荐

  1. 洛谷 P3958 奶酪 题解

    思路: 先看哪两个点能互通,再广搜寻找下一步,如果到达高度h就输出Yes,如果所有路径都找过都不能到达高度h就输出No. #include<bits/stdc++.h> using nam ...

  2. js之大文件分段上传、断点续传

    文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...

  3. SVN 常用 还原项目

    1.先修改本来两个文件,然后再提交到SVN 2.在日志界面,查看提交的文件,找到对应的版本号 3.找到对应的版本号(这里的版本号是1995,我提交生成的版本号 的前一个版本 才是我未作出修改的版本), ...

  4. js无限轮播算法中干掉if判断

    无限轮播在网页应用中经常见到,这其中算法各有千秋,在学习算法分析一书中发现自增取余方法可以干掉一些不必要的if判断,具体代码如下: var arr= [1,2,3,4,5,6,7,8]; var in ...

  5. CSP2019自闭记

    为什么我之前没有写呢,是因为我总是考的太lj,于是就不想写了. 这次不管考没考好都要强迫自己写,因为这是第一次参加提高组+第一次参加CSP. 当然什么初赛/复赛试题/答案什么的是不会出现的. Day ...

  6. js中forEach,for in,for of循环的用法详解

    一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...

  7. mysql tan() 函数

    mysql> ); +--------------------+ | tan(pi()/) | +--------------------+ | 0.9999999999999999 | +-- ...

  8. 2019 SDN第五次上机作业

    2019 SDN第五次上机作业 作业链接 1.浏览RYU官网学习RYU控制器的安装和RYU开发入门教程,提交对于教程代 码的理解,包括但不限于: 安装RYU控制器并测试 安装教程 安装过程及遇到各种问 ...

  9. #C++初学记录ACM补题(D. Candies!)前缀和运算。

    D - Candies!   Consider a sequence of digits of length [a1,a2,-,a]. We perform the following operati ...

  10. 004 vue组件

    一:创建组件 1.第一种创建方式 主要有Vue.extend,Vue.component. 注释掉的代码是一步一步的推断,后面的代码是简化的代码. <!DOCTYPE html> < ...