Home > Web Front-end > JS Tutorial > Spring Boot for JSP development dynamically creates beans

Spring Boot for JSP development dynamically creates beans

韦小宝
Release: 2018-01-18 09:48:21
Original
1921 people have browsed it

This article mainly introduces JSP JSP information related to the dynamic creation of beans in the development of Spring Boot. Friends who are interested in JSP can refer to this article

Spring Boot for JSP development dynamically creates Bean

1. Create

a through the annotation @Import import method and create a new 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); 
    } 
  } 
}
Copy after login

b. Add @Import to the configuration class to introduce the above class

@Import(MyImportBeanDefinitionRegistrar.class) 
public class TestConfig{ 
}
Copy after login

c. After this operation, you can use spring

The above is a simple example of dynamically creating a bean in Spring Boot in JSP. If you have any questions, please leave a message or go to the community of this site for discussion! !

Related recommendations:

Detailed introduction to Spring boot and an example of adding jsp support configuration

Detailed introduction to jsp page jump

The parameters of the form submitted in jsp are encapsulated into a method

The above is the detailed content of Spring Boot for JSP development dynamically creates beans. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template