Spring Aspectj 面向切面示例
2015-12-15 16:49
645 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012003601/article/details/50317667
2.自定义切面代码:
4.最后这是测试的实现代码:
5.spring的配置文件为
首先导入需要的jar包
其中aspectjweaver的jar包必须是1.5版本以上的,cglib-nodep-2.2的jar包不能与cglib.jar不能共用。
接下来是代码层:
1.自定义注解代码:
package com.springtest.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface LogAnnotation { String desc() default ""; }
2.自定义切面代码:
package com.springtest.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import com.springtest.annotation.LogAnnotation; @Component @Aspect public class AspectLog { @AfterReturning("within(com.springtest.test.MainTestService) && @annotation(la)") public void aspectTest(JoinPoint joinpoint, LogAnnotation la){ System.out.println("this is aspectlog infomation"); System.out.println(la.desc()); } }3.应用自定义注解的代码:
package com.springtest.test; import com.springtest.annotation.LogAnnotation; public class MainTestService { @LogAnnotation(desc=("this is pass infotation")) public void test(){ System.out.println("this is before test"); } }
4.最后这是测试的实现代码:
package com.springtest.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TryTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); MainTestService mainTestService = (MainTestService) ctx.getBean("mainTestService",MainTestService.class); mainTestService.test(); } }
5.spring的配置文件为
<?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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <aop:aspectj-autoproxy /> <context:component-scan base-package="com.springtest" /> <bean id="mainTestService" class="com.springtest.test.MainTestService"> </bean> </beans>
相关文章推荐
- Spring3.0 学习-AOP面向切面编程_Spring AOP的注解模式即Aspectj模式
- Spring学习4-面向切面(AOP)之aspectj注解方式
- Spring面向切面的简单示例(基于XML文件配置)
- Spring之旅、装配Bean、最小化Spring XML配置、面向切面的Spring、征服数据库、事务管理示例源代码
- Spring AOP面向切面编程AspectJ详解
- spring学习--面向切面编程AOP和AspectJ
- Spring学习4-面向切面(AOP)之aspectj注解方式
- Spring AOP(面向切面示例)
- Spring2.0 AOP @AspectJ注解定义切面学习示例。
- 深入理解面向切面的编程AOP、AspectJ、Spring
- 面向切面(AOP)之Spring接口方式 schema配置方式 aspectj注解方式
- Spring学习4-面向切面(AOP)之aspectj注解方式
- Spring面向切面的简单示例(基于Annotation配置)
- Spring Boot - AOP(面向切面)
- Spring AOP面向切面编程之日志记录
- Spring 面向切面,简单入门(AOP)
- Spring Boot之AOP面向切面编程-实战篇
- 关于spring.net的面向切面编程 (Aspect Oriented Programming with Spring.NET)-使用工厂创建代理(Using the ProxyFactoryObject to create AOP proxies)
- 面向切面的Spring
- 【我看Spring】从一个简单的AOP示例看切面编程