67,511
社区成员
发帖
与我相关
我的任务
分享
public class Boy {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void qq()
{
System.out.println("正在聊QQ");
}
}
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class Badivce implements MethodBeforeAdvice {
public void before(Method method, Object[] arg1, Object target) throws Throwable {
Boy boy=(Boy)target;
System.out.println(boy.getName()+"正在"+method.getName());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="boy" class="Boy" />
<bean id="badivce" class="Badivce" />
<bean id="boytarget" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="boy" />
</property>
<property name="interceptorNames">
<list>
<value>badivce</value>
</list>
</property>
</bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class textaop {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
Boy boy=new Boy();
boy.setName("sss");
boy=(Boy)ctx.getBean("boytarget");
boy.qq();
System.out.println(boy.getName());
}
}