Last active
March 8, 2024 19:54
-
-
Save ehdez73/b9ac35bf8d230c769309 to your computer and use it in GitHub Desktop.
Create Beans programatically with Spring Boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
@SpringBootApplication | |
@RestController | |
@EnableSampleServices | |
public class DemoApplication { | |
@Autowired | |
private List<SampleService> services; | |
@Autowired @Qualifier("s1") | |
private SampleService service1; | |
@Autowired @Qualifier("s2") | |
private SampleService service2; | |
@RequestMapping("/services") | |
public String main(){ | |
return services.stream() | |
.map( sampleService -> sampleService.getName()) | |
.collect(Collectors.joining(", ")); | |
} | |
@RequestMapping("/s1") | |
public String service1(){ | |
return service1.getName(); | |
} | |
@RequestMapping("/s2") | |
public String service2(){ | |
return service2.getName(); | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.springframework.context.annotation.Import; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
@Retention(value = RetentionPolicy.RUNTIME) | |
@Import(SampleServiceConfiguration.class) | |
public @interface EnableSampleServices {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
public class SampleService { | |
private String name; | |
public SampleService(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.springframework.beans.factory.config.BeanDefinition; | |
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | |
import org.springframework.beans.factory.support.RootBeanDefinition; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | |
import org.springframework.core.type.AnnotationMetadata; | |
import java.util.Arrays; | |
import java.util.List; | |
@Configuration | |
public class SampleServiceConfiguration implements ImportBeanDefinitionRegistrar { | |
private List<String> names = Arrays.asList("s1", "s2", "s3"); | |
@Override | |
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | |
names.stream().forEach( | |
name -> register(registry, name) | |
); | |
} | |
private void register(BeanDefinitionRegistry registry, String name){ | |
BeanDefinition bd = new RootBeanDefinition(SampleService.class); | |
bd.getConstructorArgumentValues() | |
.addGenericArgumentValue(name); | |
registry.registerBeanDefinition(name, bd); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@prinal10 were you able to figure this out?