spring aop实例

编程技术  /  houtizong 发布于 3年前   65

1.AdviceMethods.java

package com.bijian.study.spring.aop.schema;public class AdviceMethods {public void preGreeting() {System.out.println("--how are you!--");}}

2.beans.xml

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><aop:config proxy-target-class="true"><aop:aspect ref="adviceMethods"><!-- 配置指定的一个类 --><!--  <aop:before pointcut="target(com.baobaotao.NaiveWaiter) or target(com.baobaotao.NaughtyWaiter) and execution(* greetTo(..))" method="preGreeting"/>--><!-- 配置指定的多个类 --><!-- <aop:before pointcut="target(com.baobaotao.NaiveWaiter) or target(com.baobaotao.NaughtyWaiter) and execution(* greetTo(..))" method="preGreeting"/>--><!-- 配置指定的接口,此接口的所有实现类都被切面化了 --><aop:before pointcut="target(com.bijian.study.spring.aop.Waiter) and execution(* greetTo(..))" method="preGreeting"/></aop:aspect></aop:config>  <bean id="adviceMethods" class="com.bijian.study.spring.aop.schema.AdviceMethods"/><bean id="naiveWaiter" class="com.bijian.study.spring.aop.NaiveWaiter"/><bean id="naughtyWaiter" class="com.bijian.study.spring.aop.NaughtyWaiter"/></beans>

3.Waiter接口

package com.bijian.study.spring.aop;public interface Waiter {public void greetTo(String name);}

4.NaiveWaiter.java

package com.bijian.study.spring.aop;public class NaiveWaiter implements Waiter{@Overridepublic void greetTo(String name) {System.out.println("NavieWaiter:greet to " + name);}}

5.NaughtyWaiter.java

package com.bijian.study.spring.aop;public class NaughtyWaiter implements Waiter{@Overridepublic void greetTo(String name) {System.out.println("NaughtyWaiter:greet to " + name);}}

6.测试类Test.java

package com.bijian.study.spring.aop.schema;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bijian.study.spring.aop.Waiter;public class Test {public static void main(String[] args) {String configPath = "com/bijian/study/spring/aop/schema/beans.xml";ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);Waiter naiveWaiter = (Waiter)ctx.getBean("naiveWaiter");Waiter naughtyWaiter = (Waiter)ctx.getBean("naughtyWaiter");naiveWaiter.greetTo("John");naughtyWaiter.greetTo("Tom");}}

运行结果:

--how are you!--NavieWaiter:greet to John--how are you!--NaughtyWaiter:greet to Tom

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客