Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the injection mode of Springbean?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the injection method of Springbean, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Spring injection methods can be divided into three categories: xml injection, annotation injection and BeanDefinition injection; usage can be divided into three types, but the underlying implementation code is unified BeanFactory, these three are also related to xml injection and annotation injection are interfaces that rely on BeanDefinition extensions, and annotations are also transferred from xml. Let's simply take a look at these three ways of writing.

XML injection

Before the springboot framework came out, xml configuration was widely used, the configuration process was cumbersome, but less intrusive to the code, configuration and code separation operations.

Entity definition

Define two properties id,name, and implement the get/set method, override the toString method, easy to see the print results.

Public class UserXml {private String id; private String name; public String getId () {return id;} public void setId (String id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} @ Override public String toString () {return "User {" id=' "id'\'", name=' "name'\'}';}}

Xml definition

Create a spring-bean.xml file under the new directory META-INF under resources, and fill in the corresponding bean configuration. Bean needs to configure id or name values, the IOC container is unique, the entity path defined by class configuration, and the corresponding property settings initialization attributes.

Output

Create a BeanFactory object and instantiate it with ClassPathXmlApplicationContext. Simply say that BeanFactory is the underlying foundation of the IOC container. It can be said that the IOC container is BeanFactory,ClassPathXmlApplicationContext, which is the functional extension of the IOC container. ClassPathXmlApplicationContext needs to pass in the path of the resource file, and the result output is obtained when the concrete entity class is obtained through the getBean method.

/ / xml injection BeanFactory classPathXmlApplicationContext = new ClassPathXmlApplicationContext ("classpath:/META-INF/spring-bean.xml"); UserXml userXml = classPathXmlApplicationContext.getBean (UserXml.class); System.out.println ("userXml XML injection object:" userXml)

Annotation injection

Annotations only appeared after spring2.0.3, and a large number of applications were gradually accepted by people with the popularity of springboot. Its main advantage is that it is easy to operate, can be identified as a bean component through simple annotations, and extends various levels of annotations, such as @ Service, @ Service, @ Repository, which are all derived from @ Component annotations.

Entity definition

Entities work similarly with XML entities. Here a new class is created to distinguish between different bean implementations.

Public class UserAnnotation {private String id; private String name; public String getId () {return id;} public void setId (String id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} @ Override public String toString () {return "User {" id=' "id'\'", name=' "name'\'}';}

Annotation configuration

Normally, we use the standard annotation @ Configuration for scan injection. We can directly configure the class here, instantiate the bean component in this class, and initialize it.

@ Configurationpublic class UserConfiguration {@ Bean public UserAnnotation userAnnotation () {UserAnnotation userAnnotation = new UserAnnotation (); userAnnotation.setId ("11"); userAnnotation.setName ("java Circle"); return userAnnotation;}}

Output

AnnotationConfigApplicationContext is also an implementation of BeanFactory, which is similar to ClassPathXmlApplicationContext, except that the loading channel is different. Register the defined configuration class with the IOC container and call the register method. It should be noted here that the next step is to call the refresh method to load the bean, and then obtain the specific entity through getBean for output.

/ / comment injection AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext (); annotationConfigApplicationContext.register (UserConfiguration.class); annotationConfigApplicationContext.refresh (); UserAnnotation userAnnotation = annotationConfigApplicationContext.getBean (UserAnnotation.class); System.out.println ("UserAnnotation Note injection" userAnnotation)

BeanDefinition injection

BeanDefinition is the underlying implementation of BeanFactory, including the methods mentioned above, and the underlying implementation is also based on BeanDefinition. A bean component corresponds to a BeanDefinition, but it will not be used in practice, just for reference.

Entity definition

Entities work similarly with XML entities. Here a new class is created to distinguish between different bean implementations.

Public class UserBeanDefinition {private String id; private String name; public String getId () {return id;} public void setId (String id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} @ Override public String toString () {return "User {" id=' "id'\'", name=' "name'\'}';}}

Output

Instantiate a constructor through BeanDefinitionBuilder's genericBeanDefinition, and the passed parameter is the entity class, which is initialized after construction, and then BeanDefinition declares to call the getBeanDefinition method, and returns the specific parameters of bean through getPropertyValues to finish the output.

/ BeanDefinition injection BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.genericBeanDefinition (UserBeanDefinition.class); definitionBuilder.addPropertyValue ("id", "11"); definitionBuilder.addPropertyValue ("name", "java circle"); BeanDefinition beanDefinition = definitionBuilder.getBeanDefinition (); String beanClassName = beanDefinition.getBeanClassName (); MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues (); String id = mutablePropertyValues.getPropertyValue ("id"). GetValue (). ToString (); String name = mutablePropertyValues.getPropertyValue ("name"). GetValue (). ToString () System.out.println ("BeanDefinition injection object UserBeanDefition {id=" id ", name=" name "}")

Result output

The output results are xml injection, annotation injection and BeanDefinition injection.

UserXml XML injection object: User {id='11', name=' java circle'} UserAnnotation annotation injection User {id='11', name=' java circle'} BeanDefinition injection object UserBeanDefition {id=11,name=java circle}

After reading the above, do you have any further understanding of the injection method of Springbean? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report