|
何为切面:个人理解为中断,也就是在原先的方法执行前或执行后插入一些事件。
先建立一个切面执行类: - package com.freestyle.test.beans;
- public class Minstrel {
- public void singBeforeQuest (){
- System.out.println( " BeforeQuest , GOGOGO!!");
- }
- public void singAfterQuest (){
- System.out.println( " AfterQuest , Well Done!!");
- }
- }
|
在xml里面加入切面配置:
现在我们测试一下:
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.freestyle.test.beans.Knight;
- public class BraveKnightTest {
- public static void main (String[] args){
- ClassPathXmlApplicationContext w_context= new ClassPathXmlApplicationContext( "spring-beans.xml");
- Knight w_knight=(Knight) w_context.getBean( "knight");
- w_knight.embarkOnQuest();
- Knight w_knight1=(Knight) w_context.getBean( "knight");
- w_knight1.embarkOnQuest();
- System.out.println(w_knight1==w_knight);
- }
- }
|
运行结果:
BraveKnight instance created :com.freestyle.test.beans.BraveKnight@ 44a7bdab
BeforeQuest , GOGOGO!!
SlayDragonQuest embark.
AfterQuest , Well Done!!
BeforeQuest , GOGOGO!!
SlayDragonQuest embark.
AfterQuest , Well Done!!
true |
----------------------------
原文链接:https://blog.csdn.net/rocklee/article/details/50461187
程序猿的技术大观园:www.javathinker.net
[这个贴子最后由 flybird 在 2020-02-04 23:59:44 重新编辑]
|
|