Spring学习四
1先导入 Asject的jar包
2配置文件加入标签 ,并加入
<aop:aspectj-autoproxy proxy-target-class="true">(如果是实现接口模式的代理就不用后面的true)
</aop:aspectj-autoproxy>
3代理class加@Componet 和@Aspect,方法前面加@Before 当然不止Before 后面介绍
代码如下
package AOP;//接口类
public interface CalculatorMethod {
int add(int x,int y);
int sub(int x,int y);
int mul(int x,int y);
int div(int x,int y);
}
package AOP;
import org.springframework.stereotype.Component;
@Component
public class Calculator implements CalculatorMethod {
public int add(int x,int y) {
return (x+y);
}
public int sub(int x,int y) {
return (x-y);
}
public int mul(int x,int y) {
return (x*y);
}
public int div(int x,int y) {
return (x/y);
}
}
package AOP;//关键代理文件
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Logging {
@Before("execution(int AOP.Calculator.add(int, int))")
/*
* 可以缩略成execution(int AOP.Calculator.*(int, int)),代表这个包下所有方法都行。
*
* */
public void log(){
System.out.println("I am out");
}
}
package AOP;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");//在工程目录下读取bean的配置。
// CalculatorMethod cal = (CalculatorMethod) context.getBean("calculator");//这是基于接口形式 如果要基于cglib这种,则要修改xml配置文件加入proxy-target-class="true"
Calculator cal = (Calculator) context.getBean("calculator");
System.out.println(cal.add(8, 8));
System.out.println(cal.sub(8, 8));
}
}
/*
输出
I am out
16
0
*/
Bean文件添加
<!-- 如果要指定某些anntion 则可以使用 标签<context:include-filter>当然 use-defalut-filters要设置为false -->
<context:component-scan base-package="AOP" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan> <aop:aspectj-autoproxy proxy-target-class="true"> </aop:aspectj-autoproxy>
五种注解,@Before 前置,@After后置, @AfterReturning 返回,@AfterThrowing 异常, @Around环绕,具体关系看代码。
package AOP; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Aspect
@Component
public class Logging { @Before("execution(int AOP.Calculator.add(int, int))")
/*
* 可以缩略成execution(int AOP.Calculator.*(int, int)),代表这个包下所有方法都行。
*
* */
public void log(){
System.out.println("I am out");
} @After("execution(int AOP.Calculator.*(int, int))")//这里不能用返回值,因为返回值得再返回函数。
public void log2(JoinPoint jp){
String methodName = jp.getSignature().getName();
List<Object> l = Arrays.asList(jp.getArgs());
System.out.println("Method name: "+methodName+" with "+l); } //@AfterReturning("execution(int AOP.Calculator.*(int, int))",returning="result")
@AfterReturning(returning="result"
, pointcut="execution(int AOP.Calculator.*(int, int))")
public void log3(JoinPoint jp,Object result){
String methodName = jp.getSignature().getName();
List<Object> l = Arrays.asList(jp.getArgs());
System.out.println("Method name: "+methodName+" end with "+result);
} @AfterThrowing(throwing="e", pointcut="execution(int AOP.Calculator.*(int, int))")
public void log4(JoinPoint jp,Exception e){//注意这里填写什么异常就捕获什么异常,比如NullPointExcepiton只捕获空指针异常。
String methodName = jp.getSignature().getName();
List<Object> l = Arrays.asList(jp.getArgs());
System.out.println("Method name: "+methodName+" with "+l+" error:"+e);
} @Around("execution(int AOP.Calculator.*(int, int))")//相当于一次代理,用这个可以理解上面的过程
public Object log5(ProceedingJoinPoint pjp){
Object result =null; try { System.out.println("Around 前置"); //前置
result = pjp.proceed();
System.out.println("Around 后置");//后置
} catch (Throwable e) {//异常
// TODO Auto-generated catch block
System.out.println("Around 异常");
e.printStackTrace();
} System.out.println("Around 返回");//返回
return result;
}
}
切面的优先级 @Order 数值越小优先级越高,切入表达式 @Pointcut 声明表达式,不用加入其它方法,只是用于声明切入点,用的在其他方法里面加入该方法名字即可。其他class加class名字
代码简单如下
@Order(1)
@Aspect
@Component
public class Logging2 { @Pointcut("execution(int AOP.Calculator.add(int, int))")
public void logPoint(){} @After("logPoint()")
public void log1(){
System.out.println("afetr log 2");
} }
package AOP; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(2)
@Aspect
@Component
public class Logging { @Before("Logging2.logPoint()")
/*
* 可以缩略成execution(int AOP.Calculator.*(int, int)),代表这个包下所有方法都行。
*
* */
public void log(){
System.out.println("I am out");
} @After("Logging2.logPoint()")//这里不能用返回值,因为返回值得再返回函数。
public void log2(JoinPoint jp){
String methodName = jp.getSignature().getName();
List<Object> l = Arrays.asList(jp.getArgs());
System.out.println("Method name: "+methodName+" with "+l); } //@AfterReturning("execution(int AOP.Calculator.*(int, int))",returning="result")
@AfterReturning(returning="result"
, pointcut="Logging2.logPoint()")
public void log3(JoinPoint jp,Object result){
String methodName = jp.getSignature().getName();
List<Object> l = Arrays.asList(jp.getArgs());
System.out.println("Method name: "+methodName+" end with "+result);
} @AfterThrowing(throwing="e", pointcut="Logging2.logPoint()")
public void log4(JoinPoint jp,Exception e){//注意这里填写什么异常就捕获什么异常,比如NullPointExcepiton只捕获空指针异常。
String methodName = jp.getSignature().getName();
List<Object> l = Arrays.asList(jp.getArgs());
System.out.println("Method name: "+methodName+" with "+l+" error:"+e);
} @Around("Logging2.logPoint()")//相当于一次代理,用这个可以理解上面的过程
public Object log5(ProceedingJoinPoint pjp){
Object result =null; try { System.out.println("Around 前置"); //前置
result = pjp.proceed();
System.out.println("Around 后置");//后置
} catch (Throwable e) {//异常
// TODO Auto-generated catch block
System.out.println("Around 异常");
e.printStackTrace();
} System.out.println("Around 返回");//返回
return result;
}
}
基于xml的配置aspect切面
先声明切面的bean,之后配置AOP 用 aop:config
//删除所有注解注入方式
public class Logging { /*
* 可以缩略成execution(int AOP.Calculator.*(int, int)),代表这个包下所有方法都行。
*
* */
public void log(){
System.out.println("I am out");
} public void log2(JoinPoint jp){
String methodName = jp.getSignature().getName();
List<Object> l = Arrays.asList(jp.getArgs());
System.out.println("Method name: "+methodName+" with "+l); } //@AfterReturning("execution(int AOP.Calculator.*(int, int))",returning="result") public void log3(JoinPoint jp,Object result){
String methodName = jp.getSignature().getName(); System.out.println("Method name: "+methodName+" end with "+result);
} public void log4(JoinPoint jp,Exception e){//注意这里填写什么异常就捕获什么异常,比如NullPointExcepiton只捕获空指针异常。
String methodName = jp.getSignature().getName();
List<Object> l = Arrays.asList(jp.getArgs());
System.out.println("Method name: "+methodName+" with "+l+" error:"+e);
} public Object log5(ProceedingJoinPoint pjp){
Object result =null; try { System.out.println("Around 前置"); //前置
result = pjp.proceed();
System.out.println("Around 后置");//后置
} catch (Throwable e) {//异常
// TODO Auto-generated catch block
System.out.println("Around 异常");
e.printStackTrace();
} System.out.println("Around 返回");//返回
return result;
}
}
bean 修改如下
<!-- 配置切面的bean -->
<bean id="loggin1" class="AOP.Logging"></bean>
<bean id="loggin2" class="AOP.Logging2"></bean> <!-- 切面的具体设置 -->
<aop:config>
<aop:pointcut expression="execution(int AOP.Calculator.add(int, int))" id="express"/><!-- 切面需要代理的点 --> <!-- 一个aspect可以代表一个切面 -->
<aop:aspect ref="loggin1" order="1">
<aop:before method="log" pointcut-ref="express"/>
<aop:after method="log2" pointcut-ref="express"/>
<aop:after-throwing method="log4" pointcut-ref="express" throwing="e"/>
<aop:after-returning method="log3" pointcut-ref="express" returning="result"/>
<aop:around method="log5" pointcut-ref="express"/>
</aop:aspect> <aop:aspect ref="loggin2" order="2">
<aop:after method="log1" pointcut-ref="express"/>
</aop:aspect> </aop:config>
Spring学习四的更多相关文章
- spring学习(四) ———— 整合web项目(SSH)
清楚了spring的IOC 和 AOP,最后一篇就来整合SSH框架把,记录下来,以后应该会用的到. --WH 一.web项目中如何使用spring? 当tomcat启动时,就应该加载spring的配置 ...
- Spring学习(四)--面向切面的Spring
一.Spring--面向切面 在软件开发中,散布于应用中多处的功能被称为横切关注点(cross- cutting concern).通常来讲,这些横切关注点从概念上是与应用的业 务逻辑相分离的(但是往 ...
- spring学习四:springMVC
ref:http://www.cnblogs.com/ysocean/tag/SpringMVC%E5%85%A5%E9%97%A8%E7%B3%BB%E5%88%97/ Spring MVC的处理流 ...
- spring学习 四 对象的创建
spring中,有三种创建对象的方式 (1)构造创建 (2)实例工厂构造 (3)静态工厂构造 一 构造器创建 在构造器创建对象时,有无参构造和有参构造 两种 (1)在spring中,默认的是无参构造 ...
- Spring学习(四)-----Spring Bean引用同xml和不同xml bean的例子
在Spring,bean可以“访问”对方通过bean配置文件指定相同或不同的引用. 1. Bean在不同的XML文件 如果是在不同XML文件中的bean,可以用一个“ref”标签,“bean”属性引用 ...
- Spring学习四----------Bean的配置之Bean的配置项及作用域
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的作用域(每个作用域都是在同一个Bean容器中) 1.singleton:单例,指一个Bean容器中只存在一份(默认) 2.prototype ...
- Spring学习(四)
Spring Ioc容器 1.具有依赖注入功能的容器.负责实例化,定位,配置应用程序中的对象及建立这些对象间的依赖.在Spring中BeanFactory是Ioc容器的实际代表者.BeanFactor ...
- spring学习四:Spring中的后置处理器BeanPostProcessor
BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...
- 我的Spring学习记录(四)
虽然Spring管理这我们的Bean很方便,但是,我们需要使用xml配置大量的Bean信息,告诉Spring我们要干嘛,这还是挺烦的,毕竟当我们的Bean随之增多的话,xml的各种配置会让人很头疼. ...
随机推荐
- hdu 1542 Atlantis (线段树扫描线)
大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...
- Tree Requests CodeForces - 570D (dfs水题)
大意: 给定树, 每个节点有一个字母, 每次询问子树$x$内, 所有深度为$h$的结点是否能重排后构成回文. 直接暴力对每个高度建一棵线段树, 查询的时候相当于求子树内异或和, 复杂度$O((n+m) ...
- 百度基础架构组-实习生面试(2016.08 java后台开发)
一.项目 1.Spring MVC与Struts2的区别: 2.MVC三层是如何工作的?比如:要访问一个Url?a=xx&b=xx,怎么找到相应的资源,怎么运算,怎么返回等? 3.数据库myb ...
- 『MXNet』第十一弹_符号式编程初探
一.符号分类 符号对我们想要进行的计算进行了描述, 下图展示了符号如何对计算进行描述. 我们定义了符号变量A, 符号变量B, 生成了符号变量C, 其中, A, B为参数节点, C为内部节点! mxne ...
- 6月13 ThinkPHP框架基础
ThinkPHP 一.php框架基础介绍 真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维 ...
- Alibaba Java Coding Guidelines
阿里巴巴于10月14日在杭州云栖大会上,正式发布众所期待的<阿里巴巴Java开发规约>扫描插件!该插件由阿里巴巴P3C项目组研发.P3C是世界知名的反潜机,专门对付水下潜水艇,寓意是扫描出 ...
- python_数据类型
数据类型 1.数字类型 整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点.Python3 整型是没有限制大小的,可以当作 Long 类型使用,所以 Python3 没有 Pytho ...
- django学习之——创建项目
创建项目让我迷茫了会: 直接cmd 执行django-admin.py startproject pro_name 肯定是不行的 必须cd到 D:\Program Files\Python3.3.5 ...
- pip安装报错处理+PyPi源切换教程
一.pip安装出错类型 1.1 pip版本过旧导致不能安装 报错提示: You are using pip version 9.0.3, however version 10.0.1 is avail ...
- kali菜单中各工具功能
一.说明 各工具kali官方简介(竖排):https://tools.kali.org/tools-listing 安装kali虚拟机可参考:https://www.cnblogs.com/lsdb/ ...