Spring之AOP简单demo
1.加入JAR包。出了Spring自身的Jar包还要一些依赖的JAR包。不然会报ClassNotFound。
Student.java
package com.lubby.bean;
import org.springframework.stereotype.Component;
@Component("student")
public class Student {
private String id;
private String name;
public Student() {
super();
}
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
public void readBook() {
System.out.println("I am reading book now.....");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Something.java
package com.lubby.bean;
import org.springframework.stereotype.Component;
@Component("something")
public class Something {
public void borrowBook() {
System.out.println("Borrow the book from library......");
}
public void returnBook() {
System.out.println("Return the book to library........");
}
}
test.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
default-lazy-init="true"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 自己主动检測并注冊为spring中的bean+启用注解 -->
<context:component-scan base-package="com.lubby.bean"></context:component-scan>
<aop:config>
<aop:aspect ref="something">
<aop:pointcut expression="execution(* com.lubby.bean.Student.readBook(..))" --定义切点为readBook方法
id="readBook" />
<aop:before pointcut-ref="readBook" method="borrowBook" />---在运行readBook方法之前运行borrowBook方法
<aop:after-returning pointcut-ref="readBook" ----在运行readBook方法之后运行returnBook
method="returnBook" />
</aop:aspect>
</aop:config>
</beans>
运行结果
Borrow the book from library......
I am reading book now.....
Return the book to library........
Spring之AOP简单demo的更多相关文章
- 使用Spring缓存的简单Demo
使用Spring缓存的简单Demo 1. 首先创建Maven工程,在Pom中配置 <dependency> <groupId>org.springframework</g ...
- Spring的AOP简单理解
最近在研究spring的AOP,翻译出来的意思是面向切面. 总结如下: 所谓AOP就是将分散在各个方法处的公共代码提取到一处, 并通过类似拦截器的机制实现代码的动态整合.可以简单地想象成, 在某个方法 ...
- spring的AOP
最近公司项目中需要添加一个日志记录功能,就是可以清楚的看到谁在什么时间做了什么事情,因为项目已经运行很长时间,这个最初没有开来进来,所以就用spring的面向切面编程来实现这个功能.在做的时候对spr ...
- 02 浅析Spring的AOP(面向切面编程)
1.关于AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- 跟着刚哥学习Spring框架--AOP(五)
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- Spring框架 AOP面向切面编程(转)
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- Spring ( 四 )Spring的AOP动态代理、切面编程
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.AOP切面编程 1.什么是AOP AOP是面向切面编程.全称:Aspect Oriented Pro ...
- Spring的简单demo
---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...
- Spring环境搭建及简单demo
1. Spring框架简介(以下这段话可用于面试求职) Spring为JavaEE开发提供了一个轻量级的解决方案,主要表现为, IOC(或者叫做DI)的核心机制,提供了bean工厂(Spring容器) ...
随机推荐
- linux NFS 客户端的安装
1. 安装 showmount [root@allentuns ~]# yum -y install showmount 2. 查看服务器共享 [root@allentuns ~]# showmoun ...
- devops持续集成,Centos7.6下gitlab+jenkins(pipeline)实现代码自动上线
持续集成 gitlab+jenkins(pipeline)实现代码自动上线 环境准备:Centos7.6版本ip:192.168.0.13 主机名:gitip:192.168.0.23 主机名:jen ...
- python基础--内置函数filter,reduce
movie_people=["sb+_alex","sb_wupeiqi","han"] # def filter_test(array): ...
- python基础--局部变量与全局变量
#全局变量作用于全局或整个程序中,程序执行完毕后销毁,局部变量作用在当前函数中,调用函数执行完毕及销毁 #如果函数的内容无global关键字,优先读取同名局部变量,如果没有同名局部变量,只能读取同名全 ...
- pwd 显示当前所在的工作路径
1.功能说明 pwd命令是“print working directory ”首字母缩写,显示当前目录的绝对路径. 2.语法格式 pwd [option] pwd 选项 3.命令参数 参数 参数说明 ...
- Sass @debug
@debug 在 Sass 中是用来调试的,当你的在 Sass 的源码中使用了 @debug 指令之后,Sass 代码在编译出错时,在命令终端会输出你设置的提示 Bug: @debug 10em + ...
- python笔记(1)--基础知识
一.注释 单行注释 #打印“hello world” print("hello.world!") 另外一种单行注释 print("hello,world!") ...
- powerdesign 显示comment,生成excel
一.显示 Comment 1. 在弹窗的Table描述里面显Comment 效果图 实现方式: 2. 在最外层显示Comment 效果图 现实方式: 2.1. Tools > Display P ...
- linux运维、架构之路-linux磁盘管理
一.企业中磁盘选购: 1.线上的业务,用SAS磁盘 2.线下的业务,用SATA磁盘,磁带库 3.线上高并发.小容量(多人浏览力图片)的业务,SSD磁盘 4.根据数据的访问热度,智能分析分层存储,SAT ...
- PDO 的错误处理
PDO 全称 PHP Data Object ------------------ 错误处理 ------------------------- php 的 mysql 扩展对于 mys ...