注解配置业务类

使用@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.*(..))"

最新文章

  1. [Web API] Web API 2 深入系列(1) 路由
  2. ssh连接速度慢解决
  3. java生成竖排文字图片
  4. &lt;转&gt;如何进行code review
  5. Java的多线程+Socket
  6. DCI
  7. doT.js 声明
  8. C++/C#结构体转化-传string给C++
  9. Django操作model时刻,一个错误:AttributeError:’ProgrammingError’ object has no attribute ‘__traceback__’
  10. easyUI中datagrid的使用
  11. JVM学习记录-对象已死吗
  12. Java Fork/Join 框架
  13. Mysql加锁过程详解(7)-初步理解MySQL的gap锁
  14. kmeans聚类理论篇
  15. Linux静默安装matlab
  16. Linux 常见操作
  17. u-boot移植(七)---代码修改---存储控制器
  18. 《剑指offer》-铺地砖方案数
  19. java aop的理解
  20. [编程题] N阶楼梯上楼问题

热门文章

  1. LeetCode 721. Accounts Merge
  2. webpack常见配置信息
  3. BZOJ 4212: 神牛的养成计划 可持久化trie+trie
  4. 使用css3变量创建炫酷悬停效果
  5. 转载:scala中的:+\+:\::\:::
  6. mysql 通配符%以及_
  7. numpy.linalg.svd函数
  8. vue+element 表格导出Excel文件
  9. fluent将出口温度赋值给入口
  10. java中&amp;&amp; 的运算优先级高于||