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

How to realize the inheritance of Spring Bean

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to realize the inheritance of Spring Bean". In the daily operation, I believe that many people have doubts about how to realize the inheritance of Spring Bean. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to realize the inheritance of Spring Bean". Next, please follow the editor to study!

Bean definition inheritance

Bean definitions can contain a lot of configuration information, including constructor parameters, property values, container specific information such as initialization methods, static factory method names, and so on. The definition of the child bean inherits the configuration data defined by the parent. Child definitions can override some values as needed, or add other values.

Inheritance defined by Spring Bean has nothing to do with inheritance of the Java class, but the concept of inheritance is the same. You can define a definition of a parent bean as a template and other child bean to inherit the desired configuration from the parent bean. When you use XML-based configuration metadata, indicate the definition of the child bean by using the parent attribute and specifying the parent bean as the value of that property.

1. XML

Here is the configuration file Bean.xml, in which we define a "helloWorld" bean with two properties, message1 and message2. Then, use the parent attribute to define "helloIndia" bean as the child of "helloWorld" bean. This child bean inherits the properties of message2, overrides the properties of message1, and introduces a property message3.

2. Beanpublic class HelloWorld {private String message1; private String message2; public void setMessage1 (String message) {this.message1 = message;} public void setMessage2 (String message) {this.message2 = message;} public void getMessage1 () {System.out.println ("World Message1:" + message1);} public void getMessage2 () {System.out.println ("World Message2:" + message2);} public class HelloIndia {private String message1; private String message2 Private String message3; public void setMessage1 (String message) {this.message1 = message;} public void setMessage2 (String message) {this.message2 = message;} public void setMessage3 (String message) {this.message3 = message;} public void getMessage1 () {System.out.println ("India Message1:" + message1);} public void getMessage2 () {System.out.println ("India Message2:" + message2) } public void getMessage3 () {System.out.println ("India Message3:" + message3);}} 3. Main function import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("Beans.xml"); HelloWorld objA = (HelloWorld) context.getBean ("helloWorld"); objA.getMessage1 (); objA.getMessage2 () HelloIndia objB = (HelloIndia) context.getBean ("helloIndia"); objB.getMessage1 (); objB.getMessage2 (); objB.getMessage3 ();} once you have created the source code and the bean configuration file, we can run the application. If your application is working properly, the following information will be output: World Message1: Hello Worldwide World Message2: Hello Second wordsIndia Message1: Hello IndiaIndia Message2: Hello Second Worldlings India Message3: Namaste India!

You can observe here that we didn't pass message2 when we created the "helloIndia" bean, but because of the inheritance of the Bean definition, it passed message2.

Bean definition template

You can create a Bean definition template that can be used by other sub-bean definitions without much effort. When defining a Bean definition template, you should not specify the properties of the class, but should specify an abstract property with a true value, as follows:

The parent bean itself cannot be instantiated because it is incomplete and it is explicitly marked as abstract. When a definition is abstract, it is used only as a pure template bean definition, as the parent definition of a child definition.

At this point, the study of "how to realize the inheritance of Spring Bean" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report