Skip to content

Commit f034246

Browse files
committed
GH-1499: added demo code to find config beans with imports for a certain type
1 parent e4a0dec commit f034246

File tree

6 files changed

+54
-20
lines changed

6 files changed

+54
-20
lines changed

Diff for: headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/Annotations.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class Annotations {
2727

2828
public static final String COMPONENT = "org.springframework.stereotype.Component";
2929
public static final String CONFIGURATION = "org.springframework.context.annotation.Configuration";
30+
public static final String IMPORT = "org.springframework.context.annotation.Import";
31+
3032
public static final String CONTROLLER = "org.springframework.stereotype.Controller";
3133

3234
public static final String CONFIGURATION_PROPERTIES = "org.springframework.boot.context.properties.ConfigurationProperties";
@@ -113,5 +115,4 @@ public class Annotations {
113115
"org.aspectj.lang.annotation.DeclareParents", "DeclareParents"
114116
);
115117

116-
117118
}

Diff for: headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/index/test/SpringIndexerBeanRegistrarTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*******************************************************************************/
1111
package org.springframework.ide.vscode.boot.index.test;
1212

13+
import static org.junit.Assert.assertTrue;
1314
import static org.junit.jupiter.api.Assertions.assertEquals;
1415

1516
import java.io.File;
@@ -28,7 +29,9 @@
2829
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
2930
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf;
3031
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
32+
import org.springframework.ide.vscode.boot.java.Annotations;
3133
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
34+
import org.springframework.ide.vscode.commons.protocol.spring.AnnotationMetadata;
3235
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
3336
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
3437
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
@@ -94,5 +97,21 @@ void testSimpleBeanRegistration() throws Exception {
9497
assertEquals("baz", baz.getName());
9598
assertEquals("com.example.Baz", baz.getType());
9699
}
100+
101+
@Test
102+
void testNonRegisteredBeanRegistrar() throws Exception {
103+
104+
Bean[] beans = springIndex.getBeansOfProject("test-framework-7-indexing");
105+
String registrarName = "com.example.MyBeanRegistrar";
106+
107+
boolean anyMatch = Arrays.stream(beans)
108+
.filter(bean -> bean.isConfiguration()) // look into beans with @Configuration only
109+
.flatMap(bean -> Arrays.stream(bean.getAnnotations())) // look into annotations on this bean definition
110+
.filter(annotation -> Annotations.IMPORT.equals(annotation.getAnnotationType())) // look into @Import annotations only
111+
.flatMap(annotation -> Arrays.stream(annotation.getAttributes().get("value"))) // look into the attribute values of "value" attribute
112+
.anyMatch(annotationValue -> annotationValue.getName().equals(registrarName));
113+
114+
assertTrue(anyMatch);
115+
}
97116

98117
}

Diff for: headless-services/spring-boot-language-server/src/test/resources/test-projects/test-framework-7-indexing/src/main/java/com/example/BeanClass.java

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.context.annotation.Import;
5+
6+
@Configuration
7+
@Import(MyBeanRegistrar.class)
8+
public class ConfigImportsBeanRegistrar {
9+
}

Diff for: headless-services/spring-boot-language-server/src/test/resources/test-projects/test-framework-7-indexing/src/main/java/com/example/MyBeanRegistrar.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66

77
public class MyBeanRegistrar implements BeanRegistrar {
88

9-
@Override
10-
public void register(BeanRegistry registry, Environment env) {
11-
registry.registerBean(FooFoo.class);
12-
registry.registerBean("foo", Foo.class);
13-
registry.registerBean("bar", Bar.class, spec -> spec
14-
.prototype()
15-
.lazyInit()
16-
.description("Custom description")
17-
.supplier(context -> new Bar(context.bean(Foo.class))));
18-
if (env.matchesProfiles("baz")) {
19-
registry.registerBean(Baz.class, spec -> spec
20-
.supplier(context -> new Baz("Hello World!")));
21-
}
22-
}
9+
@Override
10+
public void register(BeanRegistry registry, Environment env) {
11+
registry.registerBean(FooFoo.class);
12+
registry.registerBean("foo", Foo.class);
13+
registry.registerBean("bar", Bar.class, spec -> spec.prototype().lazyInit().description("Custom description")
14+
.supplier(context -> new Bar(context.bean(Foo.class))));
15+
if (env.matchesProfiles("baz")) {
16+
registry.registerBean(Baz.class, spec -> spec.supplier(context -> new Baz("Hello World!")));
17+
}
18+
}
2319

2420
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example;
2+
3+
import org.springframework.beans.factory.BeanRegistrar;
4+
import org.springframework.beans.factory.BeanRegistry;
5+
import org.springframework.core.env.Environment;
6+
7+
public class NotRegisterredBeanRegistrar implements BeanRegistrar {
8+
9+
@Override
10+
public void register(BeanRegistry registry, Environment env) {
11+
registry.registerBean("not-registered-anyway", FooFoo.class);
12+
}
13+
14+
}

0 commit comments

Comments
 (0)