Java常用代码块、工具类、常用框架、mysql常用语句模版汇总

Idea常用插件

  • Build Generator:生成builder模式的代码
  • MybatisX:mapper.xml文件和dao之间关联、映射
  • Lombok
  • Maven Helper:分析maven冲突等
  • Json parser

Java后台常用框架

  • springboot
  • springmvc
  • mybatis -plus
  • mysql
  • maven
  • junit
  • jackson
  • guava
  • apache commons utils
  • netty
  • swagger
  • joda-time

切面示例

定义注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DistributedLockAnnotation {

    String lockUniqueKey();

    int lockTime();

    TimeUnit lockTimeUnit();
}

定义切面

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

// 切面定义
@Aspect
@Component
public class DistributedLockAspect {

    @Autowired
    private DistributedLock distributedLock;

    // 切入点
    @Pointcut("@annotation(xx.xx.DistributedLockAnnotation)")
    public void logPointCut() {
    }

    // 环绕通知
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object result;
        try {
            boolean locked = tryGetDistributeLock(point);
            if (!locked) {
                throw new RunTimeException("xx");
            }
            result = point.proceed();
        } catch (Throwable e) {
            throw e;
        }
        return result;
    }

    private boolean tryGetDistributeLock(ProceedingJoinPoint point) {
        try {
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();
            DistributedLock annotation = method.getAnnotation(DistributedLockAnnotation.class);
            String lockKey = SPELUtils.evalSPEl(point.getArgs(), method, annotation.uniqueKey());
            if (StringUtils.isBlank(lockKey)) {
                log.warn("xx, lockKey = " + lockKey);
                return false;
            }
            boolean locked = distributedLock.tryLock(lockKey, annotation.unit(), annotation.leaseTime());
            if (!locked) {
                log.warn("xx,, lockKey = " + lockKey + ", locked = " + locked);
            }
            return locked;
        } catch (Exception e) {
            log.error("", e);
        }
        return true;
    }
}

常见java启动、停止、监控脚本

启动脚本

start.sh:

#!/bin/bash
echo  "current path is : $(pwd)"
source  /etc/profile

# stop
sh /xx/xx/stop.sh

# 设置JAVA_HOME和CLASSPATH
export JAVA_HOME=/xx/xx/java
export BASE_DIR=/xx/xx/x

# 设置服务名称
SERVICE_NAME=your_service_namexx

JAVA_OPTS="${JAVA_OPTS} -server -Xms4g -Xmx4g -Xmn2048m"
JAVA_OPTS="${JAVA_OPTS} -XX:+UseG1GC -XX:G1HeapRegionSize=32m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
JAVA_OPTS="${JAVA_OPTS} -verbose:gc -Xloggc:${BASE_PATH}/gclogs/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy"
JAVA_OPTS="${JAVA_OPTS} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=100m"
JAVA_OPTS="${JAVA_OPTS} -XX:-OmitStackTraceInFastThrow"
JAVA_OPTS="${JAVA_OPTS} -XX:+AlwaysPreTouch"
JAVA_OPTS="${JAVA_OPTS} -XX:-UseLargePages -XX:-UseBiasedLocking"

# 启动服务
echo "start $SERVICE_NAME ..."
nohup $JAVA_HOME/bin/java -jar $JAVA_OPTS -Dserver.port=xx /BASE_DIR/xx.jar > /dev/null 

你可能感兴趣的:(Java,java,mysql,spring,spring,cloud,spring,boot,mybatis,guava)