加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 湛江站长网 (https://www.0759zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Spring AOP深入剖析

发布时间:2016-11-10 13:12:22 所属栏目:教程 来源:站长网
导读:副标题#e# 一、通过代理工厂模式配置通知 ①、前置通知、后置通知: 定义某接口:ISomeService,并自定义方法 public interface ISomeService {public void tran() throws Exception; public void log();} 定义类 实现该接口,并重写方法: public class So

测试类:

public class Test01 {
@Test
public void proxyTest(){
	ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
	ISomeService ser=(ISomeService) ctx.getBean("some");
	ser.tran(); 
    ser.log();
	
}

 


 四、Spring的经典AOP配置方案、使用的是Aspectj第三方框架,实现了AOP思想。注解配置的AOP,纯POJO <aop:config>

具体步骤:
① 在项目中添加SpringAOP相关的JAR文件

②使用注解定义前置增强和后置增强实现日志功能

③编写Spring配置文件,织入注解定义的增强

④编写代码获取带有增强处理的业务对象

核心JAR包:

Spring AOP深入剖析

实现思路:

 1、定义接口实现类,并重写该方法

public interface ISomeService {
public void list();
}
public class SomeService implements ISomeService{

	public void list() {
		System.out.println("SomeService.list()");		
	}

}

2、通过注解实现增强,自定义类  

使用@Aspect注解将该类定义为切面,并且使用@Before注解将该方法定义为前置增强,增强定义完后,就可以在Spring配置文件中织入使用注解定义的增强了

@Aspect

public class MyAspectj {

@Before(value = "execution(* *..service.*.*(..))")	

public void MyBeforeAdvice(){
	System.out.println("==before==");
}
}

3、Spring配置文件

Spring AOP深入剖析

4、进行测试:

public class Test01 {
@Test
public void proxyTest(){
	ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
	ISomeService ser=(ISomeService) ctx.getBean("some");
	ser.list();
	
}

实现效果: 

Spring AOP深入剖析

 


 

※※※补充点:

(编辑:PHP编程网 - 湛江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!