Spring AOP
来源:互联网 发布:python的多线程对象 编辑:程序博客网 时间:2024/06/15 17:37
public class After implements AfterReturningAdvice { @Override public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("this is after"); }}
public class Before implements MethodBeforeAdvice{ @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("this is before"); }}
public class Around implements MethodInterceptor{ @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { Object result=null; System.out.println(new Date()); result=methodInvocation.proceed();//调用要切入的方法 System.out.println(new Date()); return result; }}
public class Error implements ThrowsAdvice { public void afterThrowing(RuntimeException e) { System.out.println("运行错误"); System.out.println(e.getMessage()); } public void afterThrowing(Exception e) { System.out.println("this is exception"); System.out.println(e.getMessage()); } public void afterThrowing(SQLException e) throws Throwable { System.out.println("this is SQLException"); System.out.println(e.getMessage()); }}
public interface IService { public void save(); public void doo();}
public class Service implements IService { @Override public void save() { System.out.println("this is save!!"); } @Override public void doo() { System.out.println("this is doo!!"); }}
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml"); IService service=(IService)context.getBean("service"); service.save(); Hello hello=(Hello)context.getBean("hello"); hello.say(); }
在使用动态代理的时候 ProxySay proxySay=new ProxySay(); ISay pp=(ISay) proxySay.bind(new Person()); pp.say(); 调用绑定的时候需要强转为 Isay 接口类型,而且要用接口接收,否则 会爆出错误 Exception in thread "main" java.lang.ClassCastException: com.sun.proxy .$Proxy0 cannot be cast to com.aop.Person 以下几种写法都是 错误的 : ISayp=(Perso pn) proxySay.bind(new Person()); : Person pp=(Person) proxySay.bind(new Person());代理类方法参数解析 public Object invoke(Object proxy, Method method, Object[] args) Object proxy当前代理的类 Method method 当前方法 Object[] args 装有当前方法参数的数组 =============================================================== AOP概念: 1通知(advice) 要注入的代码(就是你想要的通知,也就是上面说的安全,事物,日志等) 类型:前通知,后通知,环绕通知,异常通知 2.连接点(JoinPoint)加入通知的地方 就是 spring 允许你使用通知的的地方 =============================================================== aspect 切面 ,专门做AOP的一个比较著名的框架AspectJ有关切入点配置器<bean id="service" class="com.com.springaop.Service"></bean><bean id="beforeAdvice" class="com.com.springaop.Before"></bean><bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><property name="advice"> <ref bean="beforeAdvice"></ref></property><property name="patterns"> <value>.*.*</value></property></bean><bean id="autoProxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>采用切入点注入,那么所注入的所有方法的对象,如果实现了接口,那么必须用接口类型接收对象如果没有实现对象,那么就可以用对象接收但是因为出现了这么一种情况,由于设置注入正则表达式的时候 .*.* 注入所有方法出现了,报错,但是改成 .*save.*,.*say.*就可以了,所以用注入所有点,是存在隐形bug的,所以最好指定注入点,造成上述错误的原因,在类的结构,或在spring 配置文件对该类bean 的设置,不想跟下去,指定切入点,解决问题就好了
0 0
- AOP、Spring的AOP
- AOP--Spring AOP
- Spring AOP 嵌套AOP
- spring AOP
- Spring AOP
- Spring AOP
- spring aop
- Spring AOP
- spring AOP
- spring aop
- Spring aop
- Spring-AOP
- Spring AOP
- spring aop
- spring aop
- Spring AOP
- Spring AOP
- Spring Aop
- 适配 iOS 10
- TCP/IP详解--拥塞控制 & 慢启动 快恢复 拥塞避免
- centos配置tomcat7
- PS颜色校正(Photoshop颜色显示偏色)
- placeholder兼容到ie8
- Spring AOP
- HTML第一章课后作业4
- Android Studio自动生成注释模板
- 一些常犯的错误
- Lua入门教程
- spring 容器的refresh方法
- 数据库分库分表(sharding)系列(二) 全局主键生成策略
- ffmpeg在Ubuntu14.04源码安装
- 使用可信科技的人脸识别活体认证遇到的问题