Spring扩充内容.doc_第1页
Spring扩充内容.doc_第2页
Spring扩充内容.doc_第3页
Spring扩充内容.doc_第4页
Spring扩充内容.doc_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

扩充内容:扩充内容:1Spring:1构造注入1通知的四种类型1spring的bean生命周期1集合类型的注入1正则的切点切入匹配方法名1延迟加载1Introduction 引入通知(待续)11Spring:构造注入通知的四种类型spring的bean生命周期集合类型的注入正则的切点切入匹配方法名延迟加载一:控制反转(IoC = Inversion of Control)的类型1. 依赖查找a) 依赖拖拽b) 上下文配置依赖查找2. 依赖注入a) 构造器注入i. 当使用某些组件之前就需要实例化存在依赖关系的类的时候b) Setter依赖注入i. 它允许依赖关系被声明为接口,如果某个组件暴露了它的依赖关系,但是也愿意自己提供一个默认的依赖关系3. aAOP 概念切面:关注点的模块化,关注点可能横切多个对象连接点:程序执行过程中明确的点(方法的调用)处理:AOP柜架在特定连接点执行的动作(以拦截器作为处理模型)切入点:系列拦截点的集合,它确定处理触发的动机引入:添加方法或字体到处理的类目标对象:包含连接点的对象,也称为被处理对象,或者被代理对象AOP代理:AOP柜架创建的对象,包含处理.( JDK动态代理 :为实现接口的目标对象的代理 (默认) CGLIB代理 :不实现接口的目标对象的代理)处理类型1. Around 环绕(包围)通知erceptor.MethodInterceptor可在方法之前和之后运行,我们也可以定义在什么时候调用目标方法。2. Before 前置通知org.springframework.aop.MethodBeforeAdvice 可以在联结点执行前进行自定义的操作。Spring中只有一种联结点,却方法的调用。可以访问调用的目标方法,也可以对方法的参数进行操作,不过它不能影响方法调用的本身3. Throws 抛出(异常)通知org.springframework.aop.ThrowAdvice仅当方法调用抛出一个异常时才被调用,它在目标方法调用返回时执行。可以只接住特定的异常。可以访问抛出异常的方法,目标方法及参数4. AfterRetuning 后置(返回后)通知org.springframework.aop.AfterReturningAdvice通知在联结点处的方法调用已经完成,并且已经返回一个值时运行。可以访问调用的目标方法,以及该方法的参数和返回值,完全不能影响方法调用本身5. Introduction 引入通知org.springframework.aop.IntroducationInterceptor使用引入拦截器,可以定义通知引入的方法的实现实战Around处理:需要实现MethodInterceptor接口,由AOP联盟提供。public class MyAroundInterceptor implements MethodInterceptor / 该方法处理完成的动作Overridepublic Object invoke(MethodInvocation invocation) throws Throwable / 执行目标方法之前处理System.out.println(调用目标方法之前 :invocation对象: + invocation + ); /执行目标方法 此方法proceed:对连接点的整个拦截器链起作用, /即拦截器链中每个拦截器都执行该方法,并返回它的返回值Object rval = ceed();/ 执行目标方法之后的处理System.out.println(调用目标方法结束.);return rval;/目标类所要实现的接口public interface IPerson void info();/目标类实现接口 public class PersonImpl implements IPerson private String name;private int age;public void info() System.out.println(n此处是要执行目标方法);System.out.println(我的名字是: + + ,今年年龄为: + this.age + n);public void setName(String name) = name;public void setAge(int age) this.age = age;applicationContext-around.xml文件com.accp.csfx.aop.around.IPersonmyAroundInterceptorpublic static void main(String args) / 创建Spring容器实例ApplicationContext context = new ClassPathXmlApplicationContext(/applicationContext-around.xml);/ 获得AOP代理实例IPerson person = (IPerson) context.getBean(personOfAround);/ 执行AOP代理方法();结果:调用目标方法之前 :invocation对象: ReflectiveMethodInvocation: public abstract void com.accp.csfx.t41.aop.around.IP(); target is of class com.accp.csfx.t41.aop.around.PersonImpl此处是要执行目标方法我的名字是: Paulz,今年年龄为:28调用目标方法结束. Before处理。实现MethodBeforeAdvice.java接口n 仅仅在方法执行之前处理。目标方法的执行无须关心/before处理实现类MyBeforeAdvice.javapublic class MyBeforeAdvice implements MethodBeforeAdvice / 该方法在连接点之前执行Overridepublic void before(Method method, Object param, Object object) throws Throwable System.out.println(n方法执行之前.);System.out.println(目标方法 : + method.getName();System.out.println(方法参数是: + Arrays.toString(param);System.out.println(目标对象是: + object);/目标类所要实现的接口public interface IPerson void run(String content);/目标类实现IPerson接口 PersonImpl.javapublic class PersonImpl implements IPerson private String name;private int age;Overridepublic void run(String content) if (this.age 50) System.out.println(我还年轻,还有机会 . + content); else System.out.println(年龄来了,要锻炼了. + content);public void setName(String name) = name;public void setAge(int age) this.age = age;applicationContext-before.xmlcom.accp.csfx.t41.aop.before.IPersonmyBeforeAdvicepublic static void main(String args) ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext-before.xml);IPerson person = (IPerson) context.getBean(personOfBefore);person.run(哈哈哈);结果:方法执行之前.目标方法 : run方法参数是: 哈哈哈目标对象是:com.accp.csfx.t41.aop.before.PersonImpl9ced8e我还年轻,还有机会 .哈哈哈After处理:实现AfterReturningAdvice接口/实现自定义处理类,必须实现AfterReturningAdvice接口public class MyAfterAdvice implements AfterReturningAdvice Overridepublic void afterReturning(Object returnValue, Method method,Object param, Object target) throws Throwable System.out.println(n方法调用结束.);System.out.println(目标方法的返回值: + returnValue);System.out.println(目标方法名称: + method.getName();System.out.println(目标方法的参数: + Arrays.toString(param);System.out.println(目标对象: + target);public interface IPerson String test(String content);/目标类,实现接口 IPerson.javapublic class PersonImpl implements IPerson private String name;private int age;public void setName(String name) = name;public void setAge(int age) this.age = age;Overridepublic String test(String content) return 我的名字是: + name + ,今年年龄是: + age + + content;applicationContext-after.xmlcom.accp.csfx.t41.aop.after.IPersontestAdvisor .*test.*结果:方法调用结束.目标方法的返回值:我的名字是:Paulz,今年年龄是:28 哈哈哈目标方法名称:test目标方法的参数:哈哈哈目标对象:com.accp.csfx.t41.aop.after.PersonImpl1bc82e7 Throws处理:实现ThrowsAdvice接口,此接口没有任何方法/* * Throws处理的类,实现的接口中没有任何方法, * 但实现该接口必须实现如下形式的方法 * afterThrowing(Method,args,target,Throwable subclass); * 描述:上面方法的前三个参数为可选,第四个参数为必须的 * 如果有四个参数,则该处理可以访问被调用的方法的参数和目标对象 * * author Paulz */public class MyExceptionAdvice implements ThrowsAdvice / 抛出异常之后的处理public void afterThrowing(ClassNotFoundException e) System.out.println(系统抛出ClassNotFoundException异常,异常提示为:+ e.getMessage();/ 抛出异常后的处理,还可以访问方法,参数,目标对象public void afterThrowing(Method method, Object param, Object target, ArithmeticException e) System.out.println(该方法抛出Arithmetic系统,异常提示为: + e.getMessage();System.out.println(抛出该异常的方法为: + method.getName();System.out.println(抛出异常的方法的参数为 : + param);System.out.println(抛出异常的目标对象为: + target);接口和实现类ICacl.java和CaclImpl.java/目标类所要实现的接口 public interface ICacl void divide(int i, int j) throws ArithmeticException;void test(File file) throws ClassNotFoundException;/目标类,实现ICacl.java接口 public class CaclImpl implements ICacl Overridepublic void divide(int i, int j) throws ArithmeticException System.out.println(i / j);Overridepublic void test(File file) throws ClassNotFoundException throw new ClassNotFoundException(类没找到.);applicationContext-exception.xmlmyExceptionAdvicepublic static void main(String args) ApplicationContext context = new ClassPathXmlApplicationContext(/applicationContext-excepetion.xml);ICacl cacl = (ICacl) context.getBean(caclProxy);try cacl.divide(9, 0);/执行此方法1/ cacl.test(new File();/执行此方法2 catch (Exception e) 执行此方法1的结果:该方法抛出Arithmetic系统,异常提示为:/ by zero抛出该异常的方法为:divide抛出异常的方法的参数为 : Ljava.lang.Object;1c86be5抛出异常的目标对象为:com.accp.csfx.aop.exception.CaclImpl157fb52执行此方法2的结果为:系统抛出ClassNotFoundException异常,异常提示为:类没找到.Introduction 引入通知(待续)事务a. 编程式事务a) 避免使用。i. 大大提高程序的代码书写量,而且,将业务逻辑和事务控制采用硬编码的方式完全耦合,造成事务策略迁移困难b. 声明式事务a) 尽可能的使用i. 尤其需要大量的事务操作时,使用声明式事务更有价值,管理配置成本不高,相当方便声明式事务配置范例Bean的生命周期管理生命周期事件,只有当bean为singleton时,下述两个生命周期事件 才会触发。如果bean被配置为non-singleton,则spring就不会管其生命周期a post-initialization(初始化)a) 一旦spring完成bean全部属性值的设定,以及结束让bean执行的依赖关系检查之后,便会触发post-initialization事件b pre-destruction(毁灭/析构)a) 在spring销毁bean的实例之前触发。指定初始化方法init()或实现InitializingBean接口对属性进行初始化赋值说明:使用初始化方法是确保bean得到正确配置的理想方案。唯一限制是:它无法接受任何参数。可以指定任意返回类型,但spring视若无睹。甚至可以使用static访问 ,只要该方法接受参数即可。如果使用static初始化方法,这种机制大打折扣,因为你无法访问任何bean状态以验证它。有两种方案:一:实现InitializingBean接口二:在配置文件中指定初始化方法属性 /*实现InitializingBean接口,重写里面的afterPropertiesSet()方法,和init()方法里的内容完全一样,去掉init()方法*/public class SimpleBean implements InitialzingBean /未实现InitializingBean接口/public class SimpleBean private static final String DEFAULT_VALUE = Paulz;private String name;private int age = Integer.MIN_VALUE;public void setName(String name) = name;public void setAge(int age) this.age = age;/ 定义为初始化回调方法,检查属性是否已设定public void init() System.out.println(n初始化bean);if (null = name) name = DEFAULT_VALUE;if (age = Integer.MIN_VALUE) throw new IllegalArgumentException(必须给类 + SimpleBean.class+ 的年龄属性设置值);public String toString() return Name : + name + nAge : + age;public static SimpleBean getBean(String beanName, BeanFactory factory) try SimpleBean bean = (SimpleBean) factory.getBean(beanName);System.out.println(bean);return bean; catch (BeanCreationException e) System.out.println(一个错误发生在bean配置: + e.getMessage();return null;applicationContext.xml:如果使用的实现InitializingBean接口 ,则去掉配置文件中bean的属性:init-method=”init”测试public static void main(String args) BeanFactory factory = new XmlBeanFactory(new FileSystemResource(config/applicationContext.xml);SimpleBean bean1 = SimpleBean.getBean(bean1, factory);SimpleBean bean2 = SimpleBean.getBean(bean2, factory);SimpleBean bean3 = SimpleBean.getBean(bean3, factory);执行结果初始化beanName : luozhuanAge : 28初始化bean使用默认值Name : PaulzAge : 128初始化bean一个错误发生在bean配置:Error creating bean with name bean3 defined in file D:Y2WorkSpaceBeanLifeCycleconfigapplicationContext.xml: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 必须给类class com.lsjt.paulz.lifecycle.SimpleBean的年龄属性设置值销毁bean:两种选择说明:以下两种机制输出结果完全一样,为了确保应用程序体面的关闭,并且不致让资源仍处于打开或不一致状态,同时可移植性事关重大,回调优先选择。否则建议采用DisposableBean接口 以减少需要的配置数量一:实现Disposable Bean接口实现销毁bean二:指定destroy-method方法销毁bean实例public class DestructionBean implements InitializingBean /同时实现DisposableBean接口,重写里面的destroy方法,所以代码原封不对,仅仅/把配置文件中的destory-method=”destroy”属性删除即可/ public class DestructionBean implements /InitializingBean,DisposableBeanprivate InputStream is;private String filePath;Overridepublic void afterPropertiesSet() throws Exception System.out.println(初始化bean);if (filePath = null) throw new IllegalArgumentException(你必须给 + DestructionBean.class + 的属性filePath赋值);is = new FileInputStream(filePath);public void destroy() System.out.println(销毁bean);if (is != null) try is.close();is = null; catch (Exception e) System.err.println(警告:当关闭InputStream的时候,一个IO异常已经发生);public void setFilePath(String filePath) this.filePath = filePath;applicationContext-destruction.xml:指定bean属性destroy-methodd:/test.txt测试public static void main(String args) ConfigurableListableBeanFactory factory = new XmlBeanFactory(new FileSystemResource(config/applicationContext-destruction.xml);DestructionBean bean = (DestructionBean)factory.getBean(destructionBean);System.out.println(准备调用destroySingletons();factory.destroySingletons();Sy

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论