经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Spring注解AOP及单元测试junit(6)
来源:cnblogs  作者:BenCoper  时间:2019/3/11 9:11:04  对本文有异议

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 

  1. 1 package service;
  2. 2
  3. 3 import org.springframework.stereotype.Component;
  4. 4
  5. 5 @Component("s")
  6. 6 public class ProductService {
  7. 7 public void doSomeService(){
  8. 8 System.out.println("doSomeService");
  9. 9 }
  10. 10
  11. 11 }

 

2.注解配置切面AOP

  @Aspect 注解表示这是一个切面
  @Component 表示这是一个bean,由Spring进行管理
  在切面类的具体的方法前加上一句,表示这个切点被触发的时候,执行该函数,用Around方式,相当于把这个切点和这个切点的处理方法关联起来。

  @Around(value = "execution(* service.ProductService.*(..))") 表示对service.ProductService 这个类中的所有方法进行切面操作.

  含义就是,当expression中的函数被调用时,就会用around形式来触发切面函数,这条语句放在谁前面,谁就被定义为切面函数,也就是辅助功能。 

  1. 1 package aspect;
  2. 2 import org.aspectj.lang.ProceedingJoinPoint;
  3. 3 import org.aspectj.lang.annotation.Around;
  4. 4 import org.aspectj.lang.annotation.Aspect;
  5. 5 import org.springframework.stereotype.Component;
  6. 6
  7. 7 @Aspect
  8. 8 @Component
  9. 9 public class LoggerAspect {
  10. 10
  11. 11 @Around(value = "execution(* service.ProductService.*(..))")
  12. 12 public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
  13. 13 System.out.println("start log:" + joinPoint.getSignature().getName());
  14. 14 Object object = joinPoint.proceed();
  15. 15 System.out.println("end log:" + joinPoint.getSignature().getName());
  16. 16 return object;
  17. 17 }
  18. 18 }

 

3.配置文件applicationContext.xml

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

<context:component-scan base-package="aspect"/>      定义切面类
<context:component-scan base-package="service"/>     定义业务类
 
<aop:aspectj-autoproxy/>       找到被注解了的切面类,进行切面配置
   Spring为了支撑AOP运行,用到了动态代理这种设计模式,这句话的意思就是启动对AOP的支持。
 动态代理以及静态代理的概念博主也是参考其他的资料在引用中提供了地址
 
  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <beans xmlns="http://www.springframework.org/schema/beans"
  3. 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. 4 xmlns:aop="http://www.springframework.org/schema/aop"
  5. 5 xmlns:tx="http://www.springframework.org/schema/tx"
  6. 6 xmlns:context="http://www.springframework.org/schema/context"
  7. 7 xsi:schemaLocation="
  8. 8 http://www.springframework.org/schema/beans
  9. 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. 10 http://www.springframework.org/schema/aop
  11. 11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. 12 http://www.springframework.org/schema/tx
  13. 13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14. 14 http://www.springframework.org/schema/context
  15. 15 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16. 16
  17. 17 <context:component-scan base-package="aspect"/>
  18. 18 <context:component-scan base-package="service"/>
  19. 19 <aop:aspectj-autoproxy/>
  20. 20
  21. 21 </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 

  1. 1 package test;
  2. 2
  3. 3 import org.junit.Test;
  4. 4 import org.junit.runner.RunWith;
  5. 5 import org.springframework.beans.factory.annotation.Autowired;
  6. 6 import org.springframework.test.context.ContextConfiguration;
  7. 7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8. 8
  9. 9 import pojo.Category;
  10. 10
  11. 11 @RunWith(SpringJUnit4ClassRunner.class)
  12. 12 @ContextConfiguration("classpath:applicationContext.xml")
  13. 13 public class TestSpring {
  14. 14 @Autowired
  15. 15 Category c;
  16. 16
  17. 17 @Test
  18. 18 public void test(){
  19. 19 System.out.println(c.getName());
  20. 20 }
  21. 21 }

 

  1. 1 package com.how2java.test;
  2. 2
  3. 3 import org.springframework.context.ApplicationContext;
  4. 4
  5. 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. 6
  7. 7 import pojo.Category;
  8. 8
  9. 9 public class TestSpringOldWay {
  10. 10
  11. 11 public static void main(String[] args) {
  12. 12 ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
  13. 13
  14. 14 Category c = (Category) context.getBean("c");
  15. 15
  16. 16 System.out.println(c.getName());
  17. 17 }
  18. 18 }
TestSpringOldWay
  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <beans xmlns="http://www.springframework.org/schema/beans"
  3. 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. 4 xmlns:aop="http://www.springframework.org/schema/aop"
  5. 5 xmlns:tx="http://www.springframework.org/schema/tx"
  6. 6 xmlns:context="http://www.springframework.org/schema/context"
  7. 7 xsi:schemaLocation="
  8. 8 http://www.springframework.org/schema/beans
  9. 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. 10 http://www.springframework.org/schema/aop
  11. 11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. 12 http://www.springframework.org/schema/tx
  13. 13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14. 14 http://www.springframework.org/schema/context
  15. 15 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16. 16
  17. 17 <bean name="c" class="pojo.Category">
  18. 18 <property name="name" value="category 1" />
  19. 19 </bean>
  20. 20
  21. 21 </beans>
applicationContext.xml
  1. 1 package pojo;
  2. 2
  3. 3 public class Category {
  4. 4 private int id;
  5. 5 private String name;
  6. 6
  7. 7 public int getId() {
  8. 8 return id;
  9. 9 }
  10. 10 public void setId(int id) {
  11. 11 this.id = id;
  12. 12 }
  13. 13 public String getName() {
  14. 14 return name;
  15. 15 }
  16. 16 public void setName(String name) {
  17. 17 this.name = name;
  18. 18 }
  19. 19
  20. 20 }
Category.java

 测试结果

 

 

 

 

原文链接:http://www.cnblogs.com/bencoper/p/10507146.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号