Spring的AOP学习笔记

Spring的ProxyFactoryBean可以代理接口(蓝色),也可以代理不实现任何接口的类(红色),不过应该尽量代理接口,这样更松藕合。另外,在代理类时,应将spring的cglib JAR包导入工程中。

1、接口

package my;

public interface Pet
{
 public void showName();
 public void cry();
}

2、业务实现类

package my;

public class MyPet implements Pet
{
 private String name;

 public void showName()
 {
  System.out.println("My name is "+this.name);
 }
 public void cry()
 {
//  System.out.println("My name is "+this.name);
  System.out.println("Wang! Wang!");
 }
 
 public void setName(String name)
 {
  this.name = name;
 }

 public void hello()
 {
  System.out.println("Hello!!!!!!!!");
 }
 
}

3、通知类

package my;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyAdvice implements MethodBeforeAdvice
{
 public void before(Method arg0, Object[] arg1, Object arg2)
   throws Throwable
 {
   System.out.println("Hello world!");
 }
}

4、spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

 <beans>
 <bean id="myPet" class="my.MyPet" abstract="false" singleton="true"
  lazy-init="default" autowire="default" dependency-check="default">
  <property name="name">
   <value type="java.lang.String">dog</value>
  </property>
 </bean>
 <bean id="beforeAdvice" class="my.MyAdvice" abstract="false"
  singleton="true" lazy-init="default" autowire="default"
  dependency-check="default">
 </bean>
 <bean id="afterAdvice" class="my.MyAfterAdvice" abstract="false"
  singleton="true" lazy-init="default" autowire="default"
  dependency-check="default">
 </bean>
 <bean id="pet"
  class="org.springframework.aop.framework.ProxyFactoryBean"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">

<property name="proxyInterfaces">
   <value>my.Pet</value>
  </property>
  <property name="proxyTargetClass">
   <value>false</value>
  </property>

  <property name="proxyTargetClass">
   <value>true</value>
  </property>

  <property name="target">
   <ref bean="myPet"/>
  </property>
  <property name="interceptorNames">
   <list>
    <value>beforePointCutAdvisor</value>
   </list>
  </property>
 </bean>
 <bean id="beforePointCutAdvisor"
  class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">

<property name="mappedName">
   <value>showName</value>
  </property>

  <property name="mappedName">
   <value>hello</value>
  </property>

  <property name="advice">
   <ref bean="beforeAdvice" />
  </property>
 </bean>
</beans>

5、测试类

package my;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test
{

 /**
  * @param args
  */
 public static void main(String[] args)
 {
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "applicationContext.xml");
  
  MyPet myPet = (MyPet) context.getBean("pet");

  Pet myPet = (Pet) context.getBean("pet");

  myPet.cry();
  myPet.showName();
  myPet.hello();
 }

}

6、结果

Wang! Wang!
My name is dog
Hello world!
Hello!!!!!!!!

Wang! Wang!
Hello world!
My name is dog

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值