Spring注解AOP及单元测试junit(6)
2019-03-10/20:19:56
演示:将xml配置方式改为注解方式
静态以及动态代理推荐博客:
junit单元测试jar包:https://share.weiyun.com/5pkuxvl
1.注解配置业务类
使用@component("s") 注解productservice 类表示业务类的bean名字为 s
1 package service; 2 3 import org.springframework.stereotype.component; 4 5 @component("s") 6 public class productservice { 7 public void dosomeservice(){ 8 system.out.println("dosomeservice"); 9 } 10 11 }
2.注解配置切面aop
@aspect 注解表示这是一个切面
@component 表示这是一个bean,由spring进行管理
在切面类的具体的方法前加上一句,表示这个切点被触发的时候,执行该函数,用around方式,相当于把这个切点和这个切点的处理方法关联起来。
@around(value = "execution(* service.productservice.*(..))") 表示对service.productservice 这个类中的所有方法进行切面操作.
含义就是,当expression中的函数被调用时,就会用around形式来触发切面函数,这条语句放在谁前面,谁就被定义为切面函数,也就是辅助功能。
1 package aspect; 2 import org.aspectj.lang.proceedingjoinpoint; 3 import org.aspectj.lang.annotation.around; 4 import org.aspectj.lang.annotation.aspect; 5 import org.springframework.stereotype.component; 6 7 @aspect 8 @component 9 public class loggeraspect { 10 11 @around(value = "execution(* service.productservice.*(..))") 12 public object log(proceedingjoinpoint joinpoint) throws throwable { 13 system.out.println("start log:" + joinpoint.getsignature().getname()); 14 object object = joinpoint.proceed(); 15 system.out.println("end log:" + joinpoint.getsignature().getname()); 16 return object; 17 } 18 }
3.配置文件applicationcontext.xml
去掉原有的配置添加三行配置
1 <?xml version="1.0" encoding="utf-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemalocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 14 http://www.springframework.org/schema/context 15 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 16 17 <context:component-scan base-package="aspect"/> 18 <context:component-scan base-package="service"/> 19 <aop:aspectj-autoproxy/> 20 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 package test; 2 3 import org.junit.test; 4 import org.junit.runner.runwith; 5 import org.springframework.beans.factory.annotation.autowired; 6 import org.springframework.test.context.contextconfiguration; 7 import org.springframework.test.context.junit4.springjunit4classrunner; 8 9 import pojo.category; 10 11 @runwith(springjunit4classrunner.class) 12 @contextconfiguration("classpath:applicationcontext.xml") 13 public class testspring { 14 @autowired 15 category c; 16 17 @test 18 public void test(){ 19 system.out.println(c.getname()); 20 } 21 }
1 package com.how2java.test; 2 3 import org.springframework.context.applicationcontext; 4 5 import org.springframework.context.support.classpathxmlapplicationcontext; 6 7 import pojo.category; 8 9 public class testspringoldway { 10 11 public static void main(string[] args) { 12 applicationcontext context = new classpathxmlapplicationcontext(new string[] { "applicationcontext.xml" }); 13 14 category c = (category) context.getbean("c"); 15 16 system.out.println(c.getname()); 17 } 18 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemalocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 14 http://www.springframework.org/schema/context 15 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 16 17 <bean name="c" class="pojo.category"> 18 <property name="name" value="category 1" /> 19 </bean> 20 21 </beans>
1 package pojo; 2 3 public class category { 4 private int id; 5 private string name; 6 7 public int getid() { 8 return id; 9 } 10 public void setid(int id) { 11 this.id = id; 12 } 13 public string getname() { 14 return name; 15 } 16 public void setname(string name) { 17 this.name = name; 18 } 19 20 }
测试结果
上一篇: Android中资源的引用
下一篇: MangoDB环境配置