- 浏览: 228106 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (168)
- j2SE--->Base (24)
- j2SE-->High (5)
- DataStruArith (9)
- DesignPattern (2)
- db (16)
- J2EEBase (11)
- j2EE--->Frame (17)
- javascript (29)
- jquery (6)
- 云帆考试 (0)
- InterviewQues (6)
- IntelligenceQues (1)
- QuesList (3)
- MathKnowledge (1)
- ProgManage (3)
- linux (1)
- ProgExperience (8)
- other (17)
- xml (2)
- webservice (1)
- jquery给日期控件赋初始值 (1)
最新评论
-
bsszds:
可以看看java编程思想第二十章了解下注解的基本原理,然后再去 ...
Aop -
chenzheng8975:
注解的方式不好理解啊
Aop -
bsszds:
调用isArray()的时候返回值是true,则说明是数组。上 ...
关于判断一个Char数组属于基本数据类型,还是一个真正的对象 -
商人shang:
2.调用isArray()的时候返回值是true。( 不是一 ...
关于判断一个Char数组属于基本数据类型,还是一个真正的对象 -
myhand:
谢谢分享,感谢。
java反射之使用Constructor调用私有构造函数
一些介绍:
1.面向方面编程是希望能够将通用需求功能从不相关的类当中分离出来,能够使得很多类共享一个行为,一旦发生变化,不必修改很多类,而只需要修改这个行为即 可 、
2.应用:
AOP 主要应用于日志记录,性能统计,安全控制 , 事务处理等方面。
在spring中配置aop
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy /> <!– 支持AOP的注解方式--> </beans>
Annotation 方式配置 AOP
package com.yang.interceptor; 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; import com.yang.bean.User; /** * 日志拦截器 */ @Aspect @Component public class TestInterceptor { @Pointcut("execution(public * com.yang.test.*.test*(..))") //声明一个名字为log的切入点 public void log(){ } @Before("log()"+"&&args(user)") //使用pointcut-->log .前置通知.处理传入的参数跟下面的形参保持一致即可。 public void before(User user){ System.out.println("方法执行前:"+user.getUname()); user.setUname("lisi"); System.out.println("方法执行前!"); } @After("log()") //最终后置通知。不管有无异常,最终都会执行! public void after(){ System.out.println("方法执行后!"); } @AfterReturning("log()") //方法正常结束,后置通知 public void afterReturning(){ System.out.println("方法执行后!after returning!"); } @AfterThrowing("log()") //抛出异常会执行,异常通知 public void afterThrowing(){ System.out.println("方法执行后!after throwing!"); } @Around("log()") //环绕通知在一个方法执行之前和之后执行 public Object around(ProceedingJoinPoint pjp){ System.out.println("环绕通知,方法执行前!"); Object obj = null; try { obj = pjp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("环绕通知,方法执行后!"); return obj; } } package com.yang.test; import org.springframework.stereotype.Component; import com.yang.bean.User; @Component("userDao") public class UserDao { public void test1(User user){ System.out.println(user.getUname()); System.out.println("UserDao.test1()"); } public void test2(){ System.out.println("UserDao.test2()"); } public void aaa(){ System.out.println("UserDao.aaa()"); } } package com.yang.bean; public class User { private int id; private String uname; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } } package com.yang.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yang.bean.User; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans.xml"}); UserDao us = (UserDao) context.getBean("userDao"); User u = new User(); u.setUname("zhangsan"); us.test1(u); us.test2(); us.aaa(); } }
个人的理解:
TestInterceptor中的方法中对应的注解最后是根据反射拿到的。
@Pointcut("execution(public * com.yang.test.*.test*(..))") //声明一个名字为log的切入点
Pointcut中的注解是使用正则进行解析的,当然中间也有一些替换。
xml中配置xop
<?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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <aop:aspectj-autoproxy /> <!-- 支持注解方式 --> <context:component-scan base-package="com.yang"/> <!-- 采用配置方式使用AOP --> <bean id="logInterceptor" class="com.yang.spring.aop.LogInterceptor"></bean> <aop:config > <!--定义切点 名字log --> <aop:pointcut expression="execution(public !java.lang.String com.yang.spring.aop.UserDaoImpl.*(com.bjsxt.spring.aop.User,..))" id="log"/> <!-- 定义切面 logAspect--> <aop:aspect id="logAspect" ref="logInterceptor"> <!-- 定义前置通知 --> <!-- arg-names="user,age,name,test" 多参数 arg-names 指定before参数名 --> <aop:before method="before" arg-names="user" pointcut="execution(!java.lang.String com.yang.spring.aop.UserDaoImpl.add(..)) and args(user)" /> <!-- 后置通知 --> <aop:after method="after" pointcut-ref="log" /> <aop:after-throwing method="afterThrowing" pointcut-ref="log"/> <aop:after-returning method="afterReturning" pointcut-ref="log"/> <aop:around method="around" pointcut-ref="log"/> </aop:aspect> </aop:config> </beans>
arg-names是为了显式地定义before方法中对应的参数的名字。
pointcut="execution(!java.lang.String com.yang.spring.aop.UserDaoImpl.add(..)) and args(user)" /> 我的理解就是相当于查add方法,参数个数为1个,类型为user,(必须要有对应的User类,before中的before方法必须是 before(User user)类型的)
评论
2 楼
bsszds
2012-10-10
可以看看java编程思想第二十章了解下注解的基本原理,然后再去看spring的注解,hibernate的注解,或者其他框架里的注解,估计就会明白些了!
1 楼
chenzheng8975
2012-10-10
注解的方式不好理解啊
发表评论
-
struts2命名规范问题
2012-10-27 17:09 1039使用struts2开发项目,在action中使用domain ... -
struts2中三种常见的接收参数的方式哪种最常用
2012-10-19 00:20 3433struts2中常见的接收参数的方式主要有三种: 1.使用A ... -
spring注解之@Autowired
2012-10-15 23:46 792最近发现了一个非常好用的spring的注解@Autowired ... -
在hibernate和spring集成中使用openSession还是getCurrentSession
2012-09-30 21:16 1409在hibernate和spring集成之后,如果我们使用spr ... -
struts2之ognl表达式使用注意点
2012-09-21 19:25 684struts中,ognl表达式只能在struts2的标签中使用 ... -
hibernate之对象的三种状态
2012-09-12 18:30 6021 )瞬态:表示该实体对象在内存中是自由存在的,也就是说与数据 ... -
hibernate查询之交叉查询误用
2011-12-21 15:06 904T1:(多的一方) public class T1 impl ... -
hibernate使用注意点之文件头更新
2011-11-14 15:12 664最近下载了hibernate3.6.8进行使用,然后和stru ... -
hibernate多对多查询
2011-10-25 14:38 695下面用最简单的Role和User来展现一个简单的hiberna ... -
hibernate关联双向多对一的join查询问题
2011-07-13 12:00 1362这是从别人那引用过来的代码,仅供参考: 1.如果没有在Hi ... -
hibernate之参数绑定注意点
2011-06-16 10:57 8571.Query查询时,使用q.setParameter的方式设 ... -
hibernate查询时传递的参数有中文
2011-06-16 10:42 1434看这样一个查询: String name="杨& ... -
hibernate的关联关系----一对一单向关联之外键关联
2011-06-09 14:09 810实体类: Address: public class Ad ... -
hibernate的关联关系----一对一单向关联之主键关联
2011-06-09 13:47 922一对一单向关联: husband 实体类: public ... -
JFreeChart学习(一)
2011-02-10 17:30 687一:简介: WW 的发展使得基于因特网的应用程序不再局限于静 ... -
hibernate错误示例一
2011-04-07 23:18 754实体类: @Entitypublic class Cup { ...
相关推荐
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...
3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...
使用 Spring AOP 进行方法耗时监测的好处有以下几点: 1. 代码实现简单,易于维护:使用 Spring AOP 可以将耗时监测的逻辑与业务逻辑进行解耦,避免业务逻辑代码的冗余和代码维护难度的提高。 2. 安全性高:使用 ...
开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具...
AOP的概念也很好理解,跟中间件差不多,说白了,就是我可以任意地在方法的前面或后面添加代码,这很适合用于缓存、日志等处理。 在net core2.2时,我当时就尝试过用autofac实现aop,但这次我不想用autofac,我用了一...
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
内容概要:springboot+拦截器+aop+自定义注解+本地线程实现统一接口日志记录,记录下接口所在模块、接口描述、接口请求参数、接口返回参数、接口请求时间以及接口耗时用于接口优化,接口记录参数以及操作人防止使用...
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
java项目中 AOP事务所需要用的jar包....................................啊..........................................................
赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
aop所依赖的所有包+文档+源码,最新版全套aop aspectjweaver aopalliance aspects aspectjrt
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
spring aop jar 包
在使用spring的aop功能时,这两个jar是必须的,否则会报错,如下: Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException at java.net....
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象...
AOP in .NET: Practical Aspect-Oriented Programming 296 pages Publisher: Manning Publications; Pap/Psc edition (June 25, 2013) Language: English ISBN-10: 1617291145 ISBN-13: 978-1617291142 ...
SpringBoot下的Spring——DAY04——动态代理总结、AOP、自定义注解进行拦截、动态获取注解参数、通知方法 1.动态代理总结 1.1 JDK动态代理特点 1.2 CGlib动态代理 1.2.1 CGLib特点说明 1.3 动态代理的作用 2 Spring...