Spring10----AOP实现HelloWorld

一. 准备环境

1 org.springframework.aop-3.0.5.RELEASE.jar
2 com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
3 com.springsource.org.aopalliance-1.0.0.jar
4 com.springsource.net.sf.cglib-2.2.0.jar
View Code

二. 定义目标类

1. 定义目标接口

1 package com.test.spring.service;
2 
3 public interface IHelloWorldService {
4     public void sayHello();
5 }
View Code

2. 定义目标实现

 1 package com.test.spring.service.impl;
 2 
 3 import com.test.spring.service.IHelloWorldService;
 4 
 5 public class HelloWorldService implements IHelloWorldService{
 6 
 7     public void sayHello() {
 8         System.out.println("======Hello World!");
 9     }
10 }
View Code

:在日常开发中最后将业务逻辑定义在一个专门的service包下,而实现定义在service包下的impl包中,服务接 口以IXXXService形式,而服务实现就是XXXService,这就是规约设计,见名知义。当然可以使用公司内部更好的形 式,只要大家都好理解就可以了

三.定义切面支持类

有了目标类,该定义切面了,切面就是通知和切入点的组合,而切面是通过配置方式定义的,因此这定义切面前, 我们需要定义切面支持类,切面支持类提供了通知实现:

 1 package com.test.spring.aop;
 2 
 3 public class HelloWorldAspect {
 4     //前置通知
 5     public void beforeAdvice() {
 6         System.out.println("=====before advice");
 7     }
 8     //后置最终通知
 9     public void afterFinallyAdvice() {
10         System.out.println("=====after finally advice");
11     }
12 
13 }
View Code

此处HelloWorldAspect类不是真正的切面实现,只是定义了通知实现的类,在此我们可以把它看作就是缺少了切 入点的切面。
:对于AOP相关类最后专门放到一个包下,如“aop”包,因为AOP是动态织入的,所以如果某个目标类被AOP 拦截了并应用了通知,可能很难发现这个通知实现在哪个包里,因此推荐使用规约命名,方便以后维护人员查找相应的 AOP实现。

四. 在xml种进行配置

1)首先配置AOP需要aop命名空间:

1 <beans  xmlns="http://www.springframework.org/schema/beans"  
2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
3         xmlns:aop="http://www.springframework.org/schema/aop"  
4         xsi:schemaLocation="  
5            http://www.springframework.org/schema/beans  
6            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
7            http://www.springframework.org/schema/aop  
8            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
9 </beans>
View Code

2)配置目标类

1 <bean id="helloWorldService"
2         class="com.test.spring.service.impl.HelloWorldService" />
View Code
note:目标类具体的class应该是个实现类的全路径,否则无法实例化

3)配置切面

<bean id="aspect" class="com.test.spring.aop.HelloWorldAspect" />

    <aop:config>
        <aop:pointcut id="pointcut"
            expression="execution(* com.test.spring.service.impl..*.*(..))" />   //”表示匹配com.test.spring.service包及子包下的任何方法执行
        <aop:aspect ref="aspect">
            <aop:before pointcut-ref="pointcut" method="beforeAdvice" />
            <!-- <aop:after pointcut="execution(* com.test.spring.service.impl..*.*(..))" method="afterFinallyAdvice"/> -->
            <aop:after pointcut-ref="pointcut"
                method="afterFinallyAdvice" />  //这一句和上面注释的部分效果是等价的
        </aop:aspect>
    </aop:config>

四. 运行测试

1 public class AopTest {
2     @Test
3     public void test() {
4         ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
5         IHelloWorldService helloWorldService=(IHelloWorldService) context.getBean("helloWorldService");
6         helloWorldService.sayHello();
7     }
8 }
View Code
1 =====before advice
2 ======Hello World!
3 =====after finally advice
View Code

说明:调用被代理Bean跟调用普通Bean完全一样,Spring AOP将为目标对象创建AOP代理

       从输出种我们可以看出::前置通知在切入点选择的连接点(方法)之前允许,而后置通知将在连接点(方法)之后执 行,具体生成AOP代理及执行过程如图所示:

 五. 动态代理的表现

上面的测试部分,如果把接口IHelloWorld改成HelloWorldService的话,就会抛出异常:

public class Test {
    public static void main(String args[]) {
        ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
        IHelloWorld helloWorld=context.getBean("target",HelloWorldService.class);//这两个只要其中一个为类,都会抛出异常
        helloWorld.sayHello();
    }

}
Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'target' must be of type [com.test.spring.service.impl.HelloWorldService], but was actually of type [com.sun.proxy.$Proxy0]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:360)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1102)
    at helloworld.Test.main(Test.java:19)

说明:这种错误一般出在AOP面向切面编程中,spring面向切面的代理有两种,一种是jdk动态代理,一种是cglib代理;这是你在使用的的使用如果混合时候就会出现上面的错;这两种代理的区别是前者是接口代理,就是返回一个接口类型对象,而后者是类代理,不能返回接口类型对象只能返回类类型对象,如果返回接口了同样会出这样的错。

参考文献:

https://jinnianshilongnian.iteye.com/blog/1418597

原文地址:https://www.cnblogs.com/Hermioner/p/10201033.html