Spring AOP实例

Spring AOP实例 - 398198920 - 冰冻三尺非一日之寒

ProxyFactoryBeanTest.java

package com.studyspring.ch3;

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

public class ProxyFactoryBeanTest {

 public static void main(String[] args) {
  ApplicationContext context=new FileSystemXmlApplicationContext("/src/applicationContext.xml");
  MultipleMethod target=(MultipleMethod)context.getBean("person");
  target.getTitle();
  target.getName();
  target.getAge();
 }

}

applicationContext.xml

<?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="advice" class="com.studyspring.ch3.BeforeAdvicetest"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
 </bean>
 
 <bean id="advisor"
  class="org.springframework.aop.support.DefaultPointcutAdvisor"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
  <property name="advice">
   <ref local="advice" />
  </property>
  <property name="pointcut">
   <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    <property name="pattern">
     <value>.*get.*</value>
    </property>
   </bean>
  </property>
 </bean>
 
 <bean id="person"
  class="org.springframework.aop.framework.ProxyFactoryBean"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
  <property name="target">
   <bean class="com.studyspring.ch3.MultipleMethod">
    <property name="name"><value>Ray</value></property>
    <property name="age"><value>21</value></property>
    <property name="title"><value>doctor</value></property>
   </bean>
  </property>
  <property name="interceptorNames">
   <list>
    <value>advisor</value>
   </list>
  </property>
 </bean></beans>

MultipleMethod.java

package com.studyspring.ch3;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MultipleMethod {
 private Log log=LogFactory.getLog(MultipleMethod.class);
 private String name;
 private int age;
 private String title;
 public int getAge() {
  log.info("我们处理get方法");
  System.out.println("我们处理get方法");
  return age;
 }
 public void setAge(int age) {
  log.info("我们处理set方法");
  System.out.println("我们处理set方法");
  this.age = age;
 }
 public String getName() {
  log.info("我们处理get方法");
  System.out.println("我们处理get方法");
  return name;
 }
 public void setName(String name) {
  log.info("我们处理set方法");
  System.out.println("我们处理set方法");
  this.name = name;
 }
 public String getTitle() {
  log.info("我们处理get方法");
  System.out.println("我们处理get方法");
  return title;
 }
 public void setTitle(String title) {
  log.info("我们处理set方法");
  System.out.println("我们处理set方法");
  this.title = title;
 }
}

 

BeforeAdvicetest .java

 

package com.studyspring.ch3;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
public class BeforeAdvicetest implements MethodBeforeAdvice{

 /**
  * @param args
  */
 private Log log=LogFactory.getLog(BeforeAdvicetest.class); 

 public void before(Method method, Object[] arg, Object target) throws Throwable {
  log.info("Now we use before advice");
  System.out.println("Now we use before advice");
 }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值