@@ -56,100 +56,115 @@ public class FelSpringBootAutoConfiguration {
5656 */
5757 @ Bean
5858 public static BeanDefinitionRegistryPostProcessor felBeanAutoRegistrar () {
59- return new BeanDefinitionRegistryPostProcessor () {
60- @ Override
61- public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry ) throws BeansException {
62- // 不需要在这里处理
63- }
59+ return new FelBeanAutoRegistrar ();
60+ }
6461
65- @ Override
66- public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory ) throws BeansException {
67- LOG .info ("Installing FEL Bean auto-registration processor" );
68- beanFactory .addBeanPostProcessor (new org .springframework .beans .factory .config .BeanPostProcessor () {
69- private boolean registered = false ;
70-
71- @ Override
72- public Object postProcessAfterInitialization (Object bean , String beanName ) throws BeansException {
73- if (!registered && bean instanceof FitRuntime ) {
74- registered = true ;
75- registerFelBeansToSpring ((FitRuntime ) bean , beanFactory );
76- }
77- return bean ;
78- }
79- });
80- }
62+ /**
63+ * FEL Bean 自动注册器,负责将 FIT 容器中的 FEL Bean 注册到 Spring 容器。
64+ */
65+ private static class FelBeanAutoRegistrar implements BeanDefinitionRegistryPostProcessor {
66+ @ Override
67+ public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry ) throws BeansException {
68+ // 不需要在这里处理
69+ }
70+
71+ @ Override
72+ public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory ) throws BeansException {
73+ LOG .info ("Installing FEL Bean auto-registration processor" );
74+ beanFactory .addBeanPostProcessor (new FitRuntimeDetectorBeanPostProcessor (beanFactory ));
75+ }
76+ }
8177
82- /**
83- * 将 FIT 容器中符合白名单的 FEL Bean 注册到 Spring 容器。
84- *
85- * @param fitRuntime FIT 运行时实例
86- * @param beanFactory Spring Bean 工厂
87- */
88- private void registerFelBeansToSpring (FitRuntime fitRuntime , ConfigurableListableBeanFactory beanFactory ) {
89- LOG .info ("Auto-registering FEL beans to Spring container (whitelist mode)" );
90- try {
91- BeanContainer container = fitRuntime .root ().container ();
92- List <BeanFactory > allFactories = container .all ();
93- LOG .debug ("Found {} beans in FIT container" , allFactories .size ());
94-
95- int registeredCount = 0 ;
96- int skippedCount = 0 ;
97- int filteredCount = 0 ;
98-
99- for (BeanFactory factory : allFactories ) {
100- BeanMetadata metadata = factory .metadata ();
101- String beanName = metadata .name ();
102- Type beanType = metadata .type ();
103- String typeName = beanType .getTypeName ();
104-
105- // 检查是否在白名单中(使用字符串匹配,支持子类)
106- boolean isRequired = REQUIRED_BEAN_TYPE_NAMES .stream ().anyMatch (requiredTypeName -> {
107- try {
108- Class <?> actualType = Class .forName (typeName );
109- Class <?> requiredType = Class .forName (requiredTypeName );
110- return requiredType .isAssignableFrom (actualType );
111- } catch (ClassNotFoundException e ) {
112- // 某些可选依赖的类可能不在 classpath 中,忽略
113- return false ;
114- }
115- });
116-
117- if (!isRequired ) {
118- LOG .trace ("Filtered out FIT bean '{}' - not in whitelist" , beanName );
119- filteredCount ++;
120- continue ;
78+ /**
79+ * FIT 运行时检测后处理器,检测 FitRuntime Bean 初始化完成后触发 FEL Bean 注册。
80+ */
81+ private static class FitRuntimeDetectorBeanPostProcessor
82+ implements org .springframework .beans .factory .config .BeanPostProcessor {
83+ private final ConfigurableListableBeanFactory beanFactory ;
84+ private boolean registered = false ;
85+
86+ FitRuntimeDetectorBeanPostProcessor (ConfigurableListableBeanFactory beanFactory ) {
87+ this .beanFactory = beanFactory ;
88+ }
89+
90+ @ Override
91+ public Object postProcessAfterInitialization (Object bean , String beanName ) throws BeansException {
92+ if (!registered && bean instanceof FitRuntime ) {
93+ registered = true ;
94+ registerFelBeansToSpring ((FitRuntime ) bean );
95+ }
96+ return bean ;
97+ }
98+
99+ /**
100+ * 将 FIT 容器中符合白名单的 FEL Bean 注册到 Spring 容器。
101+ *
102+ * @param fitRuntime FIT 运行时实例
103+ */
104+ private void registerFelBeansToSpring (FitRuntime fitRuntime ) {
105+ LOG .info ("Auto-registering FEL beans to Spring container (whitelist mode)" );
106+ try {
107+ BeanContainer container = fitRuntime .root ().container ();
108+ List <BeanFactory > allFactories = container .all ();
109+ LOG .debug ("Found {} beans in FIT container" , allFactories .size ());
110+
111+ int registeredCount = 0 ;
112+ int skippedCount = 0 ;
113+ int filteredCount = 0 ;
114+
115+ for (BeanFactory factory : allFactories ) {
116+ BeanMetadata metadata = factory .metadata ();
117+ String beanName = metadata .name ();
118+ Type beanType = metadata .type ();
119+ String typeName = beanType .getTypeName ();
120+
121+ // 检查是否在白名单中(使用字符串匹配,支持子类)
122+ boolean isRequired = REQUIRED_BEAN_TYPE_NAMES .stream ().anyMatch (requiredTypeName -> {
123+ try {
124+ Class <?> actualType = Class .forName (typeName );
125+ Class <?> requiredType = Class .forName (requiredTypeName );
126+ return requiredType .isAssignableFrom (actualType );
127+ } catch (ClassNotFoundException e ) {
128+ // 某些可选依赖的类可能不在 classpath 中,忽略
129+ return false ;
121130 }
131+ });
122132
123- // 避免重复注册
124- if (beanFactory .containsSingleton (beanName ) || beanFactory .containsBeanDefinition (beanName )) {
125- LOG .debug ("Skipping bean '{}' - already exists in Spring container" , beanName );
126- skippedCount ++;
127- continue ;
128- }
133+ if (!isRequired ) {
134+ LOG .trace ("Filtered out FIT bean '{}' - not in whitelist" , beanName );
135+ filteredCount ++;
136+ continue ;
137+ }
129138
130- try {
131- if (metadata .singleton ()) {
132- Object instance = factory .get ();
133- beanFactory .registerSingleton (beanName , instance );
134- registeredCount ++;
135- LOG .info ("Registered FEL bean '{}' (type: {}) to Spring container" , beanName , typeName );
136- }
137- } catch (Exception e ) {
138- LOG .warn ("Failed to register FEL bean '{}' of type {}: {}" ,
139- beanName ,
140- typeName ,
141- e .getMessage ());
142- }
139+ // 避免重复注册
140+ if (beanFactory .containsSingleton (beanName ) || beanFactory .containsBeanDefinition (beanName )) {
141+ LOG .debug ("Skipping bean '{}' - already exists in Spring container" , beanName );
142+ skippedCount ++;
143+ continue ;
143144 }
144145
145- LOG .info ("FEL Bean auto-registration completed: {} registered, {} skipped, {} filtered" ,
146- registeredCount ,
147- skippedCount ,
148- filteredCount );
149- } catch (Exception e ) {
150- LOG .error ("Failed to auto-register FEL beans" , e );
146+ try {
147+ if (metadata .singleton ()) {
148+ Object instance = factory .get ();
149+ beanFactory .registerSingleton (beanName , instance );
150+ registeredCount ++;
151+ LOG .info ("Registered FEL bean '{}' (type: {}) to Spring container" , beanName , typeName );
152+ }
153+ } catch (Exception e ) {
154+ LOG .warn ("Failed to register FEL bean '{}' of type {}: {}" ,
155+ beanName ,
156+ typeName ,
157+ e .getMessage ());
158+ }
151159 }
160+
161+ LOG .info ("FEL Bean auto-registration completed: {} registered, {} skipped, {} filtered" ,
162+ registeredCount ,
163+ skippedCount ,
164+ filteredCount );
165+ } catch (Exception e ) {
166+ LOG .error ("Failed to auto-register FEL beans" , e );
152167 }
153- };
168+ }
154169 }
155170}
0 commit comments