java注解参数_获取注解里面的参数

package com.sumavision.bnadmin.util;

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Inherited;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

*

* 关于Action的说明

*

* 类的方法描述注解

*

* @author gavin

* @version 1.0

* @since BN-Admin 1.0

*

*/

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Inherited

public @interface LogAction {

public String servicename() default "no servicename";

public String methodname() default "no methodname";

public String description() default "no description";

}

-----------------------------------------华丽的分割线--------------------------------------------

package com.sumavision.bnadmin.util;

import java.lang.reflect.Method;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.struts2.ServletActionContext;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import com.sumavision.bnadmin.admin.service.UseroperateService;

import com.sumavision.bnadmin.domain.entity.BnUseroperate;

/**

*

* 关于LogAspect的说明

*

* 服务日志

*

* @author gavin

* @version 1.0

* @since BN-Admin 1.0

*

*/

@Aspect

public class LogAspect {

/**

*

* 属性useroperateService

*

* 服务日志维护服务

*/

@Resource

private UseroperateService useroperateService;

public UseroperateService getUseroperateService() {

return useroperateService;

}

public void setUseroperateService(UseroperateService useroperateService) {

this.useroperateService = useroperateService;

}

private Log logger = LogFactory.getLog(LogAspect.class);

@Around("execution(* com.sumavision.bnadmin.admin.web..*(..))")

public Object doSystemLog(ProceedingJoinPoint point) throws Throwable {

Object retVal = point.proceed();

String methodName = point.getSignature().getName();

// 目标方法不为空

if (StringUtils.isNotEmpty(methodName)) {

// set与get方法除外

if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {

Class targetClass = point.getTarget().getClass();

Method method = targetClass.getMethod(methodName);

if (method != null) {

boolean hasAnnotation = method

.isAnnotationPresent(LogAction.class);

if (hasAnnotation) {

LogAction annotation = method.getAnnotation(LogAction.class);

String servicename = annotation.servicename();

String methodname = annotation.methodname();

String description = annotation.description();

if (logger.isDebugEnabled()) {

logger.debug("Action method:" + method.getName()

+ " Description:" + description);

}

// 取到当前的操作用户

String j_username = (String) ServletActionContext

.getServletContext().getAttribute("j_username");

if (j_username != null) {

try {//调用服务将信息记录到数据库

BnUseroperate useroperate = new BnUseroperate();

useroperate.setUsername(j_username);

useroperate.setServicemethodname(methodname);

useroperate.setServicename(servicename);

useroperate.setLastedittime(new Date());

useroperate.setDescription(description);

useroperateService.addUseroperate(useroperate);

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

}

}

return retVal;

}

}

-----------------------------------------优雅的分割线--------------------------------------------

@LogAction(servicename="登录",methodname="login",description="管理员登录")

public String login() {

ServletActionContext.getServletContext().setAttribute("j_username", j_username);

try {

String encodePassword = passwordEncoder.encodePassword(j_password, null);

boolean login_error = userService.login(j_username, encodePassword);

ServletActionContext.getRequest().setAttribute("login_error", login_error);

if (false == login_error) {

return ERROR;

}

} catch (Exception e) {

e.printStackTrace();

return ERROR;

}

return SUCCESS;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值