Skip to content

Commit

Permalink
Merge pull request #59 from mercyblitz/dev
Browse files Browse the repository at this point in the history
Sync Dev
  • Loading branch information
mercyblitz authored Jan 6, 2025
2 parents 7af897f + 3e77d19 commit a10ff2c
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.ObjectUtils;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand All @@ -44,9 +43,6 @@
import static io.microsphere.spring.context.ApplicationContextUtils.asConfigurableApplicationContext;
import static io.microsphere.spring.context.ApplicationContextUtils.getApplicationContextAwareProcessor;
import static io.microsphere.util.ArrayUtils.isEmpty;
import static io.microsphere.util.ClassLoaderUtils.getClassLoader;
import static io.microsphere.util.ClassLoaderUtils.isPresent;
import static io.microsphere.util.ClassLoaderUtils.resolveClass;
import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
Expand All @@ -69,10 +65,6 @@ public abstract class BeanUtils extends BaseUtils {

private static final Logger logger = LoggerFactory.getLogger(BeanUtils.class);

private static final String[] EMPTY_BEAN_NAMES = new String[0];

private static final Class<?> APPLICATION_STARTUP_CLASS = resolveClass("org.springframework.core.metrics.ApplicationStartup");

/**
* Is Bean Present or not?
*
Expand All @@ -94,7 +86,7 @@ public static boolean isBeanPresent(ListableBeanFactory beanFactory, Class<?> be
*/
public static boolean isBeanPresent(ListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors) {
String[] beanNames = getBeanNames(beanFactory, beanClass, includingAncestors);
return !ObjectUtils.isEmpty(beanNames);
return !isEmpty(beanNames);
}

/**
Expand All @@ -117,19 +109,17 @@ public static boolean isBeanPresent(ListableBeanFactory beanFactory, String bean
* @return If present , return <code>true</code> , or <code>false</code>
*/
public static boolean isBeanPresent(ListableBeanFactory beanFactory, String beanClassName, boolean includingAncestors) {
boolean present = false;
ClassLoader classLoader = null;
if (beanFactory instanceof ConfigurableBeanFactory) {
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
classLoader = configurableBeanFactory.getBeanClassLoader();
} else {
classLoader = getClassLoader(beanFactory.getClass());
}
if (isPresent(beanClassName, classLoader)) {
Class beanClass = resolveClassName(beanClassName, classLoader);
present = isBeanPresent(beanFactory, beanClass, includingAncestors);

Class beanClass = resolveClassName(beanClassName, classLoader);
if (beanClass == null) {
return false;
}
return present;
return isBeanPresent(beanFactory, beanClass, includingAncestors);
}


Expand Down Expand Up @@ -233,8 +223,8 @@ public static Class<?> resolveBeanType(String beanClassName, ClassLoader classLo
public static <T> T getOptionalBean(ListableBeanFactory beanFactory, Class<T> beanClass, boolean includingAncestors) throws BeansException {
String[] beanNames = getBeanNames(beanFactory, beanClass, includingAncestors);
if (isEmpty(beanNames)) {
if (logger.isDebugEnabled()) {
logger.debug("The bean [ class : " + beanClass.getName() + " ] can't be found ");
if (logger.isTraceEnabled()) {
logger.trace("The bean [ class : " + beanClass.getName() + " ] can't be found ");
}
return null;
}
Expand Down Expand Up @@ -281,8 +271,8 @@ public static <T> T getBeanIfAvailable(BeanFactory beanFactory, String beanName,
return beanFactory.getBean(beanName, beanType);
}

if (logger.isDebugEnabled()) {
logger.debug(format("The bean[name : %s , type : %s] can't be found in Spring BeanFactory", beanName, beanType.getName()));
if (logger.isTraceEnabled()) {
logger.trace(format("The bean[name : %s , type : %s] can't be found in Spring BeanFactory", beanName, beanType.getName()));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void resolve(Field field, ConfigurableListableBeanFactory beanFactory, Se
public void resolve(Method method, ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) {
int parametersCount = method.getParameterCount();
if (parametersCount < 1) {
logger.debug("The no-argument method[{}] will be ignored", method);
logger.trace("The no-argument method[{}] will be ignored", method);
return;
}
Parameter[] parameters = method.getParameters();
Expand All @@ -82,7 +82,7 @@ public void resolve(Method method, ConfigurableListableBeanFactory beanFactory,
public void resolve(Constructor constructor, ConfigurableListableBeanFactory beanFactory, Set<String> dependentBeanNames) {
int parametersCount = constructor.getParameterCount();
if (parametersCount < 1) {
logger.debug("The no-argument constructor[{}] will be ignored", constructor);
logger.trace("The no-argument constructor[{}] will be ignored", constructor);
return;
}
Parameter[] parameters = constructor.getParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package io.microsphere.spring.beans.factory;

import io.microsphere.util.ArrayUtils;
import io.microsphere.util.BaseUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.HierarchicalBeanFactory;
Expand All @@ -37,6 +38,7 @@

import static io.microsphere.reflect.FieldUtils.getFieldValue;
import static io.microsphere.util.ArrayUtils.of;
import static io.microsphere.util.ArrayUtils.size;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableList;
Expand Down Expand Up @@ -65,7 +67,6 @@ public abstract class BeanFactoryUtils extends BaseUtils {
* @return A bean if present , or <code>null</code>
*/
public static <T> T getOptionalBean(ListableBeanFactory beanFactory, String beanName, Class<T> beanType) {

if (!hasText(beanName)) {
return null;
}
Expand All @@ -88,21 +89,19 @@ public static <T> T getOptionalBean(ListableBeanFactory beanFactory, String bean
* @return the read-only and non-null {@link List} of Bean names
*/
public static <T> List<T> getBeans(ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType) {

if (isEmpty(beanNames)) {
int size = size(beanNames);
if (size < 1) {
return emptyList();
}

String[] allBeanNames = beanNamesForTypeIncludingAncestors(beanFactory, beanType, true, false);

List<T> beans = new ArrayList<T>(beanNames.length);

for (String beanName : beanNames) {
List<T> beans = new ArrayList<T>(size);
for (int i = 0; i < size; i++) {
String beanName = beanNames[i];
if (containsElement(allBeanNames, beanName)) {
beans.add(beanFactory.getBean(beanName, beanType));
}
}

return unmodifiableList(beans);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ private void preProcessLoadBeanClass(String beanName, RootBeanDefinition beanDef
executorService.execute(() -> {
Class beanClass = loadClass(beanClassName, classLoader, true);
beanDefinition.setBeanClass(beanClass);
if (logger.isDebugEnabled()) {
logger.debug("The bean[name : '{}'] class[name : '{}'] was loaded", beanName, beanClassName);
if (logger.isTraceEnabled()) {
logger.trace("The bean[name : '{}'] class[name : '{}'] was loaded", beanName, beanClassName);
}
});
}
Expand Down Expand Up @@ -259,7 +259,7 @@ private void flattenDependentBeanNamesMap(Map<String, Set<String>> dependentBean

for (String nonRootBeanName : nonRootBeanNames) {
if (dependentBeanNamesMap.remove(nonRootBeanName) != null) {
logger.debug("Non Root Bean name was removed : {}", nonRootBeanName);
logger.trace("Non Root Bean name was removed : {}", nonRootBeanName);
}
}

Expand All @@ -269,9 +269,9 @@ private void flattenDependentBeanNamesMap(Map<String, Set<String>> dependentBean
}

private void logDependentBeanNames(Map<String, Set<String>> dependentBeanNamesMap) {
if (logger.isDebugEnabled()) {
if (logger.isTraceEnabled()) {
for (Map.Entry<String, Set<String>> entry : dependentBeanNamesMap.entrySet()) {
logger.debug("The bean : '{}' <- bean dependencies : {}", entry.getKey(), entry.getValue());
logger.trace("The bean : '{}' <- bean dependencies : {}", entry.getKey(), entry.getValue());
}
}
}
Expand Down Expand Up @@ -332,7 +332,7 @@ private void resolveInjectionPointsDependentBeanNames(String beanName, RootBeanD
boolean isInterfaceBean = beanClass.isInterface();

if (isInterfaceBean) {
logger.debug("The resolved type of BeanDefinition : {}", beanClass.getName());
logger.trace("The resolved type of BeanDefinition : {}", beanClass.getName());
return;
}

Expand All @@ -348,8 +348,8 @@ private void resolveMethodParametersDependentBeanNames(String beanName, Class be
do {
doWithLocalMethods(targetClass, method -> {
if (isStatic(method)) {
if (logger.isDebugEnabled()) {
logger.debug("The Injection Point[bean : '{}' , class : {}] is not supported on static method : {}", beanName, method.getDeclaringClass().getName(), method);
if (logger.isTraceEnabled()) {
logger.trace("The Injection Point[bean : '{}' , class : {}] is not supported on static method : {}", beanName, method.getDeclaringClass().getName(), method);
}
return;
}
Expand All @@ -363,7 +363,7 @@ private void resolveMethodParametersDependentBeanNames(String beanName, Class be

if (method.equals(getMostSpecificMethod(method, beanClass))) {
if (isBeanMemberResolved(method)) {
logger.debug("The beans'[name : '{}'] method has been resolved : {}", beanName, method);
logger.trace("The beans'[name : '{}'] method has been resolved : {}", beanName, method);
} else {
resolvers.resolve(method, beanFactory, dependentBeanNames);
addResolvedBeanMember(method);
Expand All @@ -383,13 +383,13 @@ private void resolveFieldDependentBeanNames(String beanName, Class beanClass, De
do {
doWithLocalFields(targetClass, field -> {
if (isStatic(field)) {
if (logger.isDebugEnabled()) {
logger.debug("The Injection Point[bean : '{}' , class : {}] is not supported on static field : {}", beanName, field.getDeclaringClass().getName(), field);
if (logger.isTraceEnabled()) {
logger.trace("The Injection Point[bean : '{}' , class : {}] is not supported on static field : {}", beanName, field.getDeclaringClass().getName(), field);
}
return;
}
if (isBeanMemberResolved(field)) {
logger.debug("The beans'[name : '{}'] field has been resolved : {}", beanName, field);
logger.trace("The beans'[name : '{}'] field has been resolved : {}", beanName, field);
} else {
resolvers.resolve(field, beanFactory, dependentBeanNames);
addResolvedBeanMember(field);
Expand All @@ -410,7 +410,7 @@ private void removeReadyBeanNames(Set<String> dependentBeanNames, DefaultListabl
while (iterator.hasNext()) {
String dependentBeanName = iterator.next();
if (isBeanReady(dependentBeanName, beanFactory)) {
logger.debug("The dependent bean name['{}'] is removed since it's ready!", dependentBeanName);
logger.trace("The dependent bean name['{}'] is removed since it's ready!", dependentBeanName);
iterator.remove();
}
}
Expand Down Expand Up @@ -481,15 +481,15 @@ private void resolveConstructionParametersDependentBeanNames(String beanName, Ro
} else {
Constructor constructor = constructors[0];
if (isBeanMemberResolved(constructor)) {
logger.debug("The beans'[name : '{}'] constructor has been resolved : {}", beanName, constructor);
logger.trace("The beans'[name : '{}'] constructor has been resolved : {}", beanName, constructor);
} else {
resolvers.resolve(constructor, beanFactory, dependentBeanNames);
addResolvedBeanMember(constructor);
}
}
} else { // the @Bean or customized Method Definition
if (isBeanMemberResolved(factoryMethod)) {
logger.debug("The beans'[name : '{}'] factory-method has been resolved : {}", beanName, factoryMethod);
logger.trace("The beans'[name : '{}'] factory-method has been resolved : {}", beanName, factoryMethod);
} else {
resolvers.resolve(factoryMethod, beanFactory, dependentBeanNames);
addResolvedBeanMember(factoryMethod);
Expand Down Expand Up @@ -547,7 +547,7 @@ private Map<String, RootBeanDefinition> getEligibleBeanDefinitionsMap(DefaultLis
continue;
}
if (beanFactory.isCurrentlyInCreation(beanName)) {
logger.debug("The Bean[name : '{}'] is creating currently", beanName);
logger.trace("The Bean[name : '{}'] is creating currently", beanName);
continue;
}

Expand Down Expand Up @@ -594,8 +594,8 @@ private RootBeanDefinition getEligibleBeanDefinition(BeanDefinition beanDefiniti

private boolean isBeanReady(String beanName, DefaultListableBeanFactory beanFactory) {
boolean ready = beanFactory.containsSingleton(beanName);
if (ready && logger.isDebugEnabled()) {
logger.debug("The Bean[name : '{}'] is ready in the BeanFactory[id : '{}']", beanName, beanFactory.getSerializationId());
if (ready && logger.isTraceEnabled()) {
logger.trace("The Bean[name : '{}'] is ready in the BeanFactory[id : '{}']", beanName, beanFactory.getSerializationId());
}
return ready;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ private void registerDependentBeans(String beanName, Set<String> injectedBeanNam
if (this.beanFactory.containsBean(injectedBeanName)) {
this.beanFactory.registerDependentBean(injectedBeanName, beanName);
}
if (logger.isDebugEnabled()) {
logger.debug("Injected by type from bean name '" + beanName +
if (logger.isTraceEnabled()) {
logger.trace("Injected by type from bean name '" + beanName +
"' to bean named '" + injectedBeanName + "'");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ public static final boolean registerBeanDefinition(BeanDefinitionRegistry regist
} else {
try {
registry.registerBeanDefinition(beanName, beanDefinition);
if (logger.isDebugEnabled()) {
logger.debug("The bean[name : '{}' , role : {}] definition [{}] has been registered.", beanName, beanDefinition.getRole(), beanDefinition);
if (logger.isTraceEnabled()) {
logger.trace("The bean[name : '{}' , role : {}] definition [{}] has been registered.", beanName, beanDefinition.getRole(), beanDefinition);
}
registered = true;
} catch (BeanDefinitionStoreException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ public void wrap(BeanFactory beanFactory) {
}
return;
}
if (logger.isDebugEnabled()) {
logger.debug("The ListenableAutowireCandidateResolver bean[name : '{}'] is enabled.", this.beanName);
if (logger.isTraceEnabled()) {
logger.trace("The ListenableAutowireCandidateResolver bean[name : '{}'] is enabled.", this.beanName);
}
DefaultListableBeanFactory dbf = asDefaultListableBeanFactory(beanFactory);
AutowireCandidateResolver autowireCandidateResolver = dbf.getAutowireCandidateResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void lazyProxyResolved(DependencyDescriptor descriptor, String beanName,
}

protected void log(String messagePattern, Object... args) {
if (logger.isDebugEnabled()) {
logger.debug(messagePattern, args);
if (logger.isTraceEnabled()) {
logger.trace(messagePattern, args);
}
}

Expand Down
Loading

0 comments on commit a10ff2c

Please sign in to comment.