Spring-aop实现切面的四种方式 (2)
AOP实现方式一
<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
">
<!-- 把那个类注入进来 -->
<bean id="sleepHelper" class="com.test.aop.SleepHelper"/>
<bean id="human" class="com.test.aop.Human"/>
<!-- 配置一个切点 -->
<!-- 配置切点有好几种表达方式 1、配置aspectJ 2.使用正则表达式, 以下使用的是正则表达式 -->
<!-- pattern属性指定了正则表达式,它匹配所有的sleep方法 -->
<bean id="spleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*sleep" />
</bean>
<!-- 切点仅仅是定义了故事发生的地点,还有故事发生的时间以及最重要的故事的内容,就是通知了,我们需要把通知跟切点结合起来,我们要使用的通知者是: -->
<bean id="sleepHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="sleepHelper" />
<property name="pointcut" ref="spleepPointcut" />
</bean>
<!-- 切入点和通知都配置完成,接下来该调用ProxyFactoryBean产生代理对象了 -->
<!-- ProxyFactoryBean是一个代理,我们可以把它转换为proxyInterfaces中指定的实现该interface的代理对象: -->
<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="human" />
<property name="interceptorNames" value="sleepHelperAdvisor" />
<property name="proxyInterfaces" value="com.test.aop.Sleepable" />
</bean>
</beans>
//实现接口
package com.test.aop;
public class Human implements Sleepable {
@Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("睡觉啦,哈哈");
}
}
//定义一个接口实现jdk代理
package com.test.aop;
public interface Sleepable {
void sleep();
}
//实现前后通知
package com.test.aop;
import Java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
niaho();
}
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
enen();
}
public void niaho(){
System.out.println("调用前,你应该做的是");
}
public void enen(){
System.out.println("调用后你应该做的事");
}
}
测试创建ioc容器
package com.test.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Tests {
public static void main(String[] args){
ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/applicationContext-aop1.xml");
Sleepable sleeper = (Sleepable)appCtx.getBean("humanProxy");
sleeper.sleep();
}
}
AOP实现方式二
<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
">
<bean id="sleepHelper" class="com.test.aop2.SleepHelper" />
<bean id="sleepAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="sleepHelper"></property>
<property name="pattern" value=".*sleep"></property>
</bean>
<bean id="human" class="com.test.aop2.Human" />
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans>
package com.test.aop2;
public class Human implements Sleepable {
@Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("睡觉啦,哈哈");
}
}
package com.test.aop2;
public interface Sleepable {
void sleep();
}
package com.test.aop2;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
niaho();
}
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
enen();
}
public void niaho(){
System.out.println("调用前,你应该做的是");
}
public void enen(){
System.out.println("调用后你应该做的事");
}
}
package com.test.aop2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Tests {
public static void main(String[] args){
ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/applicationContext-aop2.xml");
Sleepable sleeper = (Sleepable)appCtx.getBean("human");
sleeper.sleep();
}
}
AOP实现方式三
<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
">
<!-- 自动扫描@Aspect注解 -->
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.test.aop3"/>
</beans>
package com.test.aop3;
import org.springframework.stereotype.Component;
@Component
public class Human implements Sleepable {
@Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("睡觉啦,哈哈");
}
}
package com.test.aop3;
import org.springframework.stereotype.Component;
@Component
public interface Sleepable {
void sleep();
}
package com.test.aop3;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class SleepHelper{
public SleepHelper(){
}
@Pointcut("execution(* *.sleep())")
public void sleeppoint(){}
@Before("sleeppoint()")
public void beforeSleep(){
System.out.println("睡觉前需要脱衣服");
}
@After("sleeppoint()")
public void aftersleep(){
System.out.println("睡醒了要睁眼");
}
}
package com.test.aop3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Tests {
public static void main(String[] args){
ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/applicationContext-aop3.xml");
Sleepable sleeper = (Sleepable)appCtx.getBean("human");
sleeper.sleep();
}
}
AOP实现方式四
<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
">
<!-- 自动扫描@Aspect注解 -->
<context:component-scan base-package="com.test.aop4"/>
<aop:config>
<aop:aspect ref="SleepHelper">
<aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))"/>
<aop:after method="afterSleep" pointcut="execution(* *.sleep(..))"/>
</aop:aspect>
</aop:config>
</beans>
package com.test.aop4;
import org.springframework.stereotype.Component;
@Component
public class Human implements Sleepable {
@Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("睡觉啦,哈哈");
}
}
package com.test.aop4;
import org.springframework.stereotype.Component;
@Component
public interface Sleepable {
void sleep();
}
package com.test.aop3;
import org.springframework.stereotype.Component;
@Component
public class Human implements Sleepable {
@Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("睡觉啦,哈哈");
}
}
package com.test.aop4;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Tests {
public static void main(String[] args){
ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/applicationContext-aop4.xml");
Sleepable sleeper = (Sleepable)appCtx.getBean("human");
sleeper.sleep();
}
}
Spring-aop实现切面的四种方式 (2)的更多相关文章
- Spring-aop实现切面的四种方式(1)
Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程 ...
- Spring中依赖注入的四种方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...
- 【websocket】spring boot 集成 websocket 的四种方式
集成 websocket 的四种方案 1. 原生注解 pom.xml <dependency> <groupId>org.springframework.boot</gr ...
- Spring AOP 中 advice 的四种类型 before after throwing advice around
spring AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...
- spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入
三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...
- Spring通过构造方法注入的四种方式
通过构造方法注入,就相当于给构造方法的参数传值 set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选 的,构造注入的优势是通过构造强制依赖关系,不可能实例化不 完全的或无法使用的bean. Me ...
- Spring中配置数据源的四种方式
1.spring自带的数据源 <bean id="dataSource" class="org.springframework.jdbc.datasource.Dr ...
- Spring事务管理的四种方式(以银行转账为例)
Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...
- 普通java类加入spring容器的四种方式
今天在自己开发的工具类中使用了spring注入的方式调用了其他类,但是发生的报错,在整理了后今天小结一下. 首先简单介绍下spring容器,spring容器是整个spring框架的核心,通常我们说的s ...
随机推荐
- JSON中的坑
坑一. 在使用localStorage时,我们会给一个key存取一个value,这个value可以是一个普通的字符串,也可以是一个对象,如果是一个字符串,我们就需要通过JSON.stringify来转 ...
- 【lua】LWT HttpdModule
要使用httpd模块,需要在脚本开头添加: require "httpd" httpd.pairs(apr_table) 用以遍历apr_table for key, value ...
- Nodejs 实现windows后台运行
首先需要到http://nssm.cc/download/?page=download 下载 nssm 下下来之后是压缩包形式的,解压之后 ctrl + R 进入cmd 命令行界面 在命令行模式下进入 ...
- idea maven install 卡住,无报错排查。
今天使用idea打包,执行install,看控制台日志,卡主了(意思是日志不继续在控制台输打印了,卡主了,也看不到错误),也没有报错,然后进行排查. 进入dos命令,进入到项目的根目录,使用 运行 m ...
- 九度oj题目1342:寻找最长合法括号序列II
题目1342:寻找最长合法括号序列II(25分) 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:886 解决:361 题目描述: 假如给你一个由’(‘和’)’组成的一个随机的括号序列,当然 ...
- Linux 命令 -- chmod
chmod命令用来变更文件或目录的权限.在UNIX系统家族里,文件或目录权限的控制分别以读取.写入.执行3种一般权限来区分,另有3种特殊权限可供运用.用户可以使用chmod指令去变更文件与目录的权限, ...
- Django的学习基础1
著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. Django的MTV模式本质上与MVC模式没有什么差别,也是 ...
- idea快捷键、常用设置
代码提示: code completion :ALT + / 选用Eclipse的keymap. 设置代码助手快捷键(keymap-basic) 代码上下行复制(Ctrl + Alt + Down ...
- javaweb之EL自定义函数
1.什么是EL自定义函数 EL自定义函数是在EL表达式中调用的某个java类的静态方法,这个静态方法需在web应用程序中进行配置才可以被EL表达式调用.EL自定义函数可以扩展EL表达式的功能,让EL表 ...
- Idea 配置启动JDK___在windows中使用Intellij Idea时选择自定义的64位JVM
略过下面的叙述快速配置 配置你的Idea启动jdk (Idea15之后的版本启动不要JDK1.8) 添加环境变量IDEA_JDK_64,配置和JAVA_HOME环境变量相同,路径写到1.8的jdk就 ...