Skip to content

Commit

Permalink
Merge pull request #150 from linyimin0812/feat/20240428_async_bean_in…
Browse files Browse the repository at this point in the history
…it_support_spring_boot3_1

Feat/20240428 async bean init support spring boot3 1
  • Loading branch information
linyimin0812 committed Apr 27, 2024
2 parents f3bb76f + 388e8e2 commit 6dd178a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<template.directory>${user.home}/spring-startup-analyzer/template</template.directory>
<sonar.organization>linyimin-bupt</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<revision>3.0.0</revision>
<revision>3.1.0</revision>
<sonar.coverage.jacoco.xmlReportPaths>
${project.basedir}/report-aggregate/target/site/
jacoco-aggregate/jacoco.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.springframework.core.type.StandardMethodMetadata;
import org.springframework.util.ClassUtils;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author linyimin
Expand Down Expand Up @@ -52,7 +52,10 @@ private static String scanAsyncInitMethodOnClass(Class<?> beanClassType) {
List<Method> candidateMethods = new ArrayList<>();

for (Method method : beanClassType.getDeclaredMethods()) {
if (AnnotatedElementUtils.hasAnnotation(method, PostConstruct.class)) {

List<String> annotations= Arrays.stream(method.getAnnotations()).map(annotation -> annotation.annotationType().getName()).collect(Collectors.toList());

if (annotations.contains("javax.annotation.PostConstruct") || annotations.contains("jakarta.annotation.PostConstruct")) {
candidateMethods.add(method);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.linyimin0812.async.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;

/**
* @author linyimin
**/
public class InstantiationAwareBeanPostProcessorAdapter implements SmartInstantiationAwareBeanPostProcessor {
public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
return null;
}

public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
return null;
}

public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
return bean;
}

public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}

public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}

public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
return null;
}

public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
return pvs;
}

public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.linyimin0812.async.AsyncBeanAutoConfiguration
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.linyimin0812.profiler.common.ui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import io.github.linyimin0812.profiler.common.logger.LogFactory;
Expand Down Expand Up @@ -48,11 +48,11 @@ public static List<Statistics> getStatisticsList() {

public static String toJSONString() {
Map<String, String> map = new HashMap<>();
map.put("statisticsList", JSON.toJSONString(statisticsList));
map.put("beanInitResultList", JSON.toJSONString(beanInitResultList));
map.put("unusedJarMap", JSON.toJSONString(unusedJarMap));
map.put("statisticsList", JSON.toJSONString(statisticsList, JSONWriter.Feature.LargeObject));
map.put("beanInitResultList", JSON.toJSONString(beanInitResultList, JSONWriter.Feature.IgnoreNonFieldGetter, JSONWriter.Feature.LargeObject));
map.put("unusedJarMap", JSON.toJSONString(unusedJarMap, JSONWriter.Feature.LargeObject));

map.put("methodInvokeDetailList", JSON.toJSONString(calculateInvokeMetrics(), SerializerFeature.IgnoreNonFieldGetter));
map.put("methodInvokeDetailList", JSON.toJSONString(calculateInvokeMetrics(), JSONWriter.Feature.IgnoreNonFieldGetter, JSONWriter.Feature.LargeObject));

// fix Use JSONObject#toJSONString to serialize a Map. The Map contains a large string and OOM appears
return JSONObject.toJSONString(map, JSONWriter.Feature.LargeObject);
Expand All @@ -76,7 +76,7 @@ private static List<MethodInvokeMetrics> calculateInvokeMetrics() {
}
} catch (Exception ex) {
List<MethodInvokeDetail> copies = methodInvokeDetailList.stream().map(invokeDetail -> new MethodInvokeDetail(invokeDetail.getMethodQualifier(), invokeDetail.getStartMillis(), invokeDetail.getDuration())).collect(Collectors.toList());
logger.error(StartupVO.class, "calculateInvokeMetrics error. methodInvokeDetailList: {}", JSON.toJSONString(copies, SerializerFeature.IgnoreNonFieldGetter), ex);
logger.error(StartupVO.class, "calculateInvokeMetrics error. methodInvokeDetailList: {}", JSON.toJSONString(copies, JSONWriter.Feature.IgnoreNonFieldGetter, JSONWriter.Feature.LargeObject), ex);
}

return metricsList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.linyimin0812.profiler.extension.enhance.springbean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import io.github.linyimin0812.profiler.api.EventListener;
import io.github.linyimin0812.profiler.api.event.AtEnterEvent;
import io.github.linyimin0812.profiler.api.event.AtExitEvent;
Expand Down Expand Up @@ -116,7 +117,8 @@ public void stop() {
}

if (!remainInitResult.isEmpty()) {
logger.warn(BeanCreateListener.class, "profilerResultThreadLocal is not empty. There may be a problem with the initialization of the bean. {}", JSON.toJSONString(remainInitResult));
logger.warn(BeanCreateListener.class, "profilerResultThreadLocal is not empty. There may be a problem with the initialization of the bean. {}",
JSON.toJSONString(remainInitResult, JSONWriter.Feature.IgnoreNonFieldGetter, JSONWriter.Feature.LargeObject));
}
}
}

0 comments on commit 6dd178a

Please sign in to comment.