ACT官方新近推出的线上备考学习系统AOP怎么样?有什么用处?

Spring在英语中含义是春天,对于JavaEE开发者来说,Spring框架出现确实带来了一股全新的春天的气息。早在2002年,Rod
Johson在其编著的《Expert one to one J2EE design anddevelopment》书中,对Java
EE框架臃肿、低效、脱离现实的种种现状提出了很多质疑,并积极寻求探索革新之道。由他主导编写了interface21框架,从实际需求出发,着&#30524;于轻便、灵巧,易于开发、测试和部署的轻量级开发框架。以interface21框架为基础,并集成了其它许多开源成果,于2004年3月24日,发布了<span style="color:#.0正式版取名为Spring。
&&& Spring的核心是个轻量级容器,实现了IoC(控制翻转)模式的容器,基于此核心容器所建立的应用程序,可以达到程序组件的松散耦合。这些特性都使得整个应用程序维护简化。Spring框架核心由下图所示的七个模块组成。
1、核心容器(Core)
&&&&&&& 这是Spring框架最基础的部分,它提供了依赖注入(Dependency
Injection)特征来实现容器对Bean的管理。这里最基本的概念是BeanFactory,它是任何Spring应用的核心。BeanFactory是工厂模式的一个实现,它使用IoC将应用配置和依赖说明从实际的应用代码中分离出来。
2、AOP模块
&&&&& AOP即面向切面编程技术,Spring在它的AOP模块中提供了对面向切面编程的丰富支持。AOP允许通过分离应用的业务逻辑与系统级服务(例如安全和事务管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责其它的系统级关注点,例如日志或事务支持。
3、对象/关系映射集成模块ORM
&&&& Hibernate是成熟的ORM产品,Spring并没有自己实现ORM框架而是集成了几个流行的ORM产品如Hibernate、JDO和iBATIS等。可以利用Spring对这些模块提供事务支持等。
4、JDBC抽象和DAO模块
&&&& Spring虽然集成了几个ORM产品,但也可以不选择这几款产品,因为Spring提供了JDBC和DAO模块。该模块对现有的JDBC技术进行了优化。你可以保持你的数据库访问代码干净简洁,并且可以防止因关闭数据库资源失败而引起的问题。
5、Spring的Web模块
&&&&& Web上下文模块建立于应用上下文模块之上,提供了一个适合于Web应用的上下文。另外,这个模块还提供了一些面向服务支持。例如:实现文件上传的multipart请求,它也提供了Spring和其它Web框架的集成,比如Struts、WebWork。
6、应用上下文(Context)模块
&&&&&& 核心模块的BeanFactory使Spring成为一个容器,而上下文模块使它成为一个框架。Web上下文模块建立于应用上下文模块之上,提供了一个适合于Web应用的上下文。该模块还提供了一些面向服务支持这个模块扩展了BeanFactory的概念,增加了对国际化(I18N)消息、事件传播以及验证的支持。
&&&&& 另外,这个模块还提供了许多企业服务,例如电子邮件、JNDI访问、EJB集成、远程以及时序调度(scheduling)服务。也包括对模版框架例如Velocity和FreeMarker集成的支持。
7、Spring的MVC框架
&&&&& Spring为构建Web应用提供了一个功能全面的MVC框架。虽然Spring可以很容易地与其它MVC框架集成,例如Struts2,但Spring的MVC框架使用IoC对控制逻辑和业务对象提供了完全的分离。
二.Spring入门示例
&&&&&&& spring2.5基础包:spring.jar和commons-logging.jar
&&&&&&& spring3.x基础包:
&commons-logging.jarorg.springframework.asm-3.1.1.RELEASE.jarorg.springframework.beans-3.1.1.RELEASE.jarorg.springframework.context.support-3.1.1.RELEASE.jarorg.springframework.context-3.1.1.RELEASE.jarorg.springframework.core-3.1.1.RELEASE.jarorg.springframework.expression-3.1.1.RELEASE.jar
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
建立好web工程或Java工程后在MyEclipse中可以加入Spring的支持,导入Spring的核心包。如果是Eclipse则在Spring网站上下载Spring的核心包并加入到工程中。
<span style="color:#、编写一个普通的Java类(JavaBean)
package cn.hncu.s25.
public class Person {
public Person() {
System.out.println(&这是构造函数&);
public String getName() {
public void setName(String name) {
this.name =
public Integer getAge() {
public void setAge(Integer age) {
this.age =
public String toString() {
return &Person [name=& + name + &, age=& + age + &]&;
<span style="color:#、在Spring配置文件applicationContext.xml。将JavaBean由Spring容器来管理。(2.5和3.x版本不一致,这里一2.5版本为例)
&?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:context=&http://www.springframework.org/schema/context&
xmlns:tx=&http://www.springframework.org/schema/tx&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd&&
&bean id=&p& class=&cn.hncu.s25.domain.Person& scope=&singleton&&&/bean&
<span style="color:#、如何使用Spring容器配置的Bean
BeanFactory factory=new XmlBeanFactory(new ClassPathResource(&applicationContext.xml&));//这是2.5版本的方法
Person p=(Person) factory.getBean(&p&);
System.out.println(p.hashCode());
三、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:context=&http://www.springframework.org/schema/context&
xmlns:tx=&http://www.springframework.org/schema/tx&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd&&
&bean id=&p& class=&cn.hncu.s3x.domain.Person&&
&property name=&name& value=&Jack&&&/property&
&property name=&age& value=&23&&&/property&
&bean id=&user& class=&cn.hncu.s3x.demo2.User&&
&property name=&name& value=&Jack& /&
&property name=&age& value=&20& /&
&property name=&cat& ref=&cat& /&
&property name=&pets&&
&value&Tom&/value&
&value&dog&/value&
&value&pig&/value&
&/property&
&property name=&map&&
&entry key=&name& value=&中国&&
&/property&
&property name=&set&&
&value&aaa&/value&
&value&bbb&/value&
&/property&
&property name=&objs&&
&value&222&/value&
&ref bean=&cat2& /&
&ref bean=&cat2& /&
&ref bean=&cat& /&
&bean class=&cn.hncu.s3x.demo2.Cat&&
&property name=&name& value=&我的猫& /&
&/property&
&property name=&list&&
&entry key-ref=&p& value-ref=&cat&&&/entry&
&entry value-ref=&cat2&&
&bean class=&cn.hncu.s3x.domain.Person&&
&property name=&name& value=&王建安& /&
&property name=&age& value=&25& /&
&/property&
&bean id=&cat& class=&cn.hncu.s3x.demo2.Cat&&
&property name=&name& value=&Tom猫& /&
&bean id=&cat2& class=&cn.hncu.s3x.demo2.Cat&&
&property name=&name& value=&黑猫& /&
这是Bean对象
package cn.hncu.s3x.demo2;
import java.util.A
import java.util.L
import java.util.M
import java.util.S
public class User {
private List&String&
private Map&String,Object&
private Set&String&
private Object objs[];
private List&Map&Object, Cat&&
public List&Map&Object, Cat&& getList() {
public void setList(List&Map&Object, Cat&& list) {
this.list =
public Set&String& getSet() {
public void setSet(Set&String& set) {
this.set =
public Object[] getObjs() {
public void setObjs(Object[] objs) {
this.objs =
public Map&String, Object& getMap() {
public void setMap(Map&String, Object& map) {
this.map =
public List&String& getPets() {
public void setPets(List&String& pets) {
this.pets =
public String getName() {
public void setName(String name) {
this.name =
public Integer getAge() {
public void setAge(Integer age) {
this.age =
public Cat getCat() {
public void setCat(Cat cat) {
this.cat =
&#160;&#160; &#160;public String toString() {
&#160;&#160; &#160;&#160;&#160; &#160;return &User [name=& + name + &, age=& + age + &, cat=& + cat
&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;+ &, pets=& + pets + &, map=& + map + &, set=& + set
&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;+ &, objs=& + Arrays.toString(objs) + &, list=& + list + &]&;
&#160;&#160; &#160;}
package cn.hncu.s3x.demo2;
public class Cat {
&#160;&#160; &#160;private S
&#160;&#160; &#160;public String getName() {
&#160;&#160; &#160;&#160;&#160; &#160;
&#160;&#160; &#160;}
&#160;&#160; &#160;public void setName(String name) {
&#160;&#160; &#160;&#160;&#160; &#160;this.name =
&#160;&#160; &#160;}
&#160;&#160; &#160;@Override
&#160;&#160; &#160;public String toString() {
&#160;&#160; &#160;&#160;&#160; &#160;return &Cat [name=& + name + &]&;
&#160;&#160; &#160;}
&#160;&#160; &#160;
package cn.hncu.s3x.
public class Person {
&#160;&#160; &#160;private S
&#160;&#160; &#160;private I
&#160;&#160; &#160;public String getName() {
&#160;&#160; &#160;&#160;&#160; &#160;
&#160;&#160; &#160;}
&#160;&#160; &#160;public void setName(String name) {
&#160;&#160; &#160;&#160;&#160; &#160;this.name =
&#160;&#160; &#160;}
&#160;&#160; &#160;public Integer getAge() {
&#160;&#160; &#160;&#160;&#160; &#160;
&#160;&#160; &#160;}
&#160;&#160; &#160;public void setAge(Integer age) {
&#160;&#160; &#160;&#160;&#160; &#160;this.age =
&#160;&#160; &#160;}
&#160;&#160; &#160;@Override
&#160;&#160; &#160;public String toString() {
&#160;&#160; &#160;&#160;&#160; &#160;return &Person [name=& + name + &, age=& + age + &]&;
&#160;&#160; &#160;}
&#160;&#160; &#160;
这是测试代码
package cn.hncu.s3x.demo2;
import org.junit.T
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class Demo2 {
public void demo1(){
ApplicationContext act=new ClassPathXmlApplicationContext(&app2.xml&);
User user=act.getBean(&user&, User.class);
System.out.println(user);
四、Spring IOC 控制反转
&&&& IoC(Inversion of Control)中文译为控制反转也可以叫做DI(Dependency
Injection,依赖注入)。
控制反转模式的基本概念是:不直接创建对象,但是在xml配置文件中描述创建它们的方式。在工程中使用该Bean时由Spring容器创建Bean的实例。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。
第1步、Spring注入(又称依赖注入DI):其目的是为其中bean的属性赋&#20540;
第<span style="color:#步:在配置文件中注入属性的初始&#20540;
五、Spring Bean的作用域
&&&& 在Spring2.0之前bean只有2种作用域即:singleton(单例)、non-singleton(也称prototype),
Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的Bean。
&&&& 因此,默认情况下Spring2.0现在有五种类型的Bean。当然,Spring2.0对Bean的类型的设计进行了重构,并设计出灵活的Bean类型支持,理论上可以有无数多种类型的Bean,用户可以根据自己的需要,增加新的Bean类型,满足实际应用需求。
<span style="color:#、singleton单实例
& &当一个bean的作用域设置为singleton,那么Spring
IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。换言之,当把一个bean定义设置为singleton作用域时,Spring
IOC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存(singletoncache)中,并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例。
&beanid=&userdao&class=&com.bean.UserDao&scope=&singleton&/&
或&beanid=& userdao& class=&com.bean.UserDao&singleton=&true&/&&
UserDaobean1=(
UserDao)factory.getBean(&userdao&);
UserDaobean2=(
UserDao)factory.getBean(&userdao&);
<span style="color:#、prototype多实例&&
&&& 每个Spring容器中,一个bean对应多个实例.
prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)都会产生一个新的bean实例,相当一个new的操作。
&beanid=&userdao&class=&com.bean.UserDao&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&scope=&prototype&/&
或&beanid=& userdao& class=&com.bean.UserDao&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&singleton=&false&/&&
&UserDao bean1=(UserDao)factory.getBean(&userdao&);
&UserDao bean2=(UserDao)factory.getBean(&userdao&);
Bean1与bean2的引用不相同。每次使用getBean()重新产生一个实例
<span style="color:#、request&&&
&&&& request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP
request内有效。
&&& &bean id=&userdao&class=&com.bean.UserDao&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&scope=&request&/&
<span style="color:#、session&&&&
&&&&& session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP
session内有效.
Spring AOP编程
&&& 切面(Aspect):简单的理解就是把那些与核心业务无关的代码提取出来,进行封装成一个或几个模块用来处理那些附加的功能代码。(如日志,事务,安全验证)我们把这个模块的作用理解为一个切面,其实切面就是我们写一个类,这个类中的代码原来是在业务模块中完成的,现在单独成一个或几个类。在业务模块需要的时候才织入。
&&& 连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。
在Spring AOP中,一个连接点总是代表一个方法的执行。通过声明一个JoinPoint类型的参数可以使通知(Advice)的主体部分获得连接点信息。&
&&& 切入点(Pointcut):本质上是一个捕获连接点的结构。在AOP中,可以定义一个pointcut,来捕获相关方法的调用
&&&&&织入(Weaving):把切面(aspect)连接到其它的应用程序类型或者对象上,并创建一个被通知(advised)的对象。这些可以在编译时,类加载时和运行时完成。Spring和其它纯Java
AOP框架一样,在运行时完成织入。
&&&&&通知(Advice):在切面的某个特定的连接点(Joinpoint)上执行的动作。通知有各种类型,其中包括“around”、“before”和“after”等通知。通知的类型将在后面部分进行讨论。许多AOP框架,包括Spring,都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链。
通知的类型:
&&& 前置通知(Before advice):在某连接点(join
point)之前执行的通知,但这个通知不能阻止连接点前的执行(除非它抛出一个异常)。
&&& 返回后通知(After returning advice):在某连接点(join
point)正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
&&& 抛出异常后通知(After throwing advice):在方法抛出异常退出时执行的通知。
&&& 后置通知(After
(finally)advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
&&&& 环绕通知(Around Advice):包围一个连接点(join
point)的通知,如方法调用。这是最强大的一种通知类型。 环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它们自己的返回&#20540;或抛出异常来结束执行。
下面演示代码:
纯Java方式写AOP(拦截技术)
package cn.hncu.spring4x.
import java.lang.reflect.M
import org.aopalliance.aop.A
import org.aopalliance.intercept.MethodI
import org.aopalliance.intercept.MethodI
import org.junit.T
import org.springframework.aop.A
import org.springframework.aop.AfterReturningA
import org.springframework.aop.MethodBeforeA
import org.springframework.aop.framework.ProxyF
import org.springframework.aop.framework.ProxyFactoryB
import org.springframework.aop.support.DefaultPointcutA
import org.springframework.aop.support.JdkRegexpMethodP
import cn.hncu.spring4x.domain.P
public class AopDemo {
@Test//纯Java的方式实现切面(拦截)技术
public void demo1(){
Person p=new Person();
ProxyFactory factory=new ProxyFactory(); //该类的功能没有ProxyFactoryBean强
factory.setTarget(p);//1 给代理工厂一个原型对象
//切面 = 切点 + 通知
JdkRegexpMethodPointcut pointcut=new JdkRegexpMethodPointcut();
pointcut.setPattern(&cn.hncu.spring4x.domain.Person.run&);
pointcut.setPattern(&.*run.*&);ProxyFactory对setPattern无效
Advice advice=new MethodInterceptor() {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println(&前面拦截&);
Object obj=invocation.proceed();
System.out.println(&后面拦截&);
//切面 = 切点 + 通知
Advisor advisor=new DefaultPointcutAdvisor(pointcut, advice);
factory.addAdvice(advice);
Person p2=(Person) factory.getProxy();
p2.run(5);
@Test//纯Java的方式实现切面(拦截)技术
public void demo2(){
ProxyFactoryBean factoryBean=new ProxyFactoryBean();
factoryBean.setTarget(new Person());
//切面 = 切点 + 通知
JdkRegexpMethodPointcut pointcut=new JdkRegexpMethodPointcut();
pointcut.setPattern(&.*run.*&);
前切面---不需要放行,原方法也能执行
Advice beforeAdvice=new MethodBeforeAdvice() {
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println(&beforeAdvice拦截&);//正则表达式有效
Advice afterReturning=new AfterReturningAdvice() {
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println(&afterReturning&);
Advice aroundAdvice=new MethodInterceptor() {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println(&前面拦截&);
Object obj=invocation.proceed();
System.out.println(&后面拦截&);
Advisor advisor1=new DefaultPointcutAdvisor(pointcut, beforeAdvice);
Advisor advisor2=new DefaultPointcutAdvisor(pointcut, afterReturning);
Advisor advisor3=new DefaultPointcutAdvisor(pointcut, aroundAdvice);
factoryBean.addAdvisors(advisor1,advisor2,advisor3);
//2 给代理工厂一个切面 ---注意,添加的顺序的拦截动作执行的顺序是有关系的!!!
//先加的切面,如果拦前面,就拦在最前面,如果拦后面,就拦在最后面.
Person p = (Person) factoryBean.getObject(); //3 从代理工厂中获取一个代理后的对象
//p.run();
//p.run(0);
下面演示5种理由配置文件AOP
通知:AroundAdvice.java
package cn.hncu.spring4x.
import org.aopalliance.intercept.MethodI
import org.aopalliance.intercept.MethodI
public class AroundAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println(&前面拦拦....&);
Object resObj = invocation.proceed();//放行
System.out.println(&后面拦拦.....&);
return resO
测试代码,
package cn.hncu.spring4x.
import org.junit.T
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class AopXmlDemo {
@Test//采用配置文件的方式使用切面拦截
public void demo1(){
ApplicationContext act=new ClassPathXmlApplicationContext(&cn/hncu/spring4x/aop/1.xml&);
Cat cat=act.getBean(&catProxide&,Cat.class);//要从catProxide返回
cat.run();
cat.say();
cat.run(6);
@Test//把切点和通知配置成 切面的内部bean
public void demo2(){
ApplicationContext ctx = new ClassPathXmlApplicationContext(&cn/hncu/spring4x/aop/2.xml&);
Cat cat = ctx.getBean(&catProxide&,Cat.class);
cat.run();
cat.say();
cat.run(7);
@Test//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置
public void demo3(){
ApplicationContext ctx = new ClassPathXmlApplicationContext(&cn/hncu/spring4x/aop/3.xml&);
Cat cat = ctx.getBean(&catProxide&,Cat.class);
cat.run();
cat.say();
cat.run(7);
@Test//自动代理
public void demo4(){
ApplicationContext ctx = new ClassPathXmlApplicationContext(&cn/hncu/spring4x/aop/4.xml&);
Cat cat = ctx.getBean(Cat.class);
cat.run();
cat.say();
cat.run(7);
@Test//自己写的自动代理
public void demo5(){
ApplicationContext ctx = new ClassPathXmlApplicationContext(&cn/hncu/spring4x/aop/5.xml&);
Cat cat = ctx.getBean(&cat&,Cat.class);
cat.run();
cat.say();
cat.run(7);
1.xml&?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:context=&http://www.springframework.org/schema/context&
xmlns:tx=&http://www.springframework.org/schema/tx&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd&&
&bean id=&cat& class=&cn.hncu.spring4x.aop.Cat&&&/bean&
&!-- 切点 --&
&bean id=&pointcut& class=&org.springframework.aop.support.JdkRegexpMethodPointcut&&
&property name=&pattern& value=&.*run.*&&&/property&
&!-- 通知 ,要自己写--&
&bean id=&advice& class=&cn.hncu.spring4x.aop.AroundAdvice&&&/bean&
&!-- 切面=切点+通知 --&
&bean id=&advisor& class=&org.springframework.aop.support.DefaultPointcutAdvisor&&
&property name=&advice& ref=&advice&&&/property&
&property name=&pointcut& ref=&pointcut&&&/property&
&bean id=&catProxide& class=&org.springframework.aop.framework.ProxyFactoryBean&&
&property name=&target& ref=&cat&&&/property&
&property name=&interceptorNames&&
&value&advisor&/value&
&/property&
&?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:context=&http://www.springframework.org/schema/context&
xmlns:tx=&http://www.springframework.org/schema/tx&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd&&
&bean id=&cat& class=&cn.hncu.spring4x.aop.Cat&&&/bean&
&!-- 切面=切点+通知 (把切点和通知写成内部bean)--&
&bean id=&advisor& class=&org.springframework.aop.support.DefaultPointcutAdvisor&&
&property name=&advice&&
&bean class=&cn.hncu.spring4x.aop.AroundAdvice&&&/bean&
&/property&
&property name=&pointcut&&
&bean class=&org.springframework.aop.support.JdkRegexpMethodPointcut&&
&property name=&patterns&&
&value&.*run.*&/value&
&value&.*say.*&/value&
&/property&
&/property&
&bean id=&catProxide& class=&org.springframework.aop.framework.ProxyFactoryBean&&
&property name=&target& ref=&cat&&&/property&
&property name=&interceptorNames&&
&value&advisor&/value&
&/property&
&?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:context=&http://www.springframework.org/schema/context&
xmlns:tx=&http://www.springframework.org/schema/tx&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd&&
&bean id=&cat& class=&cn.hncu.spring4x.aop.Cat&&&/bean&
&!--//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置 用到这个类 org.springframework.aop.support.RegexpMethodPointcutAdvisor --&
&bean id=&advisor&
class=&org.springframework.aop.support.RegexpMethodPointcutAdvisor&&
&property name=&advice&&
&bean class=&cn.hncu.spring4x.aop.AroundAdvice&&&/bean&
&/property&
&property name=&patterns&&
&value&.*run.*&/value&
&/property&
&bean id=&catProxide& class=&org.springframework.aop.framework.ProxyFactoryBean&&
&property name=&target& ref=&cat&&&/property&
&property name=&interceptorNames&&
&value&advisor&/value&
&/property&
&?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:context=&http://www.springframework.org/schema/context&
xmlns:tx=&http://www.springframework.org/schema/tx&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd&&
&bean id=&cat& class=&cn.hncu.spring4x.aop.Cat&&&/bean&
&!--//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置 用到这个类 org.springframework.aop.support.RegexpMethodPointcutAdvisor --&
&bean id=&advisor&
class=&org.springframework.aop.support.RegexpMethodPointcutAdvisor&&
&property name=&advice&&
&bean class=&cn.hncu.spring4x.aop.AroundAdvice&&&/bean&
&/property&
&property name=&patterns&&
&value&.*run.*&/value&
&/property&
&!-- 自动代理 --&
&bean class=&org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator&&&/bean&
&?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:context=&http://www.springframework.org/schema/context&
xmlns:tx=&http://www.springframework.org/schema/tx&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd&&
&bean id=&cat& class=&cn.hncu.spring4x.aop.Cat&&&/bean&
&!--//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置 用到这个类 org.springframework.aop.support.RegexpMethodPointcutAdvisor --&
&bean id=&advisor&
class=&org.springframework.aop.support.RegexpMethodPointcutAdvisor&&
&property name=&advice&&
&bean class=&cn.hncu.spring4x.aop.AroundAdvice&&&/bean&
&/property&
&property name=&patterns&&
&value&.*run.*&/value&
&/property&
&!-- 自动代理 --&
&bean class=&cn.hncu.spring4x.aop.MyAutoProxy&&&/bean&
第五种方法,模拟自动代理
MyAutoProxy.java
package cn.hncu.spring4x.
import org.springframework.aop.A
import org.springframework.aop.framework.ProxyFactoryB
import org.springframework.beans.BeansE
import org.springframework.beans.factory.config.BeanPostP
import org.springframework.context.ApplicationC
import org.springframework.context.ApplicationContextA
public class MyAutoProxy implements BeanPostProcessor,ApplicationContextAware{
private ApplicationContext applicationC
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext=applicationC//保证是同一个容器
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println(bean+&postProcessBeforeInitialization&);
//直接放行(一定要)
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println(bean+&postProcessAfterInitialization&);
ProxyFactoryBean factoryBean=new ProxyFactoryBean();
factoryBean.setTarget(bean);
Advisor advisor=applicationContext.getBean(&advisor&, Advisor.class);
factoryBean.addAdvisor(advisor);
return factoryBean.getObject();
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:38634次
积分:1825
积分:1825
排名:第16931名
原创:147篇
文章:15篇
阅读:5644
文章:10篇
阅读:4452
文章:12篇
阅读:2322
文章:21篇
阅读:12168
文章:18篇
阅读:1988
(8)(29)(18)(25)(1)(29)(11)(11)(5)(9)(5)

我要回帖

 

随机推荐