Spring 顾问
1.名称匹配方法切入点顾问
接口:ISomeService
public interface ISomeService {
public void doSome();
public void doSecont();
}
类
public class MyBeforeAdvise implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==========log==========");
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
}
public void doSecont() {
System.out.println("=================Secont================");
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--前置通知 名称匹配方法切入点顾问-->
<!--.目标对象-->
<bean id="someService" class="cn.happy.spring08aop_Advice.SomeService"></bean>
<!--.前置增强(通知)-->
<bean id="beforeAdvice" class="cn.happy.spring08aop_Advice.MyBeforeAdvise"></bean> <!--.前置增强(顾问)-->
<bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="beforeAdvice"></property>
<property name="mappedNames" value="beforeAdvisor"></property>
</bean>
<!--.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvice"></property>
<property name="proxyTargetClass" value="true"></property>
</bean>
</beans>
单测
//1.前置通知 名称匹配方法切入点顾问
@Test
public void test01(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext05_aop05_Advice.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
service.doSecont();
}
2.正则表达式 匹配方法切入点顾问
接口:ISomeService
public interface ISomeService {
public void doSome();
public void doSecont();
}
类
public class MyBeforeAdvise implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==========log==========");
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
}
public void doSecont() {
System.out.println("++===================Secont====================++");
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--正则表达式 匹配方法切入点顾问-->
<!--.目标对象-->
<bean id="someService" class="cn.happy.spring09aop_AdviceZenz.SomeService"></bean>
<!--.前置增强(通知)-->
<bean id="beforeAdvice" class="cn.happy.spring09aop_AdviceZenz.MyBeforeAdvise"></bean> <!--.前置增强(顾问)-->
<bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="beforeAdvice"></property>
<property name="pattern" value=".*d.*"></property>
</bean>
<!--.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvisor"></property> </bean>
</beans>
单测
//2.正则表达式 匹配方法切入点顾问
@Test
public void test02(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext06_aop06_Zenz.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
service.doSecont();
}
Spring 顾问的更多相关文章
- Spring顾问、IOC注解和注解增强
一.顾问 通知的一种表现方式(顾问包装通知/增强) Advisor: 名称匹配方法: NameMecthMethodPointcutAdvisor 1.定义了一个业务类 package cn.spri ...
- Spring——顾问封装通知
通知(advice)是Spring中的一种比较简单的切面,只能将切面织入到目标类的所有方法中,而无法对指定方法进行增强 顾问(advisor)是Spring提供的另外一种切面,可以织入到指定的方法中 ...
- spring顾问包装通知
前边说到了顾问就是通知,没有实践,这里就实践一下,证明一下. 虽然可以说顾问就是通知,但是他们还是有的一定的区别的,通知是将目标类中所有的方法都进行的增强,而顾问却可以指定到特定的方法上,也就是说顾问 ...
- spring增强
1.前置增强 接口:ISomeService public interface ISomeService { public void doSome(); } 类 public class MyBefo ...
- Spring 通知(Advice)和顾问(Advisor)
AOP ( Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...
- Spring中的通知(Advice)和顾问(Advisor)
在Spring中,目前我学习了几种增强的方式,和大家分享一下 之前的话: 1.AOP (Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect O ...
- SSM-Spring-12:Spring中NameMatchMethodPointcutAdvisor名称匹配方法切入点顾问
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- advice 是通知advisor 是顾问 顾问(Advisor) 通知Advice是Spring提供的一种切 ...
- Spring 通知和顾问进行增强
使用顾问增加前置增强和后置增强 <bean id="1" class="目标对象"></bean> <bean id=" ...
- Spring笔记07(Spring AOP的通知advice和顾问advisor)
1.Spring AOP的通知advice 01.接口代码: package cn.pb.dao; public interface UserDao { //主业务 String add(); //主 ...
随机推荐
- 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
- poj1094Sorting It All Out——拓扑排序
题目:http://poj.org/problem?id=1094 看到此题,首先觉得这是一种层层递进的关系,所以可以想到用拓扑排序: 就像人工排序,每次需要找到一个最小的,再找到新的最小的……所以用 ...
- poj2392磊石头——排序后背包
题目: 首先按限制高度从小到大排序,不会影响可行解,而不排序可能卡掉正确的情况: 用%2滚动数组时一定注意每次复制上一种情况,因为这个WA了好几次. 代码如下: #include<iostrea ...
- 重学JAVA基础(一):PATH和CLASSPATH
我想大多数Java初学者都会遇到的问题,那就是怎么配置环境,执行java -jar xxx.jar 都会报NoClassDefFindError,我在最开始学习的时候,也遇到了这些问题. 1.PAT ...
- 百度地图API的第一次接触——热区
1.代码很简单 var map = new BMap.Map("container"); var point = new BMap.Point(116.404, 39.915); ...
- Exception in thread "main" java.lang.NoClassDefFoundError: antlr/ANTLRException 解决方法
转自:https://blog.csdn.net/gengkunpeng/article/details/6225286?utm_source=blogxgwz4 1. struts2.3.15 hi ...
- JAVA类型信息——Class对象(转载)
JAVA类型信息--Class对象 一.RTTI概要 1.类型信息RTTI :即对象和类的信息,例如类的名字.继承的基类.实现的接口等. 2.类型信息的作用:程序员可以在程序运行时发现和使用类型信息. ...
- Pseudo Random Nubmer Sampling
Pseudo Random Nubmer Sampling https://en.wikipedia.org/wiki/Inverse\_transform\_sampling given a dis ...
- Laravel框架中使用邮件发送功能
这里是演示的用户注册之后,进行邮件激活的功能. 点击注册之后,系统会自动发送一个份邮件到注册者的邮箱,注册者点击链接激活账号. 先配置laravel中的(.env)文件 MAIL_DRIVER=smt ...
- 大白话5分钟带你走进人工智能-第二十六节决策树系列之Cart回归树及其参数(5)
第二十六节决策树系列之Cart回归树及其参数(5) 上一节我们讲了不同的决策树对应的计算纯度的计算方法, ...