Java整理18

1、AOP

AOP概念:通过预编译方式和运行期动态代理方式实现,在不修改源代码的情况下给程序动态统一添加额外功能的一种技术。
动态代理分类:
	JDK动态代理:有接口
		代理对象和目标对象实现同样的接口
	cglib动态代理:无接口
		继承被代理的目标类
(1)引入aop相关的依赖
(2)创建目标资源(接口和实现类)
(3)创建切面类(切入点和通知类型)
	切面类上加@Aspect[声明是切面类]@Component[说明是由IOC容器管理]
xml配置中开启aspectj自动代理,为目标对象生成代理
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

2、切入点和通知类型

通知类型:
	前置@Before	返回@AfterReturning	异常@AfterThrowing	后置@After	环绕@Around
@Before(value="切入点表达式配置切入点")
execution_1(public_2 int_3 com.atgui.spring.aop_4.caculator_5.div_6(7))
1-固定格式、2-权限修饰符、3-方法返回值、4-包名、5-类名、6-方法名、7-参数列表
2、3、4、5、6都可以用通配符*表示,7(..)表示参数列表任意
@Before(切入点表达式)
public void beforeMethod(JoinPoint joinpoint获取切入点信息)
{
	String methodname=joinpoint.getSignature().getName();
	Object[] objects=joinpoint.getArgs();
}
返回通知:
	@AfterReturning(切入点表达式,returning="自定义"获取返回值)
	public void afterReturning(JoinPoint joinpoint,Object 自定义)
异常通知:
	@AfterThrowing(切入点表达式,throwing="自定义"获取方法的异常信息)
	public void afterThrowing(JointPoint joinpoint,Throwable 自定义)
环绕通知:
	@Around(切入点表达式)
	public Object aroundMethod(ProceedingJoinPoint pjoinpoint)
	{
		try{
			Object result=pjoinpoint.proceed();
		}
		catch{ }
		finally{ }
	}
重用切入点表达式:
	@Pointcut(value=切入点表达式)
	public void PointCut_方法名(){}
	重用:
		@After(value="PointCut()")			同一个包下
		@After(value="包名.类名.方法名")		不同包

3、切面优先级

外层高于内层,@Order(值),值越小,优先级越高

4、xml配置文件设置通知类型

开启组件扫描
<aop:config>
	<aop:aspect ref="类名称">	//配置切面类
	<aop:pointcut id="自定义" expression="切入点表达式"/>
	配置通知类型:
		<aop:before method="方法名" poincut-ref="自定义"></aop:before>	前
		<aop:after method="方法名" poincut-ref="自定义"></aop:after>	后
		<aop:after-returing method="方法名" poincut-ref="自定义" returning="返回名"></aop:after-returing>	返回

5、Spring整合Junit

(1)依赖引入<artifactId>spring-text</artifactId>
(2)测试类@SpringJunitConfig(locations="classPath:bean.xml")
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值