2019独角兽企业重金招聘Python工程师标准>>>

一、eclipse新建java项目取名SpringTest

二、导入sping包到构建路径

还需要aspectjweaver.jar

三、创建java类(当然先要创建各种包)

IHelloService.java

package com.zjptcc.wxw.spring.hello;public interface IHelloService {public void sayHello();public void sayChinaHello();}

HelloServiceImpl.java

package com.zjptcc.wxw.spring.hello;public class HelloServiceImpl implements IHelloService {private String Hello;private String ChinaHello;@Overridepublic void sayHello() {// TODO 自动生成的方法存根System.out.println(Hello);}@Overridepublic void sayChinaHello() {// TODO 自动生成的方法存根System.out.println(ChinaHello);}public String getHello() {return Hello;}public void setHello(String hello) {Hello = hello;}public String getChinaHello() {return ChinaHello;}public void setChinaHello(String chinaHello) {ChinaHello = chinaHello;}}

SimpleHelloBean.java

package com.zjptcc.wxw.spring.hello;public class SimpleHelloBean {public void sayHello(){System.out.println("Hello World!");}
}

AopTest.java

package com.zjptcc.wxw.spring.hello;import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;@Aspect
public class AopTest {/*Pointcut for sayHello*/@Pointcut("execution(* *.sayHello())")public void hellopoint() {}@Before("hellopoint()")public void beforehello() {System.out.println("接下去调用sayHello()......");}@AfterReturning("hellopoint()")public void afterhello() {System.out.println("函数sayHello()执行结束......");}/*Pointcut for sayChinaHello*/@Pointcut("execution(* *.sayChinaHello())")public void helloChinapoint() {}@Before("helloChinapoint()")public void beforehelloChina() {System.out.println("接下去调用sayChinaHello()......");}@AfterReturning("helloChinapoint()")public void afterhelloChina() {System.out.println("函数sayChinaHello()执行结束......");}
}

四、配置文件

在src目录下创建resources目录,并在其下建立hello-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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><context:annotation-config/> <aop:aspectj-autoproxy /><bean id="helloService" class="com.zjptcc.wxw.spring.hello.HelloServiceImpl"><property name="Hello"><value>Hello,china!</value></property><property name="ChinaHello"><value>你好,中国欢迎您!</value></property></bean><bean id="SimpleHelloBean" class="com.zjptcc.wxw.spring.hello.SimpleHelloBean"></bean><bean id="aopTest" class="com.zjptcc.wxw.spring.hello.AopTest" /></beans>

五、测试

测试程序TestHelloService.java如下:

package com.zjptcc.wxw.spring.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zjptcc.wxw.spring.hello.IHelloService;
import com.zjptcc.wxw.spring.hello.SimpleHelloBean;public class TestHelloService {/*** @param args*/private static ApplicationContext ctx;public static void main(String[] args) {// TODO 自动生成的方法存根ctx = new ClassPathXmlApplicationContext("resources/hello-beans.xml");//用接口IHelloService helloWorld = (IHelloService) ctx.getBean("helloService");helloWorld.sayHello();helloWorld.sayChinaHello();System.out.println("------------------------------------------------------------------------------------");//用类SimpleHelloBean SimpleHello = (SimpleHelloBean) ctx.getBean("SimpleHelloBean");SimpleHello.sayHello();}}

运行结果如下:

六、利用配置文件实现AOP

java类

package com.zjptcc.wxw.spring.person;
public class PersonService {private String name;public void setName(String name) {this.name = name;}public void info(){System.out.println("此人姓名为:"+name);}public String getName(){return name;}
}
package com.zjptcc.wxw.spring.person;public class PersonAop {public void before_info() {System.out.println("接下去,调用info()显示人名......");}public void after_info() {System.out.println("调用info()结束......");}public void after_get() {System.out.println("调用getName()结束......");}}

resources/person-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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><context:annotation-config /><aop:aspectj-autoproxy /><bean id="personService" class="com.zjptcc.wxw.spring.person.PersonService"><property name="name"><value>露茜</value></property></bean><!--定义切面 --><bean id="personaop" class="com.zjptcc.wxw.spring.person.PersonAop" /><aop:config><aop:aspect ref="personaop"><aop:pointcut id="infopoint" expression="execution(* *.info(..))" /><aop:before pointcut-ref="infopoint" method="before_info" /><aop:after pointcut-ref="infopoint" method="after_info" /></aop:aspect><aop:aspect ref="personaop"><aop:pointcut id="getpoint" expression="execution(* *.getName(..))" /><aop:after pointcut-ref="getpoint" method="after_get" /></aop:aspect></aop:config></beans>

测试

package com.zjptcc.wxw.spring.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zjptcc.wxw.spring.person.PersonService;public class TestPersonService {/*** @param args*/private static ApplicationContext ctx;public static void main(String[] args) {// TODO 自动生成的方法存根ctx = new ClassPathXmlApplicationContext("resources/person-beans.xml");PersonService p = (PersonService) ctx.getBean("personService");p.info();System.out.println(p.getName());}}

运行

参考:

Spring AOP 详解

spring的JavaConfig方式及xml配置文件混用的例子

最最简单的spring及AOP实例

转载于:https://my.oschina.net/u/2245781/blog/597209

Spring进行面向切面编程的一个简单例子相关推荐

  1. Java实例类中的切面_Spring进行面向切面编程的一个简单例子

    一.eclipse新建java项目取名SpringTest 二.导入sping包到构建路径 还需要aspectjweaver.jar 三.创建java类(当然先要创建各种包) IHelloServic ...

  2. Spring(4)——面向切面编程(AOP模块)

    Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...

  3. spring框架学习 - 使用 Spring 的面向切面编程

    接上一篇博客:https://blog.csdn.net/qq_43605444/article/details/122029896?spm=1001.2014.3001.5502 七.使用 Spri ...

  4. Java绝地求生—Spring AOP面向切面编程

    Java绝地求生-Spring AOP面向切面编程 背景 动态代理 构建被代理对象 自动生成代理 调用动态代理 Spring方法 方式一:使用Spring的API接口 方式二:使用自定义类 方式三:使 ...

  5. 【Spring】面向切面编程AOP

    AOP基础 什么是AOP [废话解释]在软件业,AOP全称Aspect Oriented Programming 即:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AO ...

  6. 图文结合分析Spring的面向切面编程--AOP

    Spring还可以这么学–AOP 上一篇文章Spring还可以这么学–IoC(控制反转) / DI(依赖注入)理解 1. 什么是AOP? AOP(Aspect Oriented Programming ...

  7. Spring AOP(面向切面编程)

    AOP(Aspect Oriented Programming),也就是面向切面编程,作为面向对象编程的一种补充,AOP已经成为一种比较成熟的编程方式.可以这样理解:OOP是从静态角度考虑程序结构,而 ...

  8. Spring之面向切面编程AOP(八)

    介绍&步骤 视频教程: https://www.bilibili.com/video/BV1WZ4y1P7Bp?p=121 官方笔记链接:https://pan.baidu.com/s/1dn ...

  9. Spring AOP——Spring 中面向切面编程

    前面两篇文章记录了 Spring IOC 的相关知识,本文记录 Spring 中的另一特性 AOP 相关知识. 部分参考资料: <Spring实战(第4版)> <轻量级 JavaEE ...

最新文章

  1. 美多后台管理和项目环境搭建
  2. linux命令fdisk
  3. python关闭线程根据id_python之线程相关操作
  4. linux下g++和gcc_Linux中gcc和g ++有什么区别?
  5. Shell 脚本编程之基础
  6. 50个精美的 PSD 用户界面素材和设计模板资源
  7. java futuretask get reject异常_FutureTask的get()方法之异常处理
  8. 混凝土墙开洞_失传已久的混凝土墙体加固“秘籍”
  9. 【Elasticsearch】eBay上的Elasticsearch性能调优实践
  10. docker java 中文乱码_java使用awt包在生产环境docker部署时出现中文乱码的处理
  11. iframe 实现网页本页显示
  12. linux自带python_【经验总结】linux 安装python (替换系统自带的python版本)
  13. KMP扩展KMPManacher算法基础与习题(第二更)
  14. Seaweedfs 详细说明
  15. C++获取汉字拼音/简拼/首字母
  16. mysql评论回复表设计_数据库设计——评论回复功能
  17. C++优先级队列priority_queue详解及其模拟实现
  18. 思科 Spanning Tree Protocol(STP)生成树
  19. 计算机硬件专业知识西瓜视频,go实现西瓜视频花椒直播等平台智能答题
  20. Arduino CapacitiveSensor 电容式触摸传感器

热门文章

  1. 11. OD-Delphi程序暴力破解
  2. openssl 对文本加密解密
  3. mgy最新地址 mgyuser.com
  4. 转到Servlet出现500型错误
  5. SybaseASE系统表的应用
  6. Android实现异步处理 -- HTTP请求
  7. C# Windows Phone 8 WP8 开发,将WebClient的DownloadStringCompleted事件改成非同步的awiat方法。...
  8. 中央民族大学计算机考研考什么,中央民族大学电子信息专业硕士研究生入学考试初试科目考试大纲...
  9. Hollis要转行了?
  10. 这8种保证线程安全的技术你都知道吗?