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

  去掉原有的配置添加三行配置  

<context:component-scan base-package="aspect"/>      定义切面类
<context:component-scan base-package="service"/>     定义业务类
 
<aop:aspectj-autoproxy/>       找到被注解了的切面类,进行切面配置
   Spring为了支撑AOP运行,用到了动态代理这种设计模式,这句话的意思就是启动对AOP的支持。
 动态代理以及静态代理的概念博主也是参考其他的资料在引用中提供了地址
 
 <?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)的更多相关文章

  1. Spring注解 - AOP 面向切面编程

    基本概念: AOP:Aspect Oriented Programming,即面向切面编程 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式 前置通知(@Before):在目标 ...

  2. 基于spring注解AOP的异常处理

    一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...

  3. spring 注解AOP

     aspectAnnotation的切面信息,加到了AnnotationAwareAspectJAutoProxyCreator的advisorsCache属性里面去了. 解析annotationSe ...

  4. 重新学习Spring注解——AOP

    面向切面编程——思想:在一个地方定义通用功能,但是可以通过声明的方式定义这个功能要以何种方式在何处运用,而无须修改受影响的类. 切面:横切关注点可以被模块化为特殊的类. 优点: 1.每个关注点都集中在 ...

  5. spring注解 aop

    @Resource(name="as")  为空按类型装配 @autowired 按类型 @quafiler (name="xx") 按名称 spring继承关 ...

  6. spring 注解aop调用invoke()

    public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlAp ...

  7. JAVAEE——spring02:使用注解配置spring、sts插件、junit整合测试和aop演示

    一.使用注解配置spring 1.步骤 1.1 导包4+2+spring-aop 1.2 为主配置文件引入新的命名空间(约束) 1.3 开启使用注解代替配置文件 1.4 在类中使用注解完成配置 2.将 ...

  8. Spring注解配置、Spring aop、整合Junit——Spring学习 day2

    注解配置: 1.为主配置文件引入新的命名空间(约束) preference中引入文件 2.开启使用注解代理配置文件 <?xml version="1.0" encoding= ...

  9. 一步一步深入spring(5)--使用基于注解的spring实现 AOP

    1.要利用spring aop,至少需要添加以下jar包 使用spring需要的jarspring.jar .commons-logging.jar 使用切面编程(AOP)需要的jar aspectj ...

随机推荐

  1. CTF丨2019互联网安全城市巡回赛·西安站,我们来了!

    万物互联时代,网信事业发展突飞猛进,互联网悄然渗透到国民生活的每一个角落,伴随而来的网络安全威胁和风险也日渐突出.网络诈骗.钓鱼软件.勒索病毒等安全问题层出不穷,信息泄露等网络安全事件也频繁上演,给用 ...

  2. DDD领域驱动设计理论篇 - 学习笔记

    一.Why DDD? 在加入X公司后,开始了ASP.NET Core+Docker+Linux的技术实践,也开始了微服务架构的实践.在微服务的学习中,有一本微软官方出品的<.NET微服务:容器化 ...

  3. 从css 3d说到空间坐标轴

    有一次我们说到掷骰子那个游戏,当时是用了一个steps属性+雪碧图来制作帧动画,这当然颇为不错,但其实一开始我想的不是这样的,我想的是用真的3d和动画去做,这个方案涉及到不少空间的知识,今天来给大伙好 ...

  4. DSAPI多功能.NET函数库组件

    DSAPI.dll不定期更新,增加功能,故无法每次都发到网上,如果需要获得最新版DSAPI.dll的,请到QQ群:419130936群共享里下载. 简介     dsapi.dll是一款基于.net平 ...

  5. 工厂方法模式(Factory Method Pattern)

    工厂方法模式概述 工厂方法模式是为了弥补简单工厂模式的不足并且继承它的优点而延生出的一种设计模式,属于GoF中的一种.它能更好的符合开闭原则的要求. 定义:定义了一个用于创建对象的接口,但是让子类决定 ...

  6. Android之webview详解

    文章大纲 一.webview基本介绍1.什么是webview2.为什么要使用webview3.webview基本操作 二.webview高级使用1.WebView状态2.资源加载3.WebView加载 ...

  7. [AI开发]将深度学习技术应用到实际项目

    本文介绍如何将基于深度学习的目标检测算法应用到具体的项目开发中,体现深度学习技术在实际生产中的价值,算是AI算法的一个落地实现.本文算法部分可以参见前面几篇博客: [AI开发]Python+Tenso ...

  8. 开放windows服务器端口-----以打开端口8080为例

    [转载]原文地址:https://blog.csdn.net/spt_dream/article/details/75014619 本文记录两个内容: 1.win7下打开端口 2.服务器(2003或者 ...

  9. Oracle ADDM报告生成和性能分析

    我写的SQL调优专栏:https://blog.csdn.net/u014427391/article/category/8679315 对于局部的,比如某个页面列表sql,我们可以使用Oracle的 ...

  10. CMake安装grpc生成gRPCTargets.cmake文件

    以下是安装语句: cd grpc_folder git submodule update --init cmake .. make -j 4 sudo make install 然而编写依赖gRPC的 ...