十八、Spring框架(AOP)
阅读原文时间:2024年07月03日阅读:1

一、AOP(基于XML方式配置AOP)

AOP(Aspect Oriented Program):面向切面编程。思想是:把功能分为核心业务功能和周边功能。

所谓核心业务功能:比如登录,增删改数据都叫做核心业务

周边功能:比如性能统计,日志,事务管理等。

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发。然后再把他们两个"编织"在一起,就叫做AOP。

1、首先是jar包导入

为了支持AOP,需要用到一些额外的jar包。

2、开发思路图

  2.1、功能分两大类:辅助功能和核心业务功能

  2.2、辅助功能和核心业务功能彼此独立开发

  2.3、比如说登录功能,就是没有性能统计和日志输出功能,也可以正常运行的

  2.4、如果有需要的话,就可以把日志输出功能和登录功能编织在一起,这样登录的时候可以查看到日志输出。

  2.5、辅助功能:也叫做切面功能,能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想叫做面向切面编程。

3、准备业务类ProductService

package com.demo.service;

public class ProductService{
public void doService(){
System.out.println("doService");
}
}

4、TestSpring测试

package com.demo.test;

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

import com.demo.service.ProductService;

public class TestSpring{
public static void main(String[] args){
//加载applicationContext.xml配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});。
ProductService service=(ProductService)context.getBean("service");//获取配置文件的ProductService属性name
service.doService();//doService
}
}

5、准备日志切面LoggerAspect

日志切面的功能:在调用核心功能之前和之后分别打印日志,所谓切面就是那些辅助功能。

package com.demo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class LoggerAspect{
public Object log(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("start log:"+joinPoint.getSignature().getName());
//将来与某个核心功能编织之后,用来执行核心功能的代码
Object object=joinPoint.proceed();//编织

    System.out.println("end log:"+joinPoint.getSignature().getName());  
    return object;  
}  

}

6、配置applicationContext.xml

配置ProductService业务对象。

配置日志切面

声明日志切面

execution(* com.demo.service.ProductService.*(..))表示进行切面操作:

*:表示返回任意类型

com.demo.service.ProductService.*:表示包名以com.demo.service.ProductService开头的类的任意方法。

(..):表示参数是任意数量和类型。

指定核心功能:

具体的applicationContext.xml配置如下:


http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean name="category" class="com.demo.pojo.Category">  
    <property name="name" value="Apple"/>  
</bean>

<bean name="product" class="com.demo.pojo.Product">  
    <property name="name" value="Fruit"/>  
    <prpperty name="category" ref="category"/>  
</bean>  
<!--配置ProductService核心业务功能-->  
<bean name="service" class="com.demo.service.ProductService"></bean>  
<!--配置切面功能-->  
<bean id="loggerAspect" class="com.demo.aspect.LoggerAspect"></bean>

<aop:config>  
    <aop:pointcut id="loggerCutpoint" expression="(\* com.demo.service.ProductService.\*(..))"/>  
    <!--将切面功能编织在ProductService核心业务功能中-->  
    <aop:aspect id="logAspect" ref="loggerAspect">  
        <aop:around pointcut-ref="loggerCutpoint" method="log"/>  
    </aop:aspect>  
</aop:config>  

7、测试切面功能编织到核心业务功能后

TestSpring:

package com.demo.test;

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

import com.demo.service.ProductService;

public class TestSpring{
public static void main(String[] args){
//加载applicationContext.xml配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
//根据ApplicationContext对象提供的getBean()方法,获取配置bean的name属性值。
ProductService service=(ProductService)context.getBean("service");
service.doService();//doService
}
}

二、AOP(基于注解方式AOP)

1、注解配置业务类

使用@Component("service")注解ProductService类,表明这个类是业务类

package com.demo.service;

import org.springframework.stereotype.Component;

@Component("service")
public class ProductService{
public void doService(){
System.out.println("doService");
}
}

2、注解配置切面

@Aspect注解:表示这是一个切面

@Component注解:表示这是一个bean,由Spring进行管理

@Around(value="execution(* com.demo.service.ProductService.*(..))")注解:表示对com.demo.service.ProductService这个类中的所有方法进行切面操作。

  2.1、编写LoggerAspect(日志记录类)切面

package com.demo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Compoent;

@Aspect
@Component
publc class LoggerAspect{
@Around(value="execute(* com.demo.service.ProductService.*(..))")
publc Object log(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("start log:"+joinPoint.getSignature().getName());

    Object object=joinPoint.proceed();

    System.out.println("end log:"+joinPoint.getSignature().getName());

    return object;  
}  

}

3、修改applicationContext.xml配置文件

去掉原来的bean的配置,添加如下:





applicationContext.xml具体配置如下:


http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="com.demo.aspect"/>  
 <context:component-scan base-package="com.demo.service"/>  
 <aop:aspectj-autoproxy/>  

4、TestSpring测试

package com.demo.test;

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

import com.demo.service.ProductService;

public class TestSpring{
public static void main(String[] args){
//加载applicationContext.xml配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
//根据ApplicationContext对象提供的getBean()方法,通过在ProductService业务类的Component("service")注解获取到业务对象。
ProductService service=(ProductService)context.getBean("service");
service.doService();//doService
}
}