AOP(Aspect Oriented Program):面向切面编程。思想是:把功能分为核心业务功能和周边功能。
所谓核心业务功能:比如登录,增删改数据都叫做核心业务
周边功能:比如性能统计,日志,事务管理等。
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发。然后再把他们两个"编织"在一起,就叫做AOP。
为了支持AOP,需要用到一些额外的jar包。
2.1、功能分两大类:辅助功能和核心业务功能
2.2、辅助功能和核心业务功能彼此独立开发
2.3、比如说登录功能,就是没有性能统计和日志输出功能,也可以正常运行的
2.4、如果有需要的话,就可以把日志输出功能和登录功能编织在一起,这样登录的时候可以查看到日志输出。
2.5、辅助功能:也叫做切面功能,能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想叫做面向切面编程。
package com.demo.service;
public class ProductService{
public void doService(){
System.out.println("doService");
}
}
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
}
}
日志切面的功能:在调用核心功能之前和之后分别打印日志,所谓切面就是那些辅助功能。
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;
}
}
execution(* com.demo.service.ProductService.*(..))表示进行切面操作:
*:表示返回任意类型
com.demo.service.ProductService.*:表示包名以com.demo.service.ProductService开头的类的任意方法。
(..):表示参数是任意数量和类型。
指定核心功能:
具体的applicationContext.xml配置如下:
<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>
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
}
}
使用@Component("service")注解ProductService类,表明这个类是业务类
package com.demo.service;
import org.springframework.stereotype.Component;
@Component("service")
public class ProductService{
public void doService(){
System.out.println("doService");
}
}
@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;
}
}
去掉原来的bean的配置,添加如下:
applicationContext.xml具体配置如下:
<context:component-scan base-package="com.demo.aspect"/>
<context:component-scan base-package="com.demo.service"/>
<aop:aspectj-autoproxy/>
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
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章