`

Spring AOP 使用

阅读更多
applicationContext.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:p="http://www.springframework.org/schema/p"
	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:component-scan base-package="com.mhm"/>
	<!-- aop使用注解时,必须配 -->
	<aop:aspectj-autoproxy/>

</beans>


MyAOP:
package com.mhm.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAOP {
	
	@SuppressWarnings("unused")
	@Pointcut("execution(* com.mhm.mng.impl..*.*(..))")//AOP表达式语言
//执行业务方法时拦截,*表示返回类型,
//..表示对impl下的子包中的类也拦截
//*表示所有类
//*表示所有方法
//(..)表示方法的参数为随意
	private void anyMethod() {}//声明一个切入点	
	
	@Before("anyMethod()  && args(name)")//定义前置通知
//args(name)表示拦截的方法必须有一个参数,配合下面的,表示参数为String类型
	public void doBefore(String name) {
		System.out.println("前置通知:" + name);
	}
	
	@AfterReturning(pointcut="anyMethod()", returning="revalue")//定义后置通知
	public void doAfterReturning(String revalue) {
		System.out.println("后置通知");
	}
	
	@After("anyMethod()")//定义最终通知
	public void doAfter(){
		System.out.println("最终通知");
	}
	
	@AfterThrowing(pointcut="anyMethod()", throwing="ex")//定义异常通知
	public void doAfterThrowing(Exception ex){
		System.out.println("异常通知: " + ex);
	}
	
	@Around("anyMethod()")//环绕通知
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
//环绕通知最适合做权限验证
		//次return必须写
		System.out.println("进环绕通知");
		Object obj = pjp.proceed();
		System.out.println("出环绕通知"); //出通知为最后输出,最终通知在出环绕通知的前面
		return obj;
		
	}

}


PersonDao:
package com.mhm.dao;

public interface PersonDao {

	/**
	 * 得到名字
	 * @param name
	 */
	public String getName(int id);

	/**
	 * 保存
	 */
	public void save(String name);

	/**
	 * 更新
	 */
	public void update(int id);

}


PersonDaoImpl:
package com.mhm.dao.impl;

import org.springframework.stereotype.Repository;

import com.mhm.dao.PersonDao;

@Repository
public class PersonDaoImpl implements PersonDao {
	public String getName(int id){
		System.out.println("我是dao的getName:" + id);
		return "getNameById: " + id;
	}
	public void save(String name){
		throw new RuntimeException("this is a exception");
		//System.out.println("我是dao的save(),保存姓名:" + name);
	}
	
	
	public void update(int id){
		System.out.println("我是dao的update()");
	}
}


PersonMng:
package com.mhm.mng.impl;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mhm.dao.PersonDao;
import com.mhm.mng.PersonMng;

@Service
public class PersonMngImpl implements PersonMng {
	
	@Autowired
	private PersonDao personDao;
	
	@SuppressWarnings("unused")
	@PostConstruct
	private void init(){
		System.out.println("我是PersonMngImpl中的init()");
	}
	
	@SuppressWarnings("unused")
	@PreDestroy//要使用此注解,在客户端必须使用AbstractApplicationContext
	private void destroy(){
		System.out.println("我是PersonMngImpl中的destroy()");
	}
	
	public String getName(int id){
		//System.out.println("我是mng的getName:" + id);
		return personDao.getName(id);
	}
	
	public void save(String name){
		//System.out.println("我是save()");
		personDao.save(name);
	}
	
	public void update(int id){
		personDao.update(id);
	}
}



SpringTest:
package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mhm.mng.PersonMng;

public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}
	
	@Test
	public void springtest(){
//		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//		PersonMng personMng = (PersonMng)ctx.getBean("personMngImpl");
//		personMng.save();
		AbstractApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");
		PersonMng personMng = (PersonMng)actx.getBean("personMngImpl");
		personMng.save("mahongmin");
		actx.close();
	}

}
分享到:
评论

相关推荐

    Spring Aop使用实例

    **Spring AOP 使用实例** Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的一个重要组成部分,它提供了一种在不修改原有代码的情况下,通过代理方式添加额外功能的技术。这种技术使得我们...

    spring aop使用教程

    Spring AOP 使用教程 Spring AOP(Aspect-Oriented Programming)是一种编程技术,能够帮助开发者在软件系统中实现一些通用的功能,如日志记录、安全检查、事务管理等。下面是关于 Spring AOP 使用教程的详细知识...

    JavaEE Spring AOP使用

    JavaEE Spring AOP(面向切面编程)是企业级应用开发中的重要技术,它允许...无论是通过注解还是XML配置,掌握Spring AOP的使用都能显著提升我们的开发效率。在实践中,可以根据项目的具体需求选择最适合的实现方式。

    使用SpringAop使用Oracle数据权限控制

    接下来,我们使用Spring AOP来实现数据过滤。这通常涉及以下步骤: 1. **定义切点(Pointcut)**:确定哪些方法需要进行权限检查。这可以通过注解或XML配置来实现,例如定义一个名为`@DataAccess`的注解,并将其...

    spring之AOP(动态代理)

    在Spring Boot项目中,配置和使用AOP相对简单。首先,我们需要引入Spring AOP和AspectJ的依赖,这通常在pom.xml或build.gradle文件中完成。然后,我们可以定义一个切面(Aspect),它包含通知(Advice)——即在特定...

    spring AOP 引入jar包,spring IOC 引入Jar包

    在提供的 `lib` 压缩包中,应包含 Spring AOP 和 Spring IOC 相关的 jar 包,如 `spring-aop-x.x.x.jar` 和 `spring-context-x.x.x.jar` 等,它们是使用这两个功能的基础。请确保引入正确的版本,并正确配置到项目的...

    spring aop jar 包

    在项目中,我们通常会将这个jar包引入到类路径下,以便使用Spring AOP的功能。 总的来说,Spring AOP通过提供面向切面的编程能力,极大地提高了代码的可复用性和可维护性,降低了系统复杂度,特别是在处理共性问题...

    spring源码--AOP流程--笔记.docx

    在这个过程中,Spring AOP 使用 configBeanDefinitionParser 来解析 AOP 配置文件,并生成相应的 Bean 定义对象。 3. 注册 AutoProxyCreator:在解析 AOP 配置文件时,Spring AOP 也会注册一个 ...

    springAop默认代理方式.zip

    4. **代理模式的创建**:Spring AOP 使用`org.springframework.aop.framework.ProxyFactoryBean`或`@EnableAspectJAutoProxy`注解来配置代理。`ProxyFactoryBean`是XML配置方式,而`@EnableAspectJAutoProxy`是基于...

    spring aop jar

    Spring AOP使用一个名为`@Pointcut`的注解来声明切入点,并通过`@Before`, `@After`, `@AfterReturning`, `@AfterThrowing` 和 `@Around` 注解与通知关联。 3. **织入(Weaving)**:织入是将切面应用到目标对象以...

    Spring AOP 16道面试题及答案.docx

    Spring AOP,全称为Aspect Oriented Programming,是面向切面编程的一种编程范式,它是对传统的面向对象编程(OOP)的一种补充。在OOP中,核心是对象,而在AOP中,核心则是切面。切面是关注点的模块化,即程序中的...

    简单spring aop 例子

    Spring AOP(面向切面编程)是Spring框架的重要组成部分,它提供了一种模块化和声明式的方式来处理系统中的交叉关注点问题,如日志、事务管理、安全性等。本示例将简要介绍如何在Spring应用中实现AOP,通过实际的...

    Spring AOP 使用笔记

    本笔记主要探讨了如何在Spring应用中使用AOP来实现横切关注点,如日志、事务管理、性能监控等。 首先,理解AOP的基本概念至关重要。AOP的核心是切面(Aspect),它封装了跨越多个对象的行为或责任。切面由两个主要...

    spring aop 自定义注解保存操作日志到mysql数据库 源码

    一、适合人群 1、具备一定Java编程基础,初级开发者 2、对springboot,mybatis,mysql有基本认识 3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 ...4、spring boot,mybatis,druid,spring aop的使用

    Spring AOP完整例子

    在本教程中,我们将深入探讨Spring AOP的不同使用方法,包括定义切点、通知类型、组装切面以及使用JUnit4进行测试。 首先,我们需要理解Spring AOP的基础概念。AOP的核心是切点(Pointcut),它定义了关注点在何处...

    Java spring AOP源码

    Spring AOP使用两种主要的代理技术:JDK动态代理和CGLIB动态代理。JDK动态代理适用于接口的场景,而CGLIB则用于没有实现接口的情况。在这两种情况下,代理对象都会持有目标对象的引用,并在其上调用方法。具体来说:...

    jar包---Spring AOP 还需.rar

    3. **切入点表达式**:Spring AOP使用一种基于正则表达式的语言来定义切入点,用于匹配连接点。例如,`execution(* com.example.service.*.*(..))`匹配com.example.service包下的所有类的所有方法。 4. **Spring ...

    spring aop依赖jar包

    现在,我们回到主题——"springaop依赖的jar包"。在Spring 2.5.6版本中,使用Spring AOP通常需要以下核心jar包: - `spring-aop.jar`:这是Spring AOP的核心库,包含了AOP相关的类和接口。 - `spring-beans.jar`:...

Global site tag (gtag.js) - Google Analytics