Junit中@Before與SpringAOP中@Before的區別

1.Junit中@Before

在Junit中用@Before標註在方法上,可讓測試方法在之前之前先執行@Before標註的方法。可以配合@@After一起使用

需要導入的包

import org.junit.Before;

方法標註如下

    @Before
    public void before(){
        //1.初始化Spring容器
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.獲取bean
        jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
    }

代碼示例:可參見:https://blog.csdn.net/qq_32224047/article/details/107143924 

2.SpringAOP中@Before

在SpringAOP中可以通過註解來標識前置通知

@Before("execution(* cn.tedu.service..*(..))")
等價於
<aop:before method="myBefore" pointcut="within(cn.tedu.service.UserServiceImpl)"/>

需要導入的包

package org.aspectj.lang.annotation;

標註的方法如下

    @Before("execution(* cn.tedu.service..*(..))")
    public void before(){
        System.out.println("###first前置通知..");
    }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章