聚富彩票源码下载

聚富彩票源码下载

地址一:【hubawl.com】
地址二:【bbscherry.com】

聚富彩票源码下载

自定义注解

package com.xiaojukeji.common.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.METHOD })br/>@Retention(RetentionPolicy.RUNTIME)
public @interface SystemLogAnnotation {
String value();
}

(1)@Target注解是标注这个类它可以标注的位置,常用的元素类型(ElementType):

public enum ElementType {
/* Class, interface (including annotation type), or enum declaration /
// TYPE类型可以声明在类上或枚举上或者是注解上
TYPE,

/** Field declaration (includes enum constants) */ 
// FIELD声明在字段上 
FIELD, 

/** Method declaration */ 
// 声明在方法上 
METHOD, 

/** Formal parameter declaration */ 
// 声明在形参列表中 
PARAMETER, 

/** Constructor declaration */ 
// 声明在构造方法上 
CONSTRUCTOR, 

/** Local variable declaration */ 
// 声明在局部变量上 
LOCAL_VARIABLE, 

/** Annotation type declaration */ 
ANNOTATION_TYPE, 

/** Package declaration */ 
PACKAGE, 

/** * Type parameter declaration * * @since 1.8 */ 
TYPE_PARAMETER, 

/** * Use of a type * * @since 1.8 */ 
TYPE_USE

}

(2)@Retention注解表示的是本注解(标注这个注解的注解保留时期)

public enum RetentionPolicy {
/**

  • Annotations are to be discarded by the compiler.
    */
    // 源代码时期
    SOURCE,

    /**

  • Annotations are to be recorded in the class file by the compiler
  • but need not be retained by the VM at run time. This is the default
  • behavior.
    */
    // 字节码时期, 编译之后
    CLASS,

    /**

  • Annotations are to be recorded in the class file by the compiler and
  • retained by the VM at run time, so they may be read reflectively.
  • @see java.lang.reflect.AnnotatedElement
    */
    // 运行时期, 也就是一直保留, 通常也都用这个
    RUNTIME
    }

(3)@Documented是否生成文档的标注, 也就是生成接口文档是, 是否生成注解文档

二.AOP切面类---此处我用后置通知

package com.xiaojukeji.ecm.aop;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import nl.bitwalker.useragentutils.UserAgent;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.xiaojukeji.common.annotation.SystemLogAnnotation;
import com.xiaojukeji.dao.model.SystemLog;
import com.xiaojukeji.ep.ip.common.model.AdUser;
import com.xiaojukeji.ep.ip.common.utils.ContextHolder;
import com.xiaojukeji.service.ERPService;
import com.xiaojukeji.service.SystemLogService;
/**

package com.xiaojukeji.ecm.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import com.xiaojukeji.common.annotation.SystemLogAnnotation;
import com.xiaojukeji.dao.model.Terminal;
import com.xiaojukeji.service.TerminalService;

/**

四.启动项目,请求此controller里的导出方法,在此方法的return前执行后置操作,既记录日志。

五.注解讲解:

类注解:

@Aspect将一个类定义为一个切面类
@order(i)标记切面类的处理优先级,i值越小,优先级别越高.PS:可以注解类,也能注解到方法上

方法注解:

@Pointcut定义一个方法为切点里面的内容为一个表达式,下面详细介绍
@Before 在切点前执行方法,内容为指定的切点
@After 在切点后,return前执行,
br/>@AfterReturning在切入点,return后执行,如果想对某些方法的返回参数进行处理,可以在这操作
@Around 环绕切点,在进入切点前,跟切点后执行
@AfterThrowing 在切点后抛出异常进行处理
@order(i) 标记切点的优先级,i越小,优先级越高

@Pointcut注解组合使用:

上面代码中,我们定义了一个切点,该切点只进行处理指定路径的:

@Pointcut("execution(public com.example.DemoApplication.(..))")
private void controllerAspect(){}
现在,我们在定义一个处理其他路径下的切点:

@Pointcut("execution(public com.demo..*(..))")
private void controllerDemo(){}
以上切点,都是分别处理不同的内容,如果我们需要一个切点来处理他们两者,我们可以这么配置:

@Pointcut(value = "controllerAspect() || controllerDemo()")
private void all(){}
在@Pointcut注解内,直接引用其它被@Pointcut注解过的方法名称,这样,该切点就可以处理两个路径下的方法

在多个execution表达式之间使用 ||,or表示 或,使用 &&,and表示 与,!表示 非.

execution( com.travelsky.ccboy.dao...find(..)) || execution( com.travelsky.ccboy.dao...query(..))

@Pointcut注解中的execution表达式: public com.demo..*(..)

第一个 public 表示方法的修饰符,可以用代替
第一个
表示 返回值,代表所有
com.demo.
包路径,.表示路径下的所有包
第三个.
表示路径下,所有包下的所有类的方法
(..) 表示不限方法参数

关于@order(i)注解的一些注意事项:

注解类,i值是,值越小,优先级越高
注解方法,分两种情况
注解的是 @Before 是i值越小,优先级越高
注解的是 @After或者@AfterReturning 中,i值越大,优先级越高

总结两者的概括就是:
在切入点前的操作,按order的值由小到大执行
在切入点后的操作,按order的值由大到小执行

猜你喜欢

转载自blog.51cto.com/13842261/2133787