SSM(4)-AOP-自定义类来实现Aop

承接上篇,第二种方式,用自定义类来实现
1.自定义一个JAVA类(此篇是在前篇的基础上做的差异性描述)
2.配置beans.xml
3.写个测试类



1.写个JAVA类

public class myPointcut {

   public void before(){
       System.out.println("---------方法执行前---------");
  }
   public void after(){
       System.out.println("---------方法执行后---------");
  }
   
}


2.配置beans.xml
 

<!--注册bean-->
<bean id="diy" class="com.config.myPointcut"/>

<!--aop的配置-->
<aop:config>
   <!--第二种方式:使用AOP的标签实现-->
   <aop:aspect ref="diy">
       <aop:pointcut id="myPonitcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
       <aop:before pointcut-ref="myPonitcut" method="before"/>
       <aop:after pointcut-ref="myPonitcut" method="after"/>
   </aop:aspect>
</aop:config>


3.写个测试类
 

public class MyTest {
   @Test
   public void test(){
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       UserService userService = (UserService) context.getBean("userService");
       userService.add();
  }
}



​​​​​​​

posted @ 2020-08-05 21:08  jasmineTang  阅读(99)  评论(0)    收藏  举报