Skip to content

Commit

Permalink
Merge pull request #94 from microsphere-projects/dev
Browse files Browse the repository at this point in the history
Release 0.2.1
  • Loading branch information
mercyblitz authored Jan 11, 2025
2 parents 53cb3c8 + 4a90cd4 commit 7e35d1e
Show file tree
Hide file tree
Showing 23 changed files with 543 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import static io.microsphere.spring.context.ApplicationContextUtils.getApplicationContextAwareProcessor;
import static io.microsphere.util.ArrayUtils.isEmpty;
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;
import static org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors;
Expand Down Expand Up @@ -264,15 +263,14 @@ public static <T> T getOptionalBean(ListableBeanFactory beanFactory, Class<T> be
* @param <T> the bean type
* @return the bean if available, or <code>null</code>
* @throws BeansException in case of creation errors
* @since 1.0.0
*/
public static <T> T getBeanIfAvailable(BeanFactory beanFactory, String beanName, Class<T> beanType) throws BeansException {
if (isBeanPresent(beanFactory, beanName, beanType)) {
return beanFactory.getBean(beanName, beanType);
}

if (logger.isTraceEnabled()) {
logger.trace(format("The bean[name : %s , type : %s] can't be found in Spring BeanFactory", beanName, beanType.getName()));
logger.trace("The bean[name : '{}' , type : {}] 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 @@ -70,9 +70,11 @@
import static io.microsphere.logging.LoggerFactory.getLogger;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asConfigurableListableBeanFactory;
import static io.microsphere.spring.core.annotation.AnnotationUtils.getAnnotationAttributes;
import static java.lang.Integer.getInteger;
import static java.util.Collections.singleton;
import static java.util.Collections.unmodifiableCollection;
import static org.springframework.beans.BeanUtils.findPrimaryConstructor;
import static org.springframework.beans.factory.annotation.InjectionMetadata.needsRefresh;
import static org.springframework.core.BridgeMethodResolver.findBridgedMethod;
import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair;
import static org.springframework.util.Assert.notEmpty;
Expand Down Expand Up @@ -110,7 +112,7 @@ public class AnnotatedInjectionBeanPostProcessor extends InstantiationAwareBeanP
implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware, BeanClassLoaderAware,
EnvironmentAware, InitializingBean, DisposableBean {

private final static int CACHE_SIZE = Integer.getInteger("microsphere.spring.injection.metadata.cache.size", 32);
private final static int CACHE_SIZE = getInteger("microsphere.spring.injection.metadata.cache.size", 32);

private final Logger logger = getLogger(getClass());

Expand Down Expand Up @@ -419,10 +421,10 @@ private InjectionMetadata findInjectionMetadata(String beanName, Class<?> clazz,
String cacheKey = (hasLength(beanName) ? beanName : clazz.getName());
// Quick check on the concurrent map first, with minimal locking.
AnnotatedInjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
if (needsRefresh(metadata, clazz)) {
synchronized (this.injectionMetadataCache) {
metadata = this.injectionMetadataCache.get(cacheKey);
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
if (needsRefresh(metadata, clazz)) {
if (metadata != null) {
metadata.clear(pvs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import static io.microsphere.util.ArrayUtils.EMPTY_CLASS_ARRAY;
import static io.microsphere.util.ArrayUtils.isEmpty;
import static io.microsphere.util.ArrayUtils.isNotEmpty;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR;
import static org.springframework.util.ClassUtils.resolveClassName;
Expand All @@ -51,7 +50,6 @@ public abstract class AnnotatedBeanDefinitionRegistryUtils extends BaseUtils {
* @param registry {@link BeanDefinitionRegistry}
* @param annotatedClass the {@link Annotation annotated} {@link Class class}
* @return if present, return <code>true</code>, or <code>false</code>
* @since 1.0.0
*/
public static boolean isPresentBean(BeanDefinitionRegistry registry, Class<?> annotatedClass) {

Expand All @@ -70,8 +68,8 @@ public static boolean isPresentBean(BeanDefinitionRegistry registry, Class<?> an
present = nullSafeEquals(targetClass, annotatedClass);
if (present) {
if (logger.isTraceEnabled()) {
logger.trace(format("The annotatedClass[class : %s , bean name : %s] was present in registry[%s]",
className, beanName, registry));
logger.trace("The annotatedClass[class : '{}' , bean name : '{}'] was present in registry : {}",
className, beanName, registry);
}
break;
}
Expand Down Expand Up @@ -170,7 +168,6 @@ public static int scanBasePackages(BeanDefinitionRegistry registry, String... ba
* @see SingletonBeanRegistry
* @see AnnotationConfigUtils#CONFIGURATION_BEAN_NAME_GENERATOR
* @see ConfigurationClassPostProcessor#processConfigBeanDefinitions
* @since 1.0.0
*/
public static BeanNameGenerator resolveAnnotatedBeanNameGenerator(BeanDefinitionRegistry registry) {
BeanNameGenerator beanNameGenerator = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static io.microsphere.logging.LoggerFactory.getLogger;
import static io.microsphere.spring.beans.BeanUtils.getBeanIfAvailable;
import static io.microsphere.spring.beans.BeanUtils.isBeanPresent;
import static java.lang.String.format;
import static io.microsphere.text.FormatUtils.format;
import static org.springframework.context.ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME;
import static org.springframework.context.ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME;

Expand Down Expand Up @@ -67,12 +67,12 @@ public ConversionService resolve(boolean requireToRegister) {
}

if (conversionService == null) { // If not found, try to get the bean from BeanFactory
debug("The conversionService instance can't be found in Spring ConfigurableBeanFactory.getConversionService()");
trace("The conversionService instance can't be found in Spring ConfigurableBeanFactory.getConversionService()");
conversionService = getFromEnvironment();
}

if (conversionService == null) { // If not found, try to get the bean from ConfigurableEnvironment
debug("The conversionService instance can't be found in Spring ConfigurableEnvironment.getConversionService()");
trace("The conversionService instance can't be found in Spring ConfigurableEnvironment.getConversionService()");
conversionService = getIfAvailable();
}
if (conversionService == null) { // If not found, will create an instance of ConversionService as default
Expand Down Expand Up @@ -117,7 +117,7 @@ protected ConversionService createDefaultConversionService() {
return new DefaultFormattingConversionService();
}

private void debug(String message, Object... args) {
private void trace(String message, Object... args) {
if (logger.isTraceEnabled()) {
logger.trace(args.length < 1 ? message : format(message, args));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public void testRegisterSpringFactoriesBeans() {
assertEquals(2, registerSpringFactoriesBeans((BeanDefinitionRegistry) registry, Bean.class));
assertTrue(registry.containsBeanDefinition("testBean"));
assertTrue(registry.containsBeanDefinition("testBean2"));
assertEquals(TestBean.class,registry.getBean("testBean").getClass());
assertEquals(TestBean2.class,registry.getBean("testBean2").getClass());
assertEquals(TestBean.class, registry.getBean("testBean").getClass());
assertEquals(TestBean2.class, registry.getBean("testBean2").getClass());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.p6spy.engine.spy.P6Factory;
import com.p6spy.engine.spy.P6ModuleManager;
import com.p6spy.engine.spy.option.P6OptionChangedListener;
import io.microsphere.spring.beans.factory.support.BeanRegistrar;
import io.microsphere.spring.jdbc.p6spy.beans.factory.CompoundJdbcEventListenerFactory;
import io.microsphere.spring.jdbc.p6spy.beans.factory.config.P6DataSourceBeanPostProcessor;
import org.springframework.beans.BeansException;
Expand All @@ -34,6 +33,8 @@
import java.util.List;

import static io.microsphere.spring.beans.BeanUtils.getSortedBeans;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asConfigurableListableBeanFactory;
import static io.microsphere.spring.beans.factory.support.BeanRegistrar.registerBeanDefinition;

/**
* The {@link ImportBeanDefinitionRegistrar} class to register {@link BeanDefinition BeanDefinitions}
Expand All @@ -45,12 +46,12 @@ class P6DataSourceBeanDefinitionRegistrar implements ImportBeanDefinitionRegistr

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
BeanRegistrar.registerBeanDefinition(registry, P6DataSourceBeanPostProcessor.class);
registerBeanDefinition(registry, P6DataSourceBeanPostProcessor.class);
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
initP6ModuleManager((ConfigurableListableBeanFactory) beanFactory);
initP6ModuleManager(asConfigurableListableBeanFactory(beanFactory));
}

private void initP6ModuleManager(ConfigurableListableBeanFactory beanFactory) {
Expand Down
8 changes: 8 additions & 0 deletions microsphere-spring-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<logback.version>1.5.15</logback.version>
<junit.version>5.11.4</junit.version>
<mockito.version>5.14.2</mockito.version>
<jsonassert.version>1.5.3</jsonassert.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -82,6 +83,13 @@
<scope>import</scope>
</dependency>

<!-- JSONassert -->
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>${jsonassert.version}</version>
</dependency>

<!-- Slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ public void beforeExecute(HandlerMethod handlerMethod, Object[] args, NativeWebR
context.publishEvent(new HandlerMethodArgumentsResolvedEvent(request, handlerMethod, args));
}

@Override
public void afterExecute(HandlerMethod handlerMethod, Object[] args, Object returnValue, Throwable error, NativeWebRequest request) throws Exception {
// DO NOTHING
}

@Override
protected void doStart() {
WebEndpointMappingRegistry webEndpointMappingRegistry = context.getBean(WebEndpointMappingRegistry.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public interface HandlerMethodAdvice {
* @param webRequest the current request
* @throws Exception in case of errors with the preparation of argument values
*/
void beforeResolveArgument(MethodParameter parameter, HandlerMethod handlerMethod, NativeWebRequest webRequest)
throws Exception;
default void beforeResolveArgument(MethodParameter parameter, HandlerMethod handlerMethod, NativeWebRequest webRequest)
throws Exception {
}

/**
* callback after the {@link MethodParameter} being resolved
Expand All @@ -54,8 +55,9 @@ void beforeResolveArgument(MethodParameter parameter, HandlerMethod handlerMetho
* @return the resolved argument value, or {@code null} if not resolvable
* @throws Exception in case of errors with the preparation of argument values
*/
void afterResolveArgument(MethodParameter parameter, Object resolvedArgument, HandlerMethod handlerMethod,
NativeWebRequest webRequest) throws Exception;
default void afterResolveArgument(MethodParameter parameter, Object resolvedArgument, HandlerMethod handlerMethod,
NativeWebRequest webRequest) throws Exception {
}

/**
* Interception point before the execution of a {@link HandlerMethod}. Called after
Expand All @@ -67,7 +69,8 @@ void afterResolveArgument(MethodParameter parameter, Object resolvedArgument, Ha
* @param request {@link WebRequest}
* @throws Exception if any error caused
*/
void beforeExecuteMethod(HandlerMethod handlerMethod, Object[] args, NativeWebRequest request) throws Exception;
default void beforeExecuteMethod(HandlerMethod handlerMethod, Object[] args, NativeWebRequest request) throws Exception {
}

/**
* Interception point after successful execution of a {@link HandlerMethod}.
Expand All @@ -80,7 +83,8 @@ void afterResolveArgument(MethodParameter parameter, Object resolvedArgument, Ha
* @param request {@link WebRequest}
* @throws Exception if any error caused
*/
void afterExecuteMethod(HandlerMethod handlerMethod, Object[] args, @Nullable Object returnValue, @Nullable Throwable error,
NativeWebRequest request) throws Exception;
default void afterExecuteMethod(HandlerMethod handlerMethod, Object[] args, @Nullable Object returnValue, @Nullable Throwable error,
NativeWebRequest request) throws Exception {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.HandlerMethod;


/**
* The interceptor interface for the resolvable {@link HandlerMethod HandlerMethods'} {@link MethodParameter}
*
Expand All @@ -39,8 +38,9 @@ public interface HandlerMethodArgumentInterceptor {
* @param webRequest the current request
* @throws Exception in case of errors with the preparation of argument values
*/
void beforeResolveArgument(MethodParameter parameter, HandlerMethod handlerMethod, NativeWebRequest webRequest)
throws Exception;
default void beforeResolveArgument(MethodParameter parameter, HandlerMethod handlerMethod, NativeWebRequest webRequest)
throws Exception {
}

/**
* callback after the {@link MethodParameter} being resolved
Expand All @@ -52,8 +52,8 @@ void beforeResolveArgument(MethodParameter parameter, HandlerMethod handlerMetho
* @return the resolved argument value, or {@code null} if not resolvable
* @throws Exception in case of errors with the preparation of argument values
*/
void afterResolveArgument(MethodParameter parameter, Object resolvedArgument, HandlerMethod handlerMethod,
NativeWebRequest webRequest) throws Exception;

default void afterResolveArgument(MethodParameter parameter, Object resolvedArgument, HandlerMethod handlerMethod,
NativeWebRequest webRequest) throws Exception {
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public interface HandlerMethodInterceptor {
* @param request {@link WebRequest}
* @throws Exception if any error caused
*/
void beforeExecute(HandlerMethod handlerMethod, Object[] args, NativeWebRequest request) throws Exception;
default void beforeExecute(HandlerMethod handlerMethod, Object[] args, NativeWebRequest request) throws Exception {
}

/**
* Interception point after successful execution of a {@link HandlerMethod}.
Expand All @@ -50,7 +51,8 @@ public interface HandlerMethodInterceptor {
* @param request {@link WebRequest}
* @throws Exception if any error caused
*/
void afterExecute(HandlerMethod handlerMethod, Object[] args, @Nullable Object returnValue, @Nullable Throwable error,
NativeWebRequest request) throws Exception;
default void afterExecute(HandlerMethod handlerMethod, Object[] args, @Nullable Object returnValue, @Nullable Throwable error,
NativeWebRequest request) throws Exception {
}

}
6 changes: 6 additions & 0 deletions microsphere-spring-webmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.HandlerMethod;

import java.lang.reflect.Method;

import static io.microsphere.spring.webmvc.util.WebMvcUtils.setHandlerMethodReturnValue;
import static io.microsphere.spring.webmvc.util.WebMvcUtils.supportedConverterTypes;

/**
* Store {@ link HandlerMethod} return value {@ link ResponseBodyAdviceAdapter}
* Store {@link HandlerMethod} return value {@link ResponseBodyAdviceAdapter}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
* @since 1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,24 @@
boolean publishEvents() default true;

/**
* Indicate whether the {@link InterceptorRegistry} registers the beans of {@link HandlerInterceptor}
* by the specified types
* Indicate whether the {@link InterceptorRegistry} registers the beans of {@link HandlerInterceptor}.
* If it specifies <code>true</code>, {@link #handlerInterceptors()} method will not work anymore.
*
* @return <code>false</code> as default
* @see WebMvcConfigurer#addInterceptors(InterceptorRegistry)
* @see InterceptorRegistry
*/
Class<? extends HandlerInterceptor>[] registerHandlerInterceptors() default {};
boolean registerHandlerInterceptors() default false;

/**
* Specify {@link HandlerInterceptor} types or its inherited types to register into {@link InterceptorRegistry}.
* If {@link #registerHandlerInterceptors()} is <code>true</code>, specified types will be ignored.
*
* @return <code>null</code> as default
* @see WebMvcConfigurer#addInterceptors(InterceptorRegistry)
* @see InterceptorRegistry
*/
Class<? extends HandlerInterceptor>[] handlerInterceptors() default {};

/**
* Indicate that Stores the {@link MethodParameter argument} of {@link HandlerMethod} that annotated {@link RequestBody}
Expand Down
Loading

0 comments on commit 7e35d1e

Please sign in to comment.