Skip to content

Commit ae1409e

Browse files
author
Ralf Ueberfuhr
committed
BAEL-9294: Add sample for BeanRegistrar with Spring 7
1 parent 4a617a5 commit ae1409e

File tree

8 files changed

+293
-0
lines changed

8 files changed

+293
-0
lines changed

spring-boot-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<module>spring-boot-brave</module>
130130
<module>spring-boot-simple</module>
131131
<module>spring-boot-http2</module>
132+
<module>spring-boot-4</module>
132133
</modules>
133134

134135
<build>
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>spring-boot-4</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>spring-boot-4</name>
9+
<description>Demo project for Spring Boot</description>
10+
11+
<parent>
12+
<groupId>com.baeldung.spring-boot-modules</groupId>
13+
<artifactId>spring-boot-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-devtools</artifactId>
21+
<scope>runtime</scope>
22+
<optional>true</optional>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-configuration-processor</artifactId>
27+
<optional>true</optional>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.projectlombok</groupId>
31+
<artifactId>lombok</artifactId>
32+
<optional>true</optional>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.mapstruct</groupId>
36+
<artifactId>mapstruct</artifactId>
37+
<version>${mapstruct.version}</version>
38+
<optional>true</optional>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-test</artifactId>
43+
</dependency>
44+
</dependencies>
45+
46+
<profiles>
47+
<profile>
48+
<id>default</id>
49+
<activation>
50+
<activeByDefault>true</activeByDefault>
51+
</activation>
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
<configuration>
58+
<mainClass>com.baeldung.virtualthreads.VirtualThreadsApp</mainClass>
59+
<excludes>
60+
<exclude>
61+
<groupId>org.projectlombok</groupId>
62+
<artifactId>lombok</artifactId>
63+
</exclude>
64+
</excludes>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
</profile>
70+
71+
<profile>
72+
<id>integration</id>
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-surefire-plugin</artifactId>
78+
<configuration>
79+
<!-- Force sequential test execution -->
80+
<parallel>none</parallel>
81+
<forkCount>1</forkCount>
82+
<reuseForks>false</reuseForks>
83+
</configuration>
84+
</plugin>
85+
</plugins>
86+
</build>
87+
</profile>
88+
</profiles>
89+
90+
<build>
91+
<pluginManagement>
92+
<plugins>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-compiler-plugin</artifactId>
96+
<configuration>
97+
<annotationProcessorPaths>
98+
<path>
99+
<groupId>org.mapstruct</groupId>
100+
<artifactId>mapstruct-processor</artifactId>
101+
<version>${mapstruct.version}</version>
102+
</path>
103+
<path>
104+
<groupId>org.projectlombok</groupId>
105+
<artifactId>lombok</artifactId>
106+
<version>${lombok.version}</version>
107+
</path>
108+
<!-- This is needed when using Lombok 1.18.16 and above -->
109+
<path>
110+
<groupId>org.projectlombok</groupId>
111+
<artifactId>lombok-mapstruct-binding</artifactId>
112+
<version>${lombok-mapstruct-binding.version}</version>
113+
</path>
114+
</annotationProcessorPaths>
115+
</configuration>
116+
</plugin>
117+
</plugins>
118+
</pluginManagement>
119+
<plugins>
120+
<plugin>
121+
<groupId>org.springframework.boot</groupId>
122+
<artifactId>spring-boot-maven-plugin</artifactId>
123+
</plugin>
124+
<plugin>
125+
<groupId>org.apache.maven.plugins</groupId>
126+
<artifactId>maven-compiler-plugin</artifactId>
127+
</plugin>
128+
</plugins>
129+
</build>
130+
131+
<repositories>
132+
<repository>
133+
<id>repository.spring.milestone</id>
134+
<name>Spring Snapshot Repository</name>
135+
<url>https://repo.spring.io/milestone</url>
136+
</repository>
137+
<repository>
138+
<id>repository.spring.snapshot</id>
139+
<name>Spring Snapshot Repository</name>
140+
<url>https://repo.spring.io/snapshot</url>
141+
<snapshots>
142+
<enabled>true</enabled>
143+
<updatePolicy>daily</updatePolicy>
144+
</snapshots>
145+
</repository>
146+
</repositories>
147+
<pluginRepositories>
148+
<pluginRepository>
149+
<id>repository.spring.milestone.plugins</id>
150+
<name>Spring Snapshot Repository</name>
151+
<url>https://repo.spring.io/milestone</url>
152+
</pluginRepository>
153+
<pluginRepository>
154+
<id>repository.spring.snapshot.plugins</id>
155+
<name>Spring Snapshot Repository</name>
156+
<url>https://repo.spring.io/snapshot</url>
157+
<snapshots>
158+
<enabled>true</enabled>
159+
<updatePolicy>daily</updatePolicy>
160+
</snapshots>
161+
</pluginRepository>
162+
</pluginRepositories>
163+
164+
<properties>
165+
<mapstruct.version>1.6.3</mapstruct.version>
166+
<spring-boot.version>4.0.0-SNAPSHOT</spring-boot.version>
167+
<logback.version>1.5.18</logback.version>
168+
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
169+
</properties>
170+
171+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.spring.beanregistrar;
2+
3+
import org.springframework.beans.factory.BeanRegistrar;
4+
import org.springframework.beans.factory.BeanRegistry;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Import;
8+
import org.springframework.core.env.Environment;
9+
10+
@ConditionalOnProperty(name = "application.registration-v7", havingValue = "true", matchIfMissing = true)
11+
@Configuration
12+
@Import(BeanRegistrationsConfiguration.MyBeanRegistrar.class)
13+
public class BeanRegistrationsConfiguration {
14+
15+
static class MyBeanRegistrar implements BeanRegistrar {
16+
17+
@Override
18+
public void register(BeanRegistry registry, Environment env) {
19+
registry.registerBean("myService", MyService.class);
20+
registry.registerBean("service2", MyService.class, spec -> spec.prototype()
21+
.lazyInit()
22+
.primary());
23+
}
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.spring.beanregistrar;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.beans.factory.config.BeanDefinition;
5+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
6+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
7+
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
8+
import org.springframework.beans.factory.support.GenericBeanDefinition;
9+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
@ConditionalOnProperty(name = "application.registration-v7", havingValue = "false")
14+
@SuppressWarnings("NullableProblems")
15+
@Configuration
16+
public class BeanRegistrationsSpring6Configuration {
17+
18+
@Bean
19+
BeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor() {
20+
return new BeanDefinitionRegistryPostProcessor() {
21+
22+
@Override
23+
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
24+
var beanDefinition = new GenericBeanDefinition();
25+
beanDefinition.setBeanClass(MyService.class);
26+
beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
27+
registry.registerBeanDefinition("myService", beanDefinition);
28+
}
29+
30+
@Override
31+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
32+
// No-op
33+
}
34+
};
35+
}
36+
37+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.spring.beanregistrar;
2+
3+
public class MyService {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.spring.beanregistrar;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SampleApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SampleApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.spring.beanregistrar;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
9+
@SpringBootTest
10+
class BeanRegistrarIntegrationTest {
11+
12+
@Autowired
13+
MyService myService;
14+
15+
@Test
16+
void whenRunningPlatform_thenRegisterBean() {
17+
assertThat(myService).isNotNull();
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.spring.beanregistrar;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
9+
@SpringBootTest(properties = "application.registration-v7=false")
10+
class BeanRegistrationsSpring6IntegrationTest {
11+
12+
@Autowired
13+
MyService myService;
14+
15+
@Test
16+
void whenRunningPlatform_thenRegisterBean() {
17+
assertThat(myService).isNotNull();
18+
}
19+
20+
}

0 commit comments

Comments
 (0)