Spring Boot for JSP development dynamically creates Bean and Spring Bean
Spring Boot for JSP development dynamically creates beans
1. Create an Import using annotation @ Import
A. Create the MyImportBeanDefinitionRegistrar registration center.
Java code
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; import web0.services.Myservice; public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { protected String BEAN_NAME = "myservice"; public void dynamicConfiguration() throws Exception { } @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { if (!registry.containsBeanDefinition(BEAN_NAME)) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(Myservice.class); beanDefinition.setSynthetic(true); registry.registerBeanDefinition(BEAN_NAME, beanDefinition); } } }
B. Add @ Import to the configuration class to introduce the above class.
@Import(MyImportBeanDefinitionRegistrar.class) public class TestConfig{ }
C. After this operation, you can use spring to obtain the bean.
The above is a simple example of Spring Boot dynamic Bean creation in JSP. If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you, thank you for your support!