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
去掉原有的配置添加三行配置
<?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
测试结果