Spring学习笔记(二)注解方式AOP 注解方式测试

注解方式AOP

昨天学到了AOP的配置,以核心业务和周边功能缠绕的形式分开写两部分代码。但是AOP的applicationContext.xml的配置还是挺繁琐的,那么今天就出现了! 用注解的方式写AOP,会简洁很多。

使用@Component("s") 注解ProductService 类,昨天的博客上也出现了它,不过是用xml自己配置的,这里直接使用注解方式简化了,它的作用能是模拟核心业务功能进行一定的业务啦。

package com.how2java.service;
 
import org.springframework.stereotype.Component;
 
@Component("s")
public class ProductService {
    public void doSomeService(){
        System.out.println("doSomeService");
    }
     
}

@Aspect 注解表示这是一个切面
@Component 表示这是一个bean,由Spring进行管理
@Around(value = "execution(* com.how2java.service.ProductService.*(..))") 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作

这里能就是对AOP内容进行用注解的方式操作啦。

package com.how2java.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(* com.how2java.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;
    }
}

然后在xml里面把昨天的注解信息全删掉,改成这三句话:

    
    <context:component-scan base-package="com.how2java.aspect"/>
    <context:component-scan base-package="com.how2java.service"/>
    <aop:aspectj-autoproxy/> 

前两句是扫描包,定位业务类和切面类,最后一句就是告诉spring我们要用注解的形式弄切面啦。

然后运行测试就可得和昨天相同的结果啦。

package com.how2java.test;
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.service.ProductService;
 
public class TestSpring {
  
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}

注解方式测试

站长这一部分也写得挺草率的。。我也就尽量理解一下。。

注解方式测试就是把之前的测试类,写成了注解的形式,让测试类变简单了。

修改TestSpring, 并运行
1. @RunWith(SpringJUnit4ClassRunner.class)
表示这是一个Spring的测试类

2. @ContextConfiguration("classpath:applicationContext.xml")
定位Spring的配置文件

3. @Autowired
给这个测试类装配Category对象

4. @Test
测试逻辑,打印c对象的名称

package com.how2java.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 com.how2java.pojo.Category;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
    @Autowired
    Category c;
 
    @Test
    public void test(){
        System.out.println(c.getName());
    }
}

学习网站:https://how2j.cn/p/4935

发布了82 篇原创文章 · 获赞 21 · 访问量 7937

猜你喜欢

转载自blog.csdn.net/qq_41658124/article/details/104858567