2019-03-10/20:19:56 演示:將xml配置方式改為註解方式 靜態以及動態代理推薦博客:https://blog.csdn.net/javazejian/article/details/56267036 junit單元測試jar包:https://share.weiyun.com/5p ...
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 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
去掉原有的配置添加三行配置
<context:component-scan base-package="aspect"/> 定義切麵類 <context:component-scan base-package="service"/> 定義業務類 <aop:aspectj-autoproxy/> 找到被註解了的切麵類,進行切麵配置 Spring為了支撐AOP運行,用到了動態代理這種設計模式,這句話的意思就是啟動對AOP的支持。 動態代理以及靜態代理的概念博主也是參考其他的資料在引用中提供了地址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 }TestSpringOldWay
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>applicationContext.xml
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 }Category.java
測試結果