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
测试结果
