GH-1499: added demo code to find config beans with imports for a certain type

This commit is contained in:
Martin Lippert
2025-03-12 12:42:08 +01:00
parent e4a0dece0f
commit f034246f18
6 changed files with 54 additions and 20 deletions

View File

@@ -27,6 +27,8 @@ public class Annotations {
public static final String COMPONENT = "org.springframework.stereotype.Component";
public static final String CONFIGURATION = "org.springframework.context.annotation.Configuration";
public static final String IMPORT = "org.springframework.context.annotation.Import";
public static final String CONTROLLER = "org.springframework.stereotype.Controller";
public static final String CONFIGURATION_PROPERTIES = "org.springframework.boot.context.properties.ConfigurationProperties";
@@ -113,5 +115,4 @@ public class Annotations {
"org.aspectj.lang.annotation.DeclareParents", "DeclareParents"
);
}

View File

@@ -10,6 +10,7 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.index.test;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
@@ -28,7 +29,9 @@ import org.springframework.ide.vscode.boot.app.SpringSymbolIndex;
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.protocol.spring.AnnotationMetadata;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
@@ -94,5 +97,21 @@ public class SpringIndexerBeanRegistrarTest {
assertEquals("baz", baz.getName());
assertEquals("com.example.Baz", baz.getType());
}
@Test
void testNonRegisteredBeanRegistrar() throws Exception {
Bean[] beans = springIndex.getBeansOfProject("test-framework-7-indexing");
String registrarName = "com.example.MyBeanRegistrar";
boolean anyMatch = Arrays.stream(beans)
.filter(bean -> bean.isConfiguration()) // look into beans with @Configuration only
.flatMap(bean -> Arrays.stream(bean.getAnnotations())) // look into annotations on this bean definition
.filter(annotation -> Annotations.IMPORT.equals(annotation.getAnnotationType())) // look into @Import annotations only
.flatMap(annotation -> Arrays.stream(annotation.getAttributes().get("value"))) // look into the attribute values of "value" attribute
.anyMatch(annotationValue -> annotationValue.getName().equals(registrarName));
assertTrue(anyMatch);
}
}

View File

@@ -0,0 +1,9 @@
package com.example;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(MyBeanRegistrar.class)
public class ConfigImportsBeanRegistrar {
}

View File

@@ -6,19 +6,15 @@ import org.springframework.core.env.Environment;
public class MyBeanRegistrar implements BeanRegistrar {
@Override
public void register(BeanRegistry registry, Environment env) {
registry.registerBean(FooFoo.class);
registry.registerBean("foo", Foo.class);
registry.registerBean("bar", Bar.class, spec -> spec
.prototype()
.lazyInit()
.description("Custom description")
.supplier(context -> new Bar(context.bean(Foo.class))));
if (env.matchesProfiles("baz")) {
registry.registerBean(Baz.class, spec -> spec
.supplier(context -> new Baz("Hello World!")));
}
}
@Override
public void register(BeanRegistry registry, Environment env) {
registry.registerBean(FooFoo.class);
registry.registerBean("foo", Foo.class);
registry.registerBean("bar", Bar.class, spec -> spec.prototype().lazyInit().description("Custom description")
.supplier(context -> new Bar(context.bean(Foo.class))));
if (env.matchesProfiles("baz")) {
registry.registerBean(Baz.class, spec -> spec.supplier(context -> new Baz("Hello World!")));
}
}
}

View File

@@ -0,0 +1,14 @@
package com.example;
import org.springframework.beans.factory.BeanRegistrar;
import org.springframework.beans.factory.BeanRegistry;
import org.springframework.core.env.Environment;
public class NotRegisterredBeanRegistrar implements BeanRegistrar {
@Override
public void register(BeanRegistry registry, Environment env) {
registry.registerBean("not-registered-anyway", FooFoo.class);
}
}