spring添加通知配置

 

在项目里添加的spring配置文件

 

 

<bean id="beforeMethod" class="com.wxw.core.common.AdviceBefore"></bean>
   
    <bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
        <property name="pattern" value=".*save"></property>
    </bean>
   
    <bean id="defaultBeand" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="beforeMethod"></property>
        <property name="pattern" value=".*save"></property>
    </bean>

 

AdviceBefore.java

package com.wxw.core.common;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class AdviceBefore implements MethodBeforeAdvice,AfterReturningAdvice {

    @Override
    public void before(Method arg0, Object[] arg1, Object arg2)
            throws Throwable {
        System.out.println("开始.我了个去");
       
    }

    @Override
    public void afterReturning(Object returnValue, Method method,
            Object[] args, Object target) throws Throwable {
        System.out.println("结束.我了个去");
       
    }

}

 

 

 

测试类的方法:


@Test
public void saveStudent() throws Exception{
   
     ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
     StudentService studentService = (StudentService) context.getBean("studentServiceImpl");
     Student stu = new Student();
     stu.setStuName("小明");
     studentService.saveStudent(stu);
}

测试结果:

image

总结:最近一直在研究spring的一些特性,以前一直都是直接在配置文件里把别人配置好的代码直接复制过来,也没有深入研究过,导制很多问题都是不网上查一查解决,但是很多问题又很难理解其本质。自己配置并简单实现一下,能够理解其中的一些原理。这样更深刻的记住一些特性。比如,通过配置前后通知,就更容易理解spring能过配置,能够很容易的扩展spring提供的特性,并应用到自己项目里,增加了代码的可扩展性,也易于维护,通过对一步步来学习spring的一些特性,以后自己项目里添加spring便游刃有余。最后一点,实践最重要

转载于:https://www.cnblogs.com/wxwall/p/3194598.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值