`
zhuchengzzcc
  • 浏览: 441805 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring aspectj小试2

阅读更多
/**
 * @author zhuc
 * @version 2012-8-21 下午1:38:39
 */
@Aspect
@Component
public class Aspect2 {

	/**
	 * @param joinPoint
	 * @param log
	 */
	@After(value = "@annotation(log)")
	public void doAfter(JoinPoint joinPoint, Log log) {
		for (Object obj : joinPoint.getArgs()) {
			System.out.println("参数: " + obj);
		}
		System.out.println(log.function() + "," + log.desc());
		System.out.println("doAfter......");
	}
}

 

/**
 * @author zhuc
 * @version 2012-8-21 下午1:42:07
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
	String function() default "";
	String desc() default "";
}

 

/**
 * @author zhuc
 * 
 */
public class Service {

	/**
	 * @param name
	 * @return
	 */
	@Log(function = "日志管理", desc = "添加Log")
	public String addLog(String name) {
		System.out.println(name);
		return "hello:" + name;
	}
}

 

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
	<context:component-scan base-package="org.zhuc.maven.aspectj" />
	<aop:aspectj-autoproxy />
	
	<bean id="service" class="org.zhuc.maven.aspectj.Service" />

</beans>

 

 

/**
 * @author zhuc
 * @version 2012-8-21 下午1:45:19
 */
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext ac = new FileSystemXmlApplicationContext(
				"src/test/java/org/zhuc/maven/aspectj/aspectj.xml");
		Service s = (Service) ac.getBean("service");
		String str = s.addLog("zhuc");
		System.out.println(str);
	}

}

 

运行结果:

zhuc
参数: zhuc
日志管理,添加Log
doAfter......
hello:zhuc

分享到:
评论

相关推荐

    spring aspectj.jar

    《Spring AOP与AspectJ深度解析》 在Java开发领域,Spring框架以其强大的功能和灵活性深受开发者喜爱。其中,Spring的面向切面编程(AOP)特性极大地简化了代码的维护和扩展。AspectJ是Spring AOP的重要组成部分,...

    Spring AspectJ的学习一

    **Spring AspectJ 学习详解** 在Java世界中,Spring框架以其强大的依赖注入(DI)和面向切面编程(AOP)能力而闻名。AspectJ是AOP领域的一个强大工具,它扩展了Java语言,允许开发者创建所谓的"切面",来封装横切...

    spring基于AspectJ实现事务控制

    在Spring框架中,提供了多种事务管理方式,其中之一就是基于AspectJ的事务控制。本文将深入探讨如何在Spring中利用AspectJ实现事务控制。 首先,我们需要理解什么是AspectJ。AspectJ是一种面向切面编程(AOP)的...

    Spring 使用AspectJ 实现 AOP之前置通知小例子

    标题提到的"Spring 使用AspectJ 实现 AOP之前置通知小例子",指的是利用AspectJ在Spring中实现AOP的一种特定类型的通知——前置通知(Before advice)。前置通知在目标方法执行之前运行,但不会阻止方法的执行。这种...

    Spring AOP @AspectJ 入门实例

    本实例将带你深入理解并实践Spring AOP与@AspectJ的结合使用。 首先,了解AOP的基本概念。面向切面编程是一种编程范式,它允许程序员定义“切面”,即跨越多个对象的行为或责任。这些切面可以包含业务逻辑、日志、...

    spring AspectJ aop学习

    2. **加载时织入(Load-time weaving)**:在JVM加载类时,通过AspectJ的类加载器进行切面织入。 为了在Spring应用中使用AspectJ,我们需要在配置文件中启用AspectJ自动代理,并添加相关的依赖。例如,在Spring XML...

    Spring的AOP依赖包-AspectJ

    而AspectJ是Java平台上的一个开源项目,提供了一种强大的、类型安全的AOP解决方案,它能够与Spring框架完美结合,增强Spring的AOP功能。 首先,我们需要理解AOP的核心概念。切面(Aspect)是关注点的模块化,这些...

    spring和aspectj的aop实验

    2. **Spring AOP** Spring AOP主要通过代理模式实现,有接口代理和CGLIB代理两种方式。接口代理适用于实现了接口的目标对象,而CGLIB代理则用于未实现接口的对象。在Spring中,我们可以通过注解或XML配置来定义切面...

    Spring AOP之基于AspectJ注解总结与案例

    本篇内容将对Spring AOP中基于AspectJ注解的使用进行总结,并通过实际案例进行解析。 首先,让我们理解AspectJ注解在Spring AOP中的核心概念: 1. **@Aspect**: 这个注解用于定义一个类为切面,这个类将包含切点和...

    Spring AOP的AspectJ支持jar包

    Spring AOP的AspectJ支持jar包; 包括: com.springsource.net.sf.cglib-2.2.0.jar com.srpingsource.org.aopalliance-1.0.0.jar com.srpingsource.org.aspectj.weaver-1.68.RELEASE.jar

    spring aspectj-1.9.0.jar包

    aspectj.jar的1.9.0版本,下载后粘贴到所属的lib文件下即可

    aspectj的jar spring使用aop需要的jar

    2. **AspectJ集成**:虽然Spring AOP功能强大,但AspectJ提供了更全面的AOP特性,如注解支持、类型匹配、环绕通知等。当Spring与AspectJ结合使用时,可以实现更复杂、更细粒度的切面。这通常需要引入AspectJ的库,如...

    Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)

    本教程将探讨如何在Spring中结合AspectJ实现AOP,包括基于XML配置和基于注解的方式。 **一、AOP基本概念** AOP的核心概念有切面(Aspect)、连接点(Join Point)、通知(Advice)、切点(Pointcut)和引入...

    Spring 使用AspectJ 实现 AOP

    2. **配置AspectJ**:在Spring的配置文件中启用AspectJ自动代理,可以通过`&lt;aop:aspectj-autoproxy&gt;`标签来实现。 3. **定义切面**:创建一个Java类作为切面,该类需要使用`@Aspect`注解。在切面类中,我们可以定义...

    spring源码导入所需aspectj包

    2. **启用AOP代理**:在Spring配置文件中,你需要开启AOP代理,这可以通过以下方式实现: ```xml &lt;aop:aspectj-autoproxy /&gt; ``` 这行配置会自动创建代理以应用切面。 3. **编写Aspect**:在Spring应用中,你...

    Spring2.5使用AOP需要的aspectJ

    2. 在Spring配置文件中启用AspectJ自动代理。可以通过设置`&lt;aop:aspectj-autoproxy/&gt;`元素来实现。 3. 定义切面类,其中包含通知方法。这些方法需要使用特定的注解(如`@Before`、`@After`、`@Around`等)来指定其...

    Spring @AspectJ 实现AOP 入门例子

    本篇文章将深入探讨如何利用Spring的@AspectJ注解来实现AOP,这是一个入门级别的例子,旨在帮助开发者理解并掌握这一关键特性。 首先,我们要明白什么是AOP。面向切面编程是一种编程范式,它允许程序员定义“切面”...

    AspectJ in Action: Enterprise AOP with Spring Applications

    2. **面向切面编程(AOP):**深入探讨了AOP的概念、原理及其实现方式,特别是如何利用AspectJ进行开发。 3. **最佳实践与陷阱避免:**作者通过丰富的实战经验总结了一系列AOP应用的最佳实践方法,并指出了常见的开发...

    jar包---Spring Aop AspectJ新增包.rar

    2. 配置Spring,声明启用AspectJ自动代理,可以通过`&lt;aop:aspectj-autoproxy&gt;`标签实现。 3. 定义切面(Aspect),切面是AOP的核心,包含切点(Pointcut)和通知(Advice)。切点是关注点的具体定位,通知是切点发生...

    SpringAOP+AspectJ

    **Spring AOP与AspectJ详解** 在现代软件开发中,面向切面编程(Aspect-Oriented Programming,简称AOP)是一种强大的设计模式,它允许我们分离关注点,将横切关注点(如日志、事务管理、权限控制等)与核心业务...

Global site tag (gtag.js) - Google Analytics