热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringAop基于注解&Xml

目录1,【基于注解】1.1,基本结构1.2,实现步骤2,【基于XML】2.1,基本结构2.2,实现步骤1,【基于注解】1.1,基本结构        编写UserService,提

目录

1,【基于注解】

1.1,基本结构

1.2,实现步骤

2,【基于XML】

2.1,基本结构

2.2,实现步骤




1,【基于注解】

1.1,基本结构

        编写UserService,提供 insertUser 和 updateUser

        编写切面类,对两个方法进行事务管理(开始事务、结束事务)

        ​​​​​​​        


1.2,实现步骤

目标类

package com.czxy.demo16_aop_interface.service.impl;
import com.czxy.demo16_aop_interface.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Override
public String insertUser() {
System.out.println("添加");
return "Hello";
}
@Override
public String updateUser() {
System.out.println("更新");
return "World";
}
}

 配置类

package com.czxy.demo16_aop_interface.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan({"com.czxy.demo16_aop_interface.service","com.czxy.demo16_aop_interface.aop"})
@EnableAspectJAutoProxy //开启AOP
public class Demo16Configuration {
}

测试类

package com.czxy.demo16_aop_interface;
import com.czxy.demo16_aop_interface.config.Demo16Configuration;
import com.czxy.demo16_aop_interface.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Demo16Configuration.class)
public class TestDemo16 {
@Resource
private UserService userService;
@Test
public void test16(){
userService.insertUser();
userService.updateUser();
}
}

切面类

package com.czxy.demo16_aop_interface.aop;
import org.aspectj.lang.JoinPoint;
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 MyAspect {
//抽取切入点表达式
@Pointcut("execution(* com.czxy.demo16_aop_interface..*.*(..))")
private void myPointuct(){}
@Before(value = "myPointuct()")
public void start(){
System.out.println("前置通知");
}
@After("myPointuct()")
public void myAfter(JoinPoint joinPoint){
System.out.println("最终通知");
}
}


2,【基于XML】

2.1,基本结构


2.2,实现步骤

 步骤1:UserService接口:

package com.czxy.demo21_xml_aop.service;
public interface UserService {
Integer insertUser();
String updateUser();
}

步骤2:UserServiceImpl实现类

package com.czxy.demo21_xml_aop.service.impl;
import com.czxy.demo21_xml_aop.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public Integer insertUser() {
System.out.println("Demo21 添加");
return 1;
}
@Override
public String updateUser() {
System.out.println("Demo21 更新");
return "更新结果";
}
}

步骤3:MyAspect 切面类

package com.czxy.demo21_xml_aop.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {
public void myBeforeAdvice(){
System.out.println("开启事务");
}
public void myAfterReturningAdvice(JoinPoint joinPoint,Object obj){
System.out.println("目标类:"+joinPoint.getTarget());
System.out.println("方法名:"+joinPoint.getSignature().getName());
System.out.println("返回值:"+obj);
System.out.println("提交事务");
}
public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前");
//执行目标
Object proceed = proceedingJoinPoint.proceed();
System.out.println("环绕后");
return proceed;
}
public void myAfterThrowingAdvice(Throwable e) {
System.out.println("异常通知:" + e.getMessage());
}
public void myAfterAdvice() {
System.out.println("释放资源");
}
}

步骤4:测试类

package com.czxy.demo21_xml_aop;
import com.czxy.demo21_xml_aop.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@ContextConfiguration(locatiOns= "classpath:demo21.xml")
public class TestDemo21 {
@Resource(name = "UserService12")
private UserService userService;
@Test
public void TestDemo21(){
userService.insertUser();
System.out.println("-----------");
userService.updateUser();
}
}

xml配置文件


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cOntext="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
















推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
author-avatar
teddy213
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有