Skip to content

Commit

Permalink
Merge pull request #10 from admin4j/dev
Browse files Browse the repository at this point in the history
feat(version): release v0.9.7
  • Loading branch information
andanyoung authored Mar 21, 2024
2 parents e9b3138 + ea4e7ea commit a767546
Show file tree
Hide file tree
Showing 112 changed files with 2,692 additions and 431 deletions.
2 changes: 1 addition & 1 deletion admin4j-common-spring-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.admin4j.common</groupId>
<artifactId>admin4j-common-spring-web</artifactId>
<version>0.9.5</version>
<version>0.9.7</version>
<description>与业务无关的工具类库</description>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,25 @@
import com.admin4j.common.constant.WebConstant;
import com.admin4j.common.service.IUserContextHolder;
import com.admin4j.common.service.impl.SimpleUserContextHolder;
import com.admin4j.common.util.UserContextUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
* @author andanyang
* @since 2023/9/15 9:19
*/
@Configuration
@AutoConfigureOrder(WebConstant.IUserContextHolderOrder + 6)
public class UserContextAutoConfiguration implements InitializingBean, ApplicationContextAware {
@AutoConfigureOrder(WebConstant.IUserContextHolderOrder)
public class UserContextAutoConfiguration {

private ApplicationContext applicationContext;

@Bean
@Lazy
@ConditionalOnMissingBean(IUserContextHolder.class)
@ConditionalOnClass(name = "com.alibaba.ttl.TransmittableThreadLocal")
public IUserContextHolder userContextHolder() {
return new SimpleUserContextHolder();
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

@Override
public void afterPropertiesSet() throws Exception {
UserContextUtil.userContextHolder = applicationContext.getBean(IUserContextHolder.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.admin4j.common.config;

import com.admin4j.common.constant.WebConstant;
import com.admin4j.common.service.IUserContextHolder;
import com.admin4j.common.util.UserContextUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
* @author andanyang
* @since 2023/9/15 9:19
*/
@Configuration
@AutoConfigureOrder(WebConstant.IUserContextHolderOrder + 8)
public class UserContextInitAutoConfiguration implements InitializingBean, ApplicationContextAware {

private ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

@Override
public void afterPropertiesSet() throws Exception {
String[] beanNamesForType = applicationContext.getBeanNamesForType(IUserContextHolder.class);
if (beanNamesForType.length > 0) {
UserContextUtil.userContextHolder = applicationContext.getBean(IUserContextHolder.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.admin4j.spring.util.SpringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
* @author andanyang
* @since 2023/3/13 14:51
*/
@RestControllerAdvice
public abstract class AbstractExceptionHandler {


public void publishGlobalExceptionEvent(Exception e) {
SpringUtils.getApplicationContext().publishEvent(new GlobalExceptionEvent("GlobalException", e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GlobalExceptionEvent {
/**
* 异常类型
*/
private String name;
private String name = "";
private Throwable e;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.admin4j.common.pojo.AuthenticationUser;
import com.admin4j.common.pojo.ResponseEnum;
import com.admin4j.common.service.IUserContextHolder;
import com.alibaba.ttl.TransmittableThreadLocal;
import org.apache.commons.lang3.ObjectUtils;

/**
Expand All @@ -18,7 +17,15 @@ public class SimpleUserContextHolder implements IUserContextHolder {
/**
* 支持父子线程之间的数据传递 THREAD_LOCAL_TENANT
*/
private final ThreadLocal<AuthenticationUser> THREAD_LOCAL_USER = new TransmittableThreadLocal<>();
protected final ThreadLocal<AuthenticationUser> THREAD_LOCAL_USER;

public SimpleUserContextHolder() {
THREAD_LOCAL_USER = new ThreadLocal<>();
}

public SimpleUserContextHolder(ThreadLocal<AuthenticationUser> threadLocal) {
THREAD_LOCAL_USER = threadLocal;
}

/**
* 当前会话注销登录
Expand Down Expand Up @@ -128,6 +135,7 @@ public Long getUserId() {
*/
@Override
public void setUserId(Long userId) {
// 如果需要切换其他资料信息字类需要实现 切换用户信息方法
getLoginUser().setUserId(userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.admin4j.common.service.impl;

import com.admin4j.common.service.IUserContextHolder;
import com.alibaba.ttl.TransmittableThreadLocal;

/**
* 使用ttl
* 当前登录用户上下文信息,可实现切换用户,切换租户。
*
* @author andanyang
* @since 2021/7/27 10:56
*/
public class TtlUserContextHolder extends SimpleUserContextHolder implements IUserContextHolder {

public TtlUserContextHolder() {
/**
* 支持父子线程之间的数据传递 THREAD_LOCAL_TENANT
*/
super(new TransmittableThreadLocal<>());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.admin4j.common.util;


import com.admin4j.common.exception.SystemException;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
Expand All @@ -11,7 +12,6 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
Expand Down Expand Up @@ -104,46 +104,51 @@ public static Map<String, String> getHeaders(HttpServletRequest request) {
return map;
}


/**
* 将字符串渲染到客户端
*
* @param response 渲染对象
* @param string 待渲染的字符串
* @return null
* @param object 渲染对象
* @param httpStatus http 状态
*/
public static String renderString(HttpServletResponse response, String string) {
try {
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
} catch (IOException e) {
e.printStackTrace();
}
return null;
public static void renderJson(Object object, int httpStatus) {
renderString(JSONObject.toJSONString(object), httpStatus);
}

public static void renderString(String str) {
renderString(str, 200);
}

public static void renderString(String str, int httpStatus) {

HttpServletResponse response = getResponse();
renderString(response, str, httpStatus);
}


/**
* 将字符串渲染到客户端
*
* @param object 渲染对象
* @return null
* @param response 渲染对象
* @param string 待渲染的字符串
* @param httpStatus http 状态
*/
public static String renderJson(Object object) {
public static void renderString(HttpServletResponse response, String string, int httpStatus) {
try {

HttpServletResponse response = getResponse();
response.setStatus(200);
if (response == null) {
return;
}
response.setStatus(httpStatus);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.write(JSONObject.toJSONString(object));
out.flush();
out.close();
response.getWriter().write(string);
// 通常您不应该关闭流。在 servlet 完成生命周期之后,servlet 容器会自动关闭流。
// 举个例子,如果你关闭了流的话,在你实现的 Filter 中就不能再使用了。
// out.flush();
// out.close();
} catch (IOException e) {
e.printStackTrace();
throw new SystemException(e);
}
return null;
}

/**
Expand All @@ -168,10 +173,7 @@ public static boolean isAjaxRequest(HttpServletRequest request) {
}

String ajax = request.getParameter("__ajax");
if (StringUtils.equalsAnyIgnoreCase(ajax, "json", "xml")) {
return true;
}
return false;
return StringUtils.equalsAnyIgnoreCase(ajax, "json", "xml");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.admin4j.common.config.UserContextAutoConfiguration
com.admin4j.common.config.UserContextAutoConfiguration, \
com.admin4j.common.config.UserContextInitAutoConfiguration

6 changes: 3 additions & 3 deletions admin4j-common-spring/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand All @@ -12,7 +12,7 @@
<groupId>com.admin4j.common</groupId>
<artifactId>admin4j-common-spring</artifactId>
<description>与业务无关的Spring工具类库</description>
<!-- <version>0.2.0</version>-->
<version>0.9.7</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.admin4j.spring.util;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
Expand All @@ -25,6 +26,7 @@ public class SpelUtil {
*/
private static final LocalVariableTableParameterNameDiscoverer U = new LocalVariableTableParameterNameDiscoverer();
// 使用SPEL进行key的解析
@Getter
private static final ExpressionParser EL_PARSER = new SpelExpressionParser();

private SpelUtil() {
Expand Down Expand Up @@ -57,7 +59,7 @@ public static String parse(String spel, Method method, Object[] args) {
try {
return EL_PARSER.parseExpression(spel).getValue(context, String.class);
} catch (ParseException | SpelEvaluationException e) {
return spel;
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.admin4j.spring.util;

import junit.framework.TestCase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.SpelParserConfiguration;

import java.lang.reflect.Method;

/**
* @author andanyang
* @since 2024/1/26 9:59
*/
public class SpelUtilTest extends TestCase {

@Autowired
SpelParserConfiguration spelParserConfiguration;

public void testParse() throws NoSuchMethodException {

// AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
// SpelParserConfiguration bean = applicationContext.getBean(SpelParserConfiguration.class);


System.out.println("spelParserConfiguration = " + spelParserConfiguration);
Method[] methods = SpelUtilTest.class.getMethods();
Method tMethod = SpelUtilTest.class.getDeclaredMethod("tMethod", String.class);
SpelUtilTest spelUtilTest = new SpelUtilTest();


String parse = SpelUtil.parse("'saleContract:'+#p1", tMethod, new Object[]{"param"});
System.out.println("parse = " + parse);
}

public String tMethod(String id) {

return "p0-12";
}

public void testTestParse() {
}
}
1 change: 1 addition & 0 deletions admin4j-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<groupId>com.admin4j.common</groupId>
<artifactId>admin4j-common</artifactId>
<packaging>jar</packaging>
<version>0.9.7</version>

<name>admin4j-common</name>
<description>admin4j工具包</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@
@Target(ElementType.FIELD)
public @interface ObjectCompareFiled {

/**
* 忽略比较
*
* @return
*/
boolean ignore() default false;

/**
* 目标为空时,是否忽略比较
*
* @return
*/
boolean ignoreTargetNull() default false;

String value() default "";
}
Loading

0 comments on commit a767546

Please sign in to comment.