Spring Boot for JSP development dynamically creates Bean and Spring Bean

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.