spring学习--4

参考书籍《spring in action》

1. AOP

before you wak the walk, you have to learn to walk the walk.

1) advice---the job of an aspect (what and when)

/before

/after

/after-returning

/after throwing

/around: beforen and after

2) join points---a point in the execution of the application where an aspect can be plugged in.

3)pointcuts---where the advice should be woven

4)aspects--- advice & joincuts

5)introduction---add new methods or attributes to existing classes.

6)weaving---the process of applyng aspects to target object to create a

new proxied object.

/compile time

/class load time

/runtime

2. spring 对AOP的支持方式

1)classic spring proxy-based AOP

2)pure-POJO aspects

3) @AspectJ annotation driven aspects.

4) injected AspectJ aspects.

3.creating aspect in spring

1)selecting join points with pointcuts.

eg:

execution(* concert.Performance.perform())
and bean(‘woodstock‘)

2) creating annotated aspects

@Aspect
public class Audience {
@Pointcut("execution(** concert.Performance.perform(..))")
public void performance() {}
@Before("performance()")
public void silenceCellPhones() {
System.out.println("Silencing cell phones");
}
@Before("performance()")
public void takeSeats() {
System.out.println("Taking seats");
}
@AfterReturning("performance()")
public void applause() {
System.out.println("CLAP CLAP CLAP!!!");
}
@AfterThrowing("performance()")
public void demandRefund() {
System.out.println("Demanding a refund");
}
}

配置:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig {
@Bean
public Audience audience() {
return new Audience();
}
}

3)handling params in advice

4) annotation introductions

dynamic language like ruby and groovy can add new methods to an object or class.

时间: 2024-08-27 07:01:47

spring学习--4的相关文章

Struts2 + Spring 学习(一)搭建struts开发环境

·为Web应用增加Struts2支持 1. 登陆struts2官网下载struts2的最新版,本文所用的是Struts 2.3.16.2 我们所下载的压缩包内内容应给如下 其中,apps包含了struts2的实例应用,docs包含了struts2的相关文档,lib包含了Struts2框架的核心类库以及struts2的第三方插件类库,src包含了struc包含了struts2框架的全部源码. 2. 在Eclipse新建Web工程,将是所用到的类库放到WEB-INF/路径下 3. 编辑Web应用的w

Spring学习笔记(一)

Spring学习笔记(一) Spring核心思想: IOC:  Inversion Of Control (控制反转) / DI: Dependency Injection (依赖注入) AOP: Aspect Oriented Programming (面向切面编程) IOC 1. 简单的应用 Model package com.wangj.spring.model; public class User { private String username; private String pas

不错的Spring学习笔记(转)

Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是spring.jar和commons-logging.jar.此外为了便于测试加入了JUnit包.   在Myeclipse中创建Java项目.   编写一个接口类,为了简单,只加入了一个方法.   Java代码   1.package com.szy.spring.interfacebean;  

spring学习一——基本搭建,属性注入的两种方式

今天用spring 3.2.5搭建了基本的环境,spring出的太快了,前段时间才3.2.5,今儿个一瞧已经上了4的版本了,稍后给出spring的jar下载地址,毕竟现在官网上找不到了啊. 废话少说了,spring 3.2.5已经将所有的依赖包都放在了dist的lib下面,并且都有doc包和源码包,很是方便.先导入所需的jar包:core,context,beans,expression 四个jar包,除此之外,还需导入commons-logging. 下一步,新建xml文件,建议名称为 app

Spring学习(一)tomcat加载web.xml、以及项目集成Spring支持

tomcat容器加载web.xml 一. 1 .启动一个 WEB 项目的时候, WEB 容器会去读取它的配置文件 web.xml ,读取 <listener> 和 <context-param> 两个结点. 2 .紧急着,容创建一个 ServletContext ( servlet 上下文),这个 web 项目的所有部分都将共享这个上下文. 3 .容器将 <context-param> 转换为键值对,并交给 servletContext . 4 .容器创建 <li

Spring学习系列之——第三章:Spring中Bean的配置(一)

Spring的配置形式有两种:基于XML配置和基于注解配置. Bean的配置方式有以下几种: 通过全类名,即通过反射的方式: 通过工厂方法,有静态工厂方法和实例工厂方法: 通过FactoryBean配置: 通过XML文件配置bean 本篇文章将按照下面的目录来说明基于XML的方式配置bean JavaBean的创建 通过XML配置的方式来配置bean XMLbean的配置: spring的依赖注入的方式 属性注入 构造器注入 工厂方法注入(很少使用) 测试方法 IoC容器的实例化: Applic

spring学习(一)

spring的核心是IOC和DI,首先要明白IOC和DI的概念. IOC,即Inverse of Control,反转控制,就是将原本在程序中手动创建service对象的控制权交给spring容器管理,简单的说就是对象的创建权交给了spring容器管理. DI,即Dependency Injection,依赖注入,在web应用中,action调用service层,service调用dao层,也就是action依赖service,service依赖dao,而在spring容器管理对象时,在创建be

spring 学习笔记1

Spring 学习记录 任何一个成功的应用都是由多个为了实现某一个业务目标而相互协作的组件构成的.这些组件必须彼此了解,并相互协作来完成工作. 在Spring 中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互合作的对象引用赋予各个对象. 创建应用对象之间协作关系的行为通常被称为(装配),这是依赖注入的本质.

Spring学习之Ioc控制反转(1)

开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------------------------------开始啦啦啦啦啦------------------------------------------------------------------------------- 从开始接触spring起,听到最多的就是Ioc(控制反转)和AOP(面向切面编程

《Spring学习笔记》:Spring、Hibernate、struts2的整合(以例子来慢慢讲解,篇幅较长)

<Spring学习笔记>:Spring.Hibernate.struts2的整合(以例子来慢慢讲解,篇幅较长) 最近在看马士兵老师的关于Spring方面的视频,讲解的挺好的,到了Spring.Hibernate.struts2整合这里,由于是以例子的形式来对Spring+Hibernate+struts2这3大框架进行整合,因此,自己还跟着写代码的过程中,发现还是遇到了很多问题,因此,就记录下. 特此说明:本篇博文完全参考于马士兵老师的<Spring视频教程>. 本篇博文均以如下这