Spring与AOP:

  AOP的引入:

    主业务经常需要调用系统级业务(交叉业务),如果在主业务代码中大量的调用系统级业务代码,会使系统级业务与主业务深度耦合在一起,大大影响了主业务逻辑的可读性,降低了代码的可维护性,同时也增加了开发难度。

    所以,可以采用动态代理方式。动态代理是 OCP 开发原则的一个重要体现:在不修改主业务逻辑的前提下,扩展和增强其功能。

 package com.tongji.test;

 import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import org.junit.Test; import com.tongji.service.ISomeService;
import com.tongji.service.SomeServiceImpl;
import com.tongji.utils.SomeUtils; public class MyTest {
@Test
public void test01() {
final ISomeService target = new SomeServiceImpl();
ISomeService proxy = (ISomeService) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() { //织入:交叉业务逻辑切入到主业务中,就在这里完成的
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
SomeUtils.doTransaction();
Object result = method.invoke(target, args);
SomeUtils.doLog();
return result;
}
});
proxy.doSome();
proxy.doSecond();
}
}

  

  AOP简介:

    AOP(Aspect Orient Programming),面向切面编程,是面向对象编程 OOP 的一种补充。

    面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运行过程。AOP 底层,就是采用动态代理模式(代理模式见此链接)实现的。采用了两种代理:JDK 的动态代理,与 CGLIB的动态代理。
    面向切面编程,就是将交叉业务逻辑封装成切面,利用 AOP 容器的功能将切面织入到主业务逻辑中。所谓交叉业务逻辑是指,通用的、与主业务逻辑无关的代码,如安全检查、事务、日志等。
    若不使用 AOP,则会出现代码纠缠,即交叉业务逻辑与主业务逻辑混合在一起。这样,会使主业务逻辑变的混杂不清。
     例如,转账,在真正转账业务逻辑前后,需要权限控制、日志记录、加载事务、结束事务等交叉业务逻辑,而这些业务逻辑与主业务逻辑间并无直接关系。但,它们的代码量所占比重能达到总代码量的一半甚至还多。它们的存在,不仅产生了大量的“冗余”代码,
还大大干扰了主业务逻辑---转账。

    AOP编程术语:

      (1)切面(Aspect)
        切面泛指交叉业务逻辑。上例中的事务处理、日志处理就可以理解为切面。常用的切面有通知与顾问。实际就是对主业务逻辑的一种增强。
      (2)织入(Weaving)
        织入是指将切面代码插入到目标对象的过程。上例中 invoke()方法完成的工作,就可以称为织入。
      (3)切入点(Pointcut)
        切入点指切面具体织入的位置。在 StudentServiceImpl 类中,若 doSome()将被增强,而doOther()不被增强,则 doSome()为切入点,而 doOther()仅为连接点。被标记为 final 的方法是不能作为连接点与切入点的。因为最终的是不能被修的,不能被增强的。

      (4)目标对象(Target)
        目标对象指将要被增强的对象。即包含主业务逻辑的类的对象。上例中的StudentServiceImpl 的对象若被增强,则该类称为目标类,该类对象称为目标对象。当然,不被增强,也就无所谓目标不目标了。
      (5)通知(Advice)
        通知是切面的一种实现,可以完成简单织入功能(织入功能就是在这里完成的)。换个角度来说,通知定义了增强代码切入到目标代码的时间点,是目标方法执行之前执行,还是之后执行等。通知类型不同,切入时间不同。切入点定义切入的位置,通知定义切入的时间。  
      (6)顾问(Advisor)
        顾问是切面的另一种实现,能够将通知以更为复杂的方式织入到目标对象中,是将通知包装为更复杂切面的装配器。

  通知(Advice):

    通知(Advice),切面的一种实现,可以完成简单织入功能(织入功能就是在这里完成的)。常用通知有:前置通知、后置通知、环绕通知、异常处理通知。
    通知的用法步骤:

      (1)定义目标类
          定义目标类,就是定义之前的普通 Bean 类,也就是即将被增强的 Bean 类。
      (2)定义通知类
          通知类是指,实现了相应通知类型接口的类。当前,实现了这些接口,就要实现这些接口中的方法,而这些方法的执行,则是根据不同类型的通知,其执行时机不同。
          前置通知:在目标方法执行之前执行
          后置通知:在目标方法执行之后执行
          环绕通知:在目标方法执行之前与之后均执行
          异常处理通知:在目标方法执行过程中,若发生指定异常,则执行通知中的方法
      (3)注册目标类
          即在 Spring 配置文件中注册目标对象 Bean。

      (4)注册通知切面
          即在 Spring 配置文件中注册定义的通知对象 Bean。

      (5)注册代理工厂 Bean 类对象 ProxyFactoryBean

      (6)客户端访问动态代理对象
          客户端访问的是动态代理对象,而非原目标对象。因为代理对象可以将交叉业务逻辑按照通知类型,动态的织入到目标对象的执行中。

    通知详解:

      下面详解的共同代码:

      主业务接口(底层使用JDK的动态代理时使用,使用CGLIB动态代理时不用):

 package com.tongji.aop05;

 //主业务接口
public interface ISomeService {
public void doSome();
public String doSecond();
public void doThird();
}

      目标类:

 package com.tongji.aop05;

 //目标类
public class SomeServiceImpl implements ISomeService { @Override
public void doSome() {
System.out.println("执行doSome()方法");
} @Override
public String doSecond() {
System.out.println("执行doSecond()方法");
return "China";
} @Override
public void doThird() {
System.out.println("执行doThird()方法");
} }

     测试类:

 package com.tongji.aop06;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test
public void test01() {
String resource = "com/tongji/aop06/applicationContext.xml";
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
ISomeService service = (ISomeService) ac.getBean("serviceProxy");
service.doSome();
System.out.println("-------------------");
String second = service.doSecond();
System.out.println(second);
System.out.println("-------------------");
service.doThird();
} }

     (1) 前置通知 MethodBeforeAdvice
        定义前置通知,需要实现 MethodBeforeAdvice 接口。该接口中有一个方法 before(),会在目标方法执行之前执行。

        通知类:

 package com.tongji.aop01;

 import java.lang.reflect.Method;

 import org.springframework.aop.MethodBeforeAdvice;

 //前置通知
public class MyMethodBeforeAdvice implements MethodBeforeAdvice { //该方法在目标方法执行之前执行
//method:目标方法
//args:目标方法的参数列表
//target:目标对象
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("目标方法执行之前:对目标对象的增强代码就是写在这里的"); } }

        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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目标对象 -->
<bean id="someService" class="com.tongji.aop01.SomeServiceImpl"/>
<!-- 切面:前置通知 -->
<bean id="beforeAdvice" class="com.tongji.aop01.MyMethodBeforeAdvice"/> <!-- 代理对象的生成:注意这里的ProxyFactoryBefan不是代理类,而是代理工厂 -->
<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="someService"/>
<!-- <property name="targetName" value="someService"/> -->
<!-- 可以不写接口,因为默认会自动检测到目标类所实现的所有接口 -->
<!-- <property name="interfaces" value="com.tongji.aop01.ISomeService"/> -->
<property name="interceptorNames" value="beforeAdvice"/>
</bean>
</beans>

      (2) 后置通知 AfterRunningAdvice:

        定义后置通知,需要实现接口 AfterReturningAdvice。该接口中有一个方法 afterReturning ()。

        通知类:

 package com.tongji.aop02;

 import java.lang.reflect.Method;

 import org.springframework.aop.AfterReturningAdvice;

 //后置通知
public class MyAfterReturningAdvice implements AfterReturningAdvice { //returnValue:目标方法返回值,后置通知只能获取返回值不能修改返回值
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("目标方法执行之后,目标方法返回值为" + returnValue); } }

        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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目标对象 -->
<bean id="someService" class="com.tongji.aop02.SomeServiceImpl"/>
<!-- 切面:后置通知 -->
<bean id="afterAdvice" class="com.tongji.aop02.MyAfterReturningAdvice"/> <!-- 代理对象的生成 -->
<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="someService"/>
<property name="interceptorNames" value="afterAdvice"/>
</bean>
</beans>

      (3) 环绕通知 MethodInterceptor:

        定义环绕通知,需要实现 MethodInterceptor 接口。环绕通知,也叫方法拦截器,可以在目标方法调用之前及之后做处理,可以改变目标方法的返回值,也可以改变程序执行流程。

        通知类:

 package com.tongji.aop03;

 import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class MyMethodInterceptor implements MethodInterceptor { @Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("目标方法执行之前");
//调用目标方法
Object result = invocation.proceed();
System.out.println("目标方法执行之后");
if (result != null) {
result = ((String)result).toUpperCase();
}
return result;
} }

        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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目标对象 -->
<bean id="someService" class="com.tongji.aop03.SomeServiceImpl"/>
<!-- 切面:环绕通知 -->
<bean id="aroundAdvice" class="com.tongji.aop03.MyMethodInterceptor"/> <!-- 代理对象的生成 -->
<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="someService"/>
<property name="interceptorNames" value="aroundAdvice"/>
</bean>
</beans>

      (4) 异常通知 ThrowsAdvice:

        定义异常通知,需要实现 ThrowsAdvice 接口。该接口的主要作用是,在目标方法抛出异常后,根据异常的不同做出相应的处理。当该接口处理完异常后,会简单地将异常再次抛出给目标方法。
        不过,这个接口较为特殊,从形式上看,该接口中没有必须要实现的方法。但,这个接口却确实有必须要实现的方法 afterThrowing()。这个方法重载了四种形式。由于使用时,一般只使用其中一种,若要都定义到接口中,则势必要使程序员在使用时必须要实现这四个方法。这是很麻烦的。所以就将该接口定义为了标识接口(没有方法的接口)。使用方法时要参考接口源代码中的提示。

        自定义异常类(略)

        主业务接口:

 package com.tongji.aop04;

 //主业务接口
public interface ISomeService {
boolean check(String username, String password) throws UserException;
}

        目标类:

 package com.tongji.aop04;

 //目标类
public class SomeServiceImpl implements ISomeService { //目标方法
@Override
public boolean check(String username, String password) throws UserException {
if (!"qjj".equals(username)) {
throw new UsernameException("用户名不正确");
} if (!"248xiaohai".equals(password)) {
throw new PasswordException("密码不正确");
} return true;
} }

        通知类:

 package com.tongji.aop04;

 import org.springframework.aop.ThrowsAdvice;

 //异常处理通知
public class MyThrowsAdvice implements ThrowsAdvice { //若发生UsernameException异常,则该方法会自动被调用执行
public void afterThrowing(UsernameException ex) {
System.out.println("发生用户名异常,ex=" + ex);
} //若发生PasswordException异常,则该方法会自动被调用执行
public void afterThrowing(PasswordException ex) {
System.out.println("发生密码异常,ex=" + ex);
}
}

       这里的参数 ex 为,与具体业务相关的用户自定义的异常类对象。容器会根据异常类型的不同,自动选择不同的该方法执行。这些方法的执行是在目标方法执行结束后执行的。

       测试类:

 package com.tongji.aop04;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test
public void test01() {
String resource = "com/tongji/aop04/applicationContext.xml";
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
ISomeService service = (ISomeService) ac.getBean("serviceProxy");
try {
service.check("qjj1", "248");
} catch (UserException e) {
e.printStackTrace();
}
} }

      (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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目标对象 -->
<bean id="someService" class="com.tongji.aop11.SomeServiceImpl"/>
<!-- 切面:通知 -->
<bean id="afterAdvice" class="com.tongji.aop11.MyAfterReturningAdvice"/>
<bean id="beforeAdvice" class="com.tongji.aop11.MyMethodBeforeAdvice"/> <!-- 代理对象的生成 -->
<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="someService"/>
<property name="interceptorNames" value="afterAdvice,beforeAdvice"/>
</bean>
</beans>

      (6) 无主业务接口时,AOP底层使用CGLIB动态代理:

        目标类(无接口),其他代码均相同:

 package com.tongji.aop09;

 //目标类
public class SomeService { public void doSome() {
System.out.println("执行doSome()方法");
} public String doSecond() {
System.out.println("执行doSecond()方法");
return "China";
} }

     (7) 有主业务接口时,迫使AOP底层使用CGLIB动态代理:

        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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目标对象 -->
<bean id="someService" class="com.tongji.aop10.SomeServiceImpl"/>
<!-- 切面:后置通知 -->
<bean id="afterAdvice" class="com.tongji.aop10.MyAfterReturningAdvice"/> <!-- 代理对象的生成 -->
<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="someService"/>
<property name="interceptorNames" value="afterAdvice"/>
<!-- <property name="proxyTargetClass" value="true"/> -->
<property name="optimize" value="true"/>
</bean>
</beans>

    顾问(Advisor):

      通知(Advice)是 Spring 提供的一种切面(Aspect)织入方法。但其功能过于简单:只能将切面织入到目标类的所有目标方法中,无法完成将切面织入到指定目标方法中。
        顾问(Advisor)是 Spring 提供的另一种切面织入方法。其可以完成更为复杂的切面织入功能。PointcutAdvisor 是顾问的一种,可以指定具体的切入点。顾问将通知进行了包装,会根据不同的通知类型,在不同的时间点,将切面织入到不同的切入点。

      PointcutAdvisor 接口有两个较为常用的实现类:
        NameMatchMethodPointcutAdvisor 名称匹配方法切入点顾问
        RegexpMethodPointcutAdvisor 正则表达式匹配方法切入点顾问
      1. 名称匹配方法切入点顾问
        NameMatchMethodPointcutAdvisor,即名称匹配方法切入点顾问。容器可根据配置文件中指定的方法名来设置切入点。

        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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目标对象 -->
<bean id="someService" class="com.tongji.aop05.SomeServiceImpl"/>
<!-- 切面:后置通知 -->
<bean id="afterAdvice" class="com.tongji.aop05.MyAfterReturningAdvice"/>
<!-- 切面:名称匹配方法切入点顾问 -->
<bean id="afterAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="afterAdvice"/>
<!-- <property name="mappedNames" value="do*"/> -->
<property name="mappedNames" value="doSome,doThird"/>
<!-- <property name="mappedNames">
<array>
<value>doSome</value>
<value>doThird</value>
</array>
</property>
-->
</bean> <!-- 代理对象的生成 -->
<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="someService"/>
<property name="interceptorNames" value="afterAdvisor"/>
</bean>
</beans>

      2. 正则表达式方法切入点顾问
        RegexpMethodPointcutAdvisor,即正则表达式方法顾问。容器可根据正则表达式来设置切入点。注意,与正则表达式进行匹配的对象是接口中的方法名,而非目标类(接口的实现类)的方法名。

        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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目标对象 -->
<bean id="someService" class="com.tongji.aop06.SomeServiceImpl"/>
<!-- 切面:后置通知 -->
<bean id="afterAdvice" class="com.tongji.aop06.MyAfterReturningAdvice"/>
<!-- 切面:正则表达式方法切入点顾问 -->
<bean id="afterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="afterAdvice"/>
<!-- 正则表达式匹配的对象为:全限定性方法名,而不是简单方法名 -->
<!-- <property name="pattern" value=".*T.*"/> -->
<!-- <property name="patterns" value=".*T.*,.*Sec.*"/> -->
<property name="pattern" value=".*T.*|.*Sec.*"/>
</bean> <!-- 代理对象的生成 -->
<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="someService"/>
<property name="interceptorNames" value="afterAdvisor"/>
</bean>
</beans>

    自动代理生成器:

      前面代码中所使用的代理对象,均是由 ProxyFactoryBean 代理工具类生成的。而该代理工具类存在着如下缺点:
        (1)一个代理对象只能代理一个 Bean,即如果有两个 Bean 同时都要织入同一个切面,这时,不仅要配置这两个 Bean,即两个目标对象,同时还要配置两个代理对象。
        (2)在客户类中获取 Bean 时,使用的是代理类的 id,而非我们定义的目标对象 Bean 的 id。我们真正想要执行的应该是目标对象。从形式上看,不符合正常的逻辑。
      Spring 提供了自动代理生成器,用于解决 ProxyFactoryBean 的问题。常用的自动代理生成器有两个:  
        默认 advisor 自动代理生成器
        Bean 名称自动代理生成器
      需要注意的是,自动代理生成器均继承自 Bean 后处理器 BeanPostProcessor。容器中所有 Bean 在初始化时均会自动执行 Bean 后处理器中的方法,故其无需 id 属性。所以自动代理生成器的 Bean 也没有 id 属性,客户类直接使用目标对象 bean 的 id。

     1. 默认 advisor 自动代理生成器
       DefaultAdvisorAutoProxyCreator 代理的生成方式是,将所有的目标对象与 Advisor 自动结合,生成代理对象。无需给生成器做任何的注入配置。注意,只能与 Advisor 配合使用。
       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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目标对象 -->
<bean id="someService1" class="com.tongji.aop07.SomeServiceImpl"/>
<bean id="someService2" class="com.tongji.aop07.SomeServiceImpl"/>
<!-- 切面:通知 -->
<bean id="afterAdvice" class="com.tongji.aop07.MyAfterReturningAdvice"/>
<bean id="beforeAdvice" class="com.tongji.aop07.MyMethodBeforeAdvice"/>
<!-- 切面:名称匹配方法切入点顾问 -->
<bean id="afterAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="afterAdvice"/>
<property name="mappedName" value="doSome"/>
</bean>
<bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="beforeAdvice"/>
<property name="mappedName" value="doSecond"/>
</bean> <!-- 自动代理生成器 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
</beans>

       测试类:

 package com.tongji.aop07;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test
public void test01() {
String resource = "com/tongji/aop07/applicationContext.xml";
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
ISomeService service1 = (ISomeService) ac.getBean("someService1");
service1.doSome();
service1.doSecond();
service1.doThird();
System.out.println("-------------------");
ISomeService service2 = (ISomeService) ac.getBean("someService2");
service2.doSome();
service2.doSecond();
service2.doThird();
} }

      2. Bean 名称自动代理生成器
        DefaultAdvisorAutoProxyCreator 会为每一个目标对象织入所有匹配的 Advisor,不具有选择性。而 BeanNameAutoProxyCreator 的代理生成方式是,根据 bean 的 id,来为符合相应名称的类生成相应代理对象。

        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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目标对象 -->
<bean id="someService1" class="com.tongji.aop08.SomeServiceImpl"/>
<bean id="someService2" class="com.tongji.aop08.SomeServiceImpl"/>
<!-- 切面:通知 -->
<bean id="afterAdvice" class="com.tongji.aop08.MyAfterReturningAdvice"/>
<bean id="beforeAdvice" class="com.tongji.aop08.MyMethodBeforeAdvice"/>
<!-- 切面:名称匹配方法切入点顾问 -->
<bean id="afterAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="afterAdvice"/>
<property name="mappedName" value="doSome"/>
</bean>
<bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="beforeAdvice"/>
<property name="mappedName" value="doSecond"/>
</bean> <!-- 该自动代理生成器,不仅可以指定目标对象,还可以指定切面,并且切面可以是通知或顾问 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="someService1"/>
<property name="interceptorNames" value="beforeAdvice"/>
</bean>
</beans>

Spring4笔记6--Spring与AOP的更多相关文章

  1. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

  2. Spring中AOP原理,源码学习笔记

    一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...

  3. Spring学习笔记(二)Spring基础AOP、IOC

    Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...

  4. spring学习笔记二 注解及AOP

    本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...

  5. Spring学习笔记5——注解方式AOP

    第一步:注解配置业务类 使用@Component("Pservice")注解ProductService 类 package com.spring.service; import ...

  6. 【学习笔记】Spring AOP注解使用总结

    Spring AOP基本概念 是一种动态编译期增强性AOP的实现 与IOC进行整合,不是全面的切面框架 与动态代理相辅相成 有两种实现:基于jdk动态代理.cglib Spring AOP与Aspec ...

  7. Spring4笔记7--AspectJ 对 AOP 的实现

    AspectJ 对 AOP 的实现: 对于 AOP 这种编程思想,很多框架都进行了实现.Spring 就是其中之一,可以完成面向切面编程.然而,AspectJ 也实现了 AOP 的功能,且其实现方式更 ...

  8. Spring学习笔记(12)——aop

    先了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个"时 ...

  9. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理

    通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...

随机推荐

  1. C# 源码计数器

    设计背景 编程工作中,有些文档需要填写代码量,例如申请软件著作权.查阅相关资料之后,编写了这个小程序. 设计思路 主要思路为分析项目文件,根据项目文件查找代码文件,然后遍历代码文件进行分析 相关技术 ...

  2. DB磁盘满导致Zabbix Server Crash一例

    故障描述 今天线上zabbix出现几次数据中断的情况,经排查为DB服务器磁盘空间不足导致的.还好我们目前我们zabbix,falcon两套监控系统并存,哈哈. 故障排查过程没什么技术含量,简单的将故障 ...

  3. jmeter函数

    1.常用JMeter函数 1)__regexFunction 正则表达式函数可以使用正则表达式(用户提供的)来解析前面的服务器响应(或者是某个变量值).函数会返回一个有模板的字符串,其中携带有可变的值 ...

  4. c# base64算法解密

    /// <summary> /// 将字符串使用base64算法加密 /// </summary> /// <param name="code_type&quo ...

  5. 【BZOJ4591】【Shoi2015】超能粒子炮

    Description 传送门 Solution ​ 记\(a=\lfloor\frac n p\rfloor\),\(b=n\%p\).我们尝试使用Lucas定理展开这些组合数,寻找公共部分.以下除 ...

  6. BZOJ2800 [Poi2012]Leveling Ground 【扩展欧几里得 + 三分 + 堆】

    题目链接 BZOJ2800 题解 区间加极难操作,差分之后可转化为两点一加一减 那么现在问题就将每个点暂时独立开来 先判定每个点是否被\((A,B)\)整除,否则无解 之后我们先将\(A,B\)化为互 ...

  7. 利用java实现可远程执行linux命令的小工具

    在linux的脚本中,如果不对机器做其他的处理,不能实现在linux的机器上执行命令.为了解决这个问题,写了个小工具来解决这个问题. 后面的代码是利用java实现的可远程执行linux命令的小工具,代 ...

  8. 【bzoj2034】 2009国家集训队—最大收益

    http://www.lydsy.com/JudgeOnline/problem.php?id=2034 (题目链接) 题意 n个任务,每个任务只需要一个时刻就可以完成,完成后获得${W_i}$的收益 ...

  9. VS生成事件执行XCOPY时出现Invalid num of parameters的解决方案

    最近想偷懒 想把项目生成的dll全部自动汇集到一个文件夹下 于是乎就动用了VS的生成后事件 在执行Xcopy的时候碰到了点问题 Invalid number of parameters 挺奇怪的,在公 ...

  10. 《Java程序猿面试宝典》之字符串

    前不久刚看完这一章,然而这遗忘速度实在是不能忍,既然总是遗忘,那么老衲就和你磨上一磨. 1.字符串基础 先说字符串吧,看例1: String a = "abc"; String b ...