面向切面编程的实现手段--SpringAop

1.什么是面向切面编程?

springAop是面向切面编程的一种代表,通过对多模块下共同功能的统一管理,来控制业务逻辑与公有逻辑的解耦,而散布于应用多处共有的功能称为横切关注点,把这些横切关注点与业务逻辑相分离是面向切面编程需要解决的问题。

下面介绍通过xml文件的方式来实现springaop简单应用:

2.springaop应用

第一步,搭建项目环境,工程目录图如下:

添加依赖,spring-context包,aspectj包:

 <dependency>
	   <groupId>org.springframework</groupId>
	   <artifactId>spring-context</artifactId>
	   <version>4.3.9.RELEASE</version>
    </dependency>
    
    
      <dependency>
	   <groupId>org.aspectj</groupId>
	   <artifactId>aspectjrt</artifactId>
	   <version>1.8.8</version>
    </dependency>

    <dependency>
	   <groupId>org.aspectj</groupId>
	   <artifactId>aspectjweaver</artifactId>
	   <version>1.8.3</version>
    </dependency>

第二步: 新建一个接口

package com.hand.proxy.aop;

public interface EatInter {
    void eat();
}

并去实现它:

package com.hand.proxy.aop;

public class People implements EatInter {

	public void eat() {
		System.out.println("吃饭!");
	}

}

第三步,创建切面类

package com.hand.proxy.aop;


public class DoSomethingHelp {
	
	/**
	   * 切面类,指定公有的方执行的一些动作
	 */
	
	public void eatPoint() {
       System.out.println("切点");
	}
	
	public void beforeEat() {
	    System.out.println("吃饭之前,我们应该去洗手!");	
	}
	
	
	public void afterEat() {
		System.out.println("吃饭后,去睡觉!");
	}

}

第四步,添加applicationContext.xml文件,配置切面类和bean,通过xml文件的 方式来配置切面和通知类型:

其中创建的切点时,指定连接点,此处把接口中的eat()方法来作为连接点。

<aop:config>  标签用来配置aop

<aop:aspectj> 标签用来指定切面,其中ref属性就是用来引用切面类。

<aop:before> 和<aop:after> 标签表示通知的类型

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    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.0.xsd
        http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 
   
   <bean id="people" class="com.hand.proxy.aop.People" ></bean>
   <bean id='aspectJ' class="com.hand.proxy.aop.DoSomethingHelp"></bean>
   
   <!-- 需要添加此配置,将需要代理的类织入到切面中 -->
   <aop:aspectj-autoproxy proxy-target-class="true"/> 
   <aop:config>
     <aop:aspect ref="aspectJ">
        <aop:after method="afterEat" pointcut="execution(* com.hand.proxy.aop.EatInter.eat(..))"/>
        <aop:before method="beforeEat" pointcut="execution(* com.hand.proxy.aop.EatInter.eat(..))"></aop:before>
     </aop:aspect> 
   </aop:config>
   
</beans>

注:如果此处不添加如下标签 :

<aop:aspectj-autoproxy proxy-target-class="true"/>

会报错:

org.springframework.beans.factory.BeanNotOfRequiredTypeException:
 Bean named 'people' is expected to be of type 'com.hand.proxy.aop.People' but was actually of type 'com.sun.proxy.$Proxy6'

原因是:需要通过此配置将切点织入到目标的切面类中,默认的proxy-target-class为false。

测试案例:

package com.hand.proxy.aop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AopTest {
	
	
	@Test
	public void testAop(){
	ApplicationContext ac
		 =new ClassPathXmlApplicationContext("applicationContext.xml");
	People people=ac.getBean("people",People.class);
	people.eat();
	}

}

打印结果如下:

吃饭之前,我们应该去洗手!
吃饭!
吃饭后,去睡觉!
发布了53 篇原创文章 · 获赞 45 · 访问量 8824

猜你喜欢

转载自blog.csdn.net/qq_33036061/article/details/103134511