Spring注解AOP及单元测试junit(6)
2019-03-10/20:19:56
演示:将xml配置方式改为注解方式
静态以及动态代理推荐博客:https://blog.csdn.net/javazejian/article/details/56267036
junit单元测试jar包:https://share.weiyun.com/5pKuXVL
1.注解配置业务类
使用@Component("s") 注解ProductService 类表示业务类的Bean名字为 s
package service; import org.springframework.stereotype.Component; @Component("s") public class ProductService { public void doSomeService(){ System.out.println("doSomeService"); } }
2.注解配置切面AOP
@Aspect 注解表示这是一个切面
@Component 表示这是一个bean,由Spring进行管理
在切面类的具体的方法前加上一句,表示这个切点被触发的时候,执行该函数,用Around方式,相当于把这个切点和这个切点的处理方法关联起来。
@Around(value = "execution(* service.ProductService.*(..))") 表示对service.ProductService 这个类中的所有方法进行切面操作.
含义就是,当expression中的函数被调用时,就会用around形式来触发切面函数,这条语句放在谁前面,谁就被定义为切面函数,也就是辅助功能。
package aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class LoggerAspect { @Around(value = "execution(* service.ProductService.*(..))") public Object log(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("start log:" + joinPoint.getSignature().getName()); Object object = joinPoint.proceed(); System.out.println("end log:" + joinPoint.getSignature().getName()); return object; } }
3.配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="aspect"/> <context:component-scan base-package="service"/> <aop:aspectj-autoproxy/> </beans>
4.单元测试junit
1.下载jar包地址在文章引用部分 junit-4.12.jar和hamcrest-all-1.3.jar 记得Add
2.修改TestSpring
@RunWith(SpringJUnit4ClassRunner.class) 表示这是一个Spring的测试类
@ContextConfiguration("classpath:applicationContext.xml")定位Spring的配置文件
@Autowired给这个测试类装配Category对象
@Test测试逻辑,打印c对象的名称
3.单元测试用的例子是博主SpringIOC/DI这篇文章中的例子参考链接https://www.cnblogs.com/bencoper/p/10494369.html
4.所有代码
TestSpring
package test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import pojo.Category; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class TestSpring { @Autowired Category c; @Test public void test(){ System.out.println(c.getName()); } }
package com.how2java.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import pojo.Category; public class TestSpringOldWay { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); Category c = (Category) context.getBean("c"); System.out.println(c.getName()); } }
TestSpringOldWay
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="c" class="pojo.Category"> <property name="name" value="category 1" /> </bean> </beans>
applicationContext.xml
package pojo; public class Category { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Category.java
测试结果
Spring注解AOP及单元测试junit(6)的更多相关文章
- Spring注解 - AOP 面向切面编程
基本概念: AOP:Aspect Oriented Programming,即面向切面编程 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式 前置通知(@Before):在目标 ...
- 基于spring注解AOP的异常处理
一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...
- spring 注解AOP
aspectAnnotation的切面信息,加到了AnnotationAwareAspectJAutoProxyCreator的advisorsCache属性里面去了. 解析annotationSe ...
- 重新学习Spring注解——AOP
面向切面编程——思想:在一个地方定义通用功能,但是可以通过声明的方式定义这个功能要以何种方式在何处运用,而无须修改受影响的类. 切面:横切关注点可以被模块化为特殊的类. 优点: 1.每个关注点都集中在 ...
- spring注解 aop
@Resource(name="as") 为空按类型装配 @autowired 按类型 @quafiler (name="xx") 按名称 spring继承关 ...
- spring 注解aop调用invoke()
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlAp ...
- JAVAEE——spring02:使用注解配置spring、sts插件、junit整合测试和aop演示
一.使用注解配置spring 1.步骤 1.1 导包4+2+spring-aop 1.2 为主配置文件引入新的命名空间(约束) 1.3 开启使用注解代替配置文件 1.4 在类中使用注解完成配置 2.将 ...
- Spring注解配置、Spring aop、整合Junit——Spring学习 day2
注解配置: 1.为主配置文件引入新的命名空间(约束) preference中引入文件 2.开启使用注解代理配置文件 <?xml version="1.0" encoding= ...
- 一步一步深入spring(5)--使用基于注解的spring实现 AOP
1.要利用spring aop,至少需要添加以下jar包 使用spring需要的jarspring.jar .commons-logging.jar 使用切面编程(AOP)需要的jar aspectj ...
随机推荐
- 1. CMake 系列 - 从零构建动态库和静态库
目录 1. 文件目录结构 2. 库文件源代码 3. 编译生成库文件 1. 文件目录结构 首先创建如下目录结构: └── lib ├── build # ├── CMakeLists.txt └── s ...
- Java代码规范与质量检测插件SonarLint
1. SonarLint SonarLint是一个代码质量检测插件,可以帮助我们检测出代码中的坏味道 下载与安装 在需要检测的单个文件或者单个项目上右键 --> Analyze --> ...
- 依赖注入容器-- Autofac
目录: 一.简介 二.如何使用 2.1.基本使用 2.2.接口使用 2.3. 其他注入 2.4. 注入的生命周期 一.简介 在上一篇文章中讲到替换默认服务容器,我们选择了Autofac Autofac ...
- Android Gradle 依赖配置:implementation & api
背景: Android Gradle plugin 3.0开始(对应Gradle版本 4.1及以上),原有的依赖配置类型compile已经被废弃,开始使用implementation.api和anno ...
- 全网首发! Odoo 订单分解执行及供应链管理免费开源解决方案
引言 前一篇介绍了佛山王朝家具的案例背景.佛山王朝家具公司在全国有30多家门店,三个生产工厂.王朝家具有六大痛点问题: 订单迫切需要实现电子化管理及在线签名 总部分单工作量大,供应链效率低 配送和售后 ...
- 2017-10-31 中文代码示例教程之Vuejs入门&后续计划
"中文编程"知乎专栏原链 为了检验中文命名在主流框架中的支持程度, 这里把vuejs官方入门教程第一部分的示例代码中尽量使用了中文命名. 过程中有一些发现, 初步看来Vuejs对中 ...
- SQLsever存储过程分页查询
使用存储过程实现分页查询,SQL语句如下: USE [DatebaseName] --数据库名 GO /****** Object: StoredProcedure [dbo].[Pagination ...
- CentOS 查看和修改 Mysql 字符集
客户提供了 mysql 的环境,但是不支持中文,通过以下命令查看了 mysql 的字符集 mysql> show variables like 'character_set%'; 显示如下: + ...
- Could not load file or assembly……
今天在运行一个ASP.NET Core项目的时候发现这样的错误: 我一开始觉得这是个很简单的问题,很明显,出错的原因是项目中某些地方还保留了对Njt.MvcAuthLib这个库的引用,而现在我不需要了 ...
- 彻底关闭windows10自动更新解决方案
window10的自动更新其实和window7和window8都有所不同,有些人认为只要停止了windows updates 就可以了,不会再开始了! 但是往往不是这样的,因为微软在window up ...