发表于: 2017-08-28 23:45:06

1 962


今天完成的任务

学习了AOP和注解AOP



1.LoggerAspect类

@Aspect //注解表示这是一个切面
@Component //表示这是一个bean,由Spring进行管理
public class LoggerAspect {

@Around(value = "execution(* com.how2java.service.ProductService.*(..))")//对这个类中的所有方法进行切面操作
  public Object log(ProceedingJoinPoint joinPoint) throws Throwable {//声明一个ProceedingJoinPoint的joinPoint
     System.out.println("start log:" + joinPoint.getSignature().getName());
     Object object = joinPoint.proceed();//将来与某个核心功能编织之后,用于执行核心功能的代码
     System.out.println("end log:" + joinPoint.getSignature().getName());
     return object;
  }
}

2.ProductService类


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


}



其实就是通过xml中的配置,在LoggerAspect类调用ProductService类

有两张方法


1.通过设置config


<aop:config> <!--    声明别名     -->
   <aop:pointcut id="loggerCutpoint"
                 expression=
                         "execution(* com.how2java.service.ProductService.*(..)) "/>
   <!-- public 是指定public的方法,也可以不写直接:execution(* cn.dao.IUserDAO.*(..)
       *   是任意返回值,可以有返回值,也可以是void没有返回值的方法
       cn.dao.IUserDAO.*  是指定目录下的指定类任意方法
       cn.dao.IUserDAO.insert* 是指定目录下的指定类insert开头的任意方法
       cn.dao.IUserDAO.*.*  是指定目录下的任意类下的任意方法
       cn.dao..*.*  是指定目录下的任意目录下任意类下的任意方法
       (..) 是任何参数,可以是没有参数        -->
   <aop:aspect id="logAspect" ref="loggerAspect">   <!--   命名并且使用loggerAspect调用loggerCutpoint 中的log方法 -->
       <aop:around pointcut-ref="loggerCutpoint" method="log"/>
   </aop:aspect>
</aop:config>





2.通过标签

<!--   扫描包com.how2java.aspect和com.how2java.service,定位业务类和切面类      -->
<context:component-scan base-package="com.how2java.aspect"/>
<context:component-scan base-package="com.how2java.service"/>
<aop:aspectj-autoproxy/>    <!--    找到被注解了的切面类,进行切面配置      -->


学习了junit


1.TestSpring

 
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)//表示这是一个Spring的测试类
@ContextConfiguration("classpath:applicationContext.xml")//定位Spring的配置文件
public class TestSpring {//运行这个的tun也只打印test方法
   @Autowired//给这个测试类装配Category对象
   Category c;

   @Test//测试逻辑,打印c对象的名称
   public void test(){
System.out.println(c.getName()+"测试逻辑,打印c对象的名称 ");
   }


public void test1(){
System.out.println("我是test1 ");//如果不加@test可以显现run图标,但是还是无法运行
   }
}

这是测试的类,通过加标签@Test调用类的方法

我把笔记都记到备注里了,就偷懒一下了


明天的任务


接着写


遇到的问题



收获没啥收获





返回列表 返回列表
评论

    分享到