注解配置业务类

使用@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. 【NOIP2017】逛公园 D1 T3

    记忆化搜索 跑一次反向的最短路求出MinDis(u,n)MinDis(u,n)MinDis(u,n) f[u][k]f[u][k]f[u][k]表示dis(u,n)<=MinDis(u,n)+d ...

  2. codeforces B. Make Them Odd -C++stl之set的使用

    B. Make Them Odd There are nn positive integers a1,a2,…,ana1,a2,…,an. For the one move you can choos ...

  3. Omnibus 安装

    使用gem gem install omnibus 说明 可能需要配置gem source ,通过 gem source list 可以进行检查 参考如下:   gem source -r https ...

  4. Python之NumPy(axis=0/1/2...)的透彻理解

    https://blog.csdn.net/sky_kkk/article/details/79725646 numpy中axis取值的说明首先对numpy中axis取值进行说明:一维数组时axis= ...

  5. Exception in thread "main" java.lang.IllegalStateException: Failed to read 问题解决

    开发中偶尔遇到这样的问题:Exception in thread "main" java.lang.IllegalStateException: Failed to read .. ...

  6. python下浏览器静默运行驱动

    此处以chromdriver为例,放置driver路径问题参看上一篇问题.和java处理差不多,python实现静默运行方式如下 首先解答为什么进行静默运行? 我们在本地一般便于调试可以用GUI界面运 ...

  7. dedecms 模板文件不存在,无法解析文档的终极各种解决办法

    dedecms 模板文件不存在,无法解析文档"的终极各种解决办法 方法一:[此对应喜欢把模板文件使用".html"的格式,] /include/arc.archives. ...

  8. Django入门——《Python编程从入门到实践》

    Django是一个Web框架--一套用于帮助开发交互式网站的工具.Django能够响应网页请求,还能让你更轻松地读写数据库.管理用户等. 1.建立项目 开始编写一个名为"学习笔记" ...

  9. Tomcat的安装以及环境变量的配置

    目录 下载 解压并配置环境变量 测试 关闭服务 Tomcat启动时,控制台和IDEA控制台中文乱码解决方案 下载 官方网址:Apache Tomcat® https://tomcat.apache.o ...

  10. 详解python3如何调用c语言代码

    本文链接:https://blog.csdn.net/u012247418/article/details/80170690开发环境linux: python3.5.2 + ubuntu-gnome- ...