Spring(使用Annotation方式的AOP)

一.配置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"
        xmlns:context="http://www.springframework.org/schema/context"
         xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           ">

    <context:annotation-config/>
    <!-- 扫描com.daoan下面的所有包,包括子包 -->
    <context:component-scan base-package="com.daoan"/>
    <aop:aspectj-autoproxy/>

</beans>
  <!-- 如果这里写
<aop:aspectj-autoproxy/>
没有提示需要引入相关的xsd文件 -->    查找Spring-aop-2.5.xsd(C:\spring-framework-2.5.6-with-dependencies\spring-framework-2.5.6\dist\resources)  

key:http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

写一个逻辑来测试

需要引入相应的jar

package com.daoan.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogIntercetor {
    //(在save()方法执行之前先执行before()方法,如果需要把该逻辑织入到某个类的某个方法上,那个对象必须是spring管理起来的)
    @Before("execution(public void com.daoan.dao.impl.UserDAOImpl.save(com.daoan.model.User))")
    public void before() {
        System.out.println("method start");
    }
}

前进固然艰难,且代价惨重,而退回去舒服,却要吞咽人生致命的毒药!

遇到问题不能想着逃,如果总是逃避,那将永远都是你的问题,永远无法解答!

 

原文地址:https://www.cnblogs.com/xdalsh/p/8440430.html

时间: 2024-08-01 12:25:25

Spring(使用Annotation方式的AOP)的相关文章

使用Spring的注解方式实现AOP的细节

前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Spring的注解方式实现AOP的一些细节.本文是建立在使用Spring的注解方式实现AOP入门的案例的基础之上的. 本文是来讲解使用Spring的注解方式实现AOP的一些细节,其实说白了就是学习如何使用各种通知而已,例如前置通知.后置通知.异常通知.最终通知.环绕通知等,之前我们已经学习了前置通知,现在就来学习剩余的通知. 我们先来看后置通知,此时须将MyInterceptor类的代码修改为: /** * 切面 * @

使用Spring的注解方式实现AOP入门

首先在Eclipse中新建一个普通的Java Project,名称为springAOP.为了使用Spring的注解方式进行面向切面编程,需要在springAOP项目中加入与AOP相关的jar包,spring aop需要额外的jar包有: com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring-aop-4.2.5.RELEASE.jar sprin

Spring的注解方式实现AOP

Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,aspectjweaver.jar,cglib-nodep.jar. 然后我们写一个接口 [java] view plain copy print? package com.bird.service; public interface PersonServer { public void save(Str

Spring(使用XML方式的AOP)

方法1:<?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:context="http://www.springframework.org

使用spring的注解方式实现aop

首先是导入一些的jar包 切面的代码 @Component @Aspect public class Advice { @Before("init()") public void log(){ System.out.println("before do..."); } @Pointcut("execution(* service.*.*(..))")//方法切入点,execution为执行的意思,*代表任意返回值,然后是包名,.*意思是包下面的所

Spring(十一)使用Spring的注解方式实现面向切面技术AOP

AOP涉及到的一些概念: Aspect(切面):横切性关注点的抽象即为切面,与类相似,只是两者的关注度不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象. Joinpoint(连接点):所谓连接点指那些被拦截的点.在Spring中,这些点指的是方法,因为Spring只支持方法类型的连接点(实际上Joinpoint还可以是field或类构造器). Pointcut(切入点):所谓切入点是指我们要对那些Joinpoint进行拦截的定义. Advice(通知):所谓通知是指拦截到Joinpoin

springMVC系列之(三) spring+springMVC集成(annotation方式)

个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助.不过,如果用都不会,谈思想就变成纸上谈兵了!!!先技术,再思想.实践出真知. 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来.它是为了解决企业应用开发的复杂性而创建的.Spring使用

Spring笔记(五)--注解方式实现AOP

包:aspectjrt.jar.aspectjweaver.jar AOP:面向切面的编程 1.XML配置: 2.注解. 一.注解方式: 打开注解处理器: <aop:aspectj-autoproxy/> 接口: 1 package com.dwr.spring.proxy; 2 3 public interface UserManager { 4 public void addUser(String username,String password); 5 public void delet

一起学Spring之注解和Schema方式实现AOP

概述 在上一篇,我们了解了通过实现接口和XML配置的方式来实现AOP,在实现注解方式AOP之前,先了解一下AspectJ.AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP语法,能够在编译时实现代码的注入.Spring通过集成ApsectJ实现了以注解方式定义通知类,大大减少了配置文件的工作量.本文主要讲解通过注解方式和Schema方式实现AOP编程,仅供学习分享使用,如有不足之处,还请指正. 实现AOP的三种方式: 通过实现相应接口,并配置Xml进行实现,可以参考上一篇.