Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Apr 19, 2024
1 parent 8091c30 commit 3ce8be8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion rxlib/src/main/java/org/rx/spring/Interceptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
}

if (Strings.equals(httpEnv.left.getParameter("rmx"), Constants.ENABLE_FLAG)) {
MxController controller = SpringContext.getBean(MxController.class);
MxController controller = SpringContext.getBean(MxController.class, false);
if (controller != null) {
return controller.health(httpEnv.left);
}
Expand Down
2 changes: 1 addition & 1 deletion rxlib/src/main/java/org/rx/spring/MxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Object health(HttpServletRequest request) {
Object source = null, target;
if (!Strings.isBlank(type)) {
Class<?> clazz = Class.forName(type);
target = SpringContext.getBean(clazz);
target = SpringContext.getBean(clazz, false);
if (target == null) {
return null;
}
Expand Down
10 changes: 9 additions & 1 deletion rxlib/src/main/java/org/rx/spring/SpringContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Setter;
import org.rx.core.Linq;
import org.rx.core.Strings;
import org.rx.exception.InvalidException;
import org.rx.util.function.TripleFunc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand Down Expand Up @@ -37,8 +38,15 @@ public static <T> T getBean(String name) {
}

public static <T> T getBean(Class<T> clazz) {
return getBean(clazz, true);
}

public static <T> T getBean(Class<T> clazz, boolean throwOnEmpty) {
Map<String, T> beanMaps = getApplicationContext().getBeansOfType(clazz);
return !beanMaps.isEmpty() ? beanMaps.values().iterator().next() : null;
if (throwOnEmpty && beanMaps.isEmpty()) {
throw new InvalidException("Bean {} not registered", clazz);
}
return beanMaps.values().iterator().next();
}

public static String[] fromYamlArray(String yamlArray) {
Expand Down

0 comments on commit 3ce8be8

Please sign in to comment.