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

<bean id="man" class="类所在路径">
		<property name="属性">
			<value type="java.lang.String">张三</value>
		</property>
	</bean>

	<bean id="fbi" class="类所在路径" />
	
	<bean id="civilian"
		class="org.springframework.aop.framework.ProxyFactoryBean">

		<property name="target">
			<ref bean="man" />
		</property>
		<property name="interceptorNames">
			<list>
				<value>fbi</value>
			</list>
		</property>
	</bean>	


 

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();

      }

 

}


运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值