Spring的学习运用
  p8RyuvESu2pg 2023年11月02日 19 0

1.AspectJ对AOP的实现

1.1:添加相关的jar包

1.2:AspectJ的通知类型

                                                    (1)前置通知

                                                    (2)后置通知

                                                    (3)环绕通知

                                                    (4)异常通知

                                                    (5)最终通知

1.3:定义切面类

定义方法,在方法上定义各种注解通知

切入点表达式规则:

execution(访问权限 方法返回值 方法名(参数) 异常类型)

切入点表达式要匹配的对象就是目标 方法的方法名

execution(* cn.lexed.service.*.*(..)) 包下所有类的所有方法

execution(* cn.lexed.service..*.*(..)) 包及子包下所有类的所有方法

public * adduser(com.pb.entity.User):"*"表示匹配所有类型的返回值

public void *(com.pb.entity.User):"*"表示匹配所有方法名

public void addUser (..): ".."表示匹配所有参数个数和类型

* com.pb.service.*.*(..):匹配com.pb.service 包下所有类的所有方法

* com.pb.service..*(..):匹配com.pb.service 包及子包下所有类的所有方法

导入jar包

Spring的学习运用_spring

编写SomeService和SomeServiceImpl

Spring的学习运用_方法名_02

SomeService.java

package cn.lexed.service;

public interface SomeService {
	public abstract void doSome(String name,int age);

}

SomeServiceImpl.java

package cn.lexed.service.impl;

import org.springframework.stereotype.Service;

import cn.lexed.service.SomeService;
@Service("s")
public class SomeServiceImpl implements SomeService{

	@Override
	public void doSome(String name, int age) {
		// TODO Auto-generated method stub
		
		System.out.println("执行了业务方法doSome");
		
	}

}

编写MyAspectJ

@Aspect:

作用:修饰类为切面类

位置:在类定义的上面

切面类:把非核心业务代码,增强的代码,放到这个类中

切面类中定义若干方法,将这些方法作为不同通知的方法,用来增强

定义方法:

1.public

2.方法没有返回值

3.方法名自定义

4.方法可以有参数,也可以没有参数

如果有参数,参数不是自定义的,有几个参数类型

@Before:前置通知注解

属性:value

位置:在方法上面

特点:1.在核心方法之前先执行

2.不会改变核心方法执行的结果

3.不会影响核心方法的执行

Spring的学习运用_spring_03

MyAspectJ.java

package cn.lexed.aop;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/* @Aspect:
 *         作用:修饰类为切面类
 *         位置:在类定义的上面
 *         切面类:把非核心业务代码,增强的代码,放到这个类中
 * 
 * */

@Aspect
public class MyAspectJ {
	//切面类中定义若干方法,将这些方法作为不同通知的方法,用来增强
	
	/* 定义方法:
	 *       1.public
	 *       2.方法没有返回值
	 *       3.方法名自定义
	 *       4.方法可以有参数,也可以没有参数
	 *         如果有参数,参数不是自定义的,有几个参数类型
	 * 
	 * */
	
	/* @Before:前置通知注解
	 *     属性:value
	 *     位置:在方法上面
	 *
	 *     特点:1.在核心方法之前先执行
	 *         2.不会改变核心方法执行的结果
	 *         3.不会影响核心方法的执行
	 *         
	 * */
	@Before(value="execution(* cn.lexed.service..*(..))")
	public void myBefore(){
		System.out.println("在业务方法之前进行日志输出显示");
	}
	@AfterReturning(value="execution(* cn.lexed.service..*(..))")
	public void myAfterReturing(){
		System.out.println("在业务方法之后进行事务相关的处理");
	}
}

编写配置文件(点击Namespaces添加aop、context)

Spring的学习运用_xml_04

app.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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

<!-- 使用组件扫描器扫描 -->
<context:component-scan base-package="cn.lexed.service"></context:component-scan>
<!-- 声明切面类对象 -->
<bean class="cn.lexed.aop.MyAspectJ"></bean>
<!-- 配置AspectJ的代理机制 -->
<aop:aspectj-autoproxy/>
</beans>

编写测试类

Spring的学习运用_方法名_05

Test.java

package cn.lexed.test;

import static org.junit.Assert.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.lexed.service.SomeService;

public class Test {

	@org.junit.Test
	public void test() {
		//1.创建Spring容器的对象
		ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
		SomeService pro=(SomeService)ac.getBean("s");
		pro.doSome("李四", 20);
	}

}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
p8RyuvESu2pg
作者其他文章 更多