Spring_aop切面编程

1. 先建一个Man类,此具有一个name属性,并且具有两个方法(行为)qq,mm

package MAN;
* 具有聊QQ和泡MM两个行为	的人的对象。还有一个用户名的属性
public class Man {
private String name;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public void qq(){
	System.out.println("QQ");
}
public void mm(){
	System.out.println("我在泡MM");
}
}


 

2.建一个FBI

继承MethodBeforeAdvice借口

Public class FBI implements MethodBeforeAdvice(){

publicvoid before(Method method, Object[] arg1, Object target)

                 throws Throwable {

           // TODO Auto-generated method stub

      Man man = (Man)target;

      

      System.err.println("FBI发现"+man.getName()+"即将进行"+method.getName()

                +"活动");

      }

 

}


 

3.配置applicationContext.xml


		
			张三
		
	

	
	
	

		
			
		
		
			
				fbi
			
		
		


 

4.编写test

package MAN;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import MAN.*;

publicclass AOPTest {

      publicstaticvoid main(String[] args) {

           ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

           Man man=(Man)ctx.getBean("civilian");

           man.qq();

           man.mm();

      }

 

}


运行结果:

你可能感兴趣的:(Spring,spring,aop,编程,string,class,object)