In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "detailed introduction of IoC in Spring". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the detailed introduction of IoC in Spring".
IoC in Spring:
1) concept: leave the creation, initialization and destruction of objects to the spring container. The lifecycle of the object is controlled by the spring container. 2) purpose: in order to solve the dependency relationship between multiple objects. 3) principle: 1) describe the dependency relationship between bean and bean through configuration files (or annotations). 2) instantiate bean through the reflection mechanism of dynamic proxy and java, and establish the dependency relationship between bean. 4) there are three ways for spring containers to create objects (instantiation of bean): 1) use Spring's IoC to create objects: call the default constructor (default) 1 > specify the class property of bean 2 > class requires a default empty constructor To instance object: HelloWorld helloWorld = (HelloWorld) context.getBean ("helloWorld") 2) use factory mode to create objects in Spring: use static factory method 1 > specify outside class attribute 2 > specify static factory method to create bean instance through factory-method property Note: it is equivalent to calling HelloWorldFactory.createInstance () Public class HelloWorldFactory {public static HelloWorld createInstance () {return new HelloWorld () }} get the instance object: HelloWorld helloWorld = (HelloWorld) context.getBean ("helloWorld") Use instance factory method 1 > define a factory class 2 > specify a factory class through the factory-bean attribute Specify the non-static factory method of the factory class through the factory-method attribute Note: it is equivalent to calling new HelloWorldFactory.createInstance () Public class HelloWorldFactory {public HelloWorld createInstance () {return new HelloWorld () }} get the instance object: HelloWorld helloWorld = (HelloWorld) context.getBean ("helloWorld") 3) use Spring's IoC to create objects VS use factory mode to create objects when the created object needs to be modified (eg: new attribute): Spring's IoC only needs to modify the corresponding class of the object. Factory mode requires modification or addition of factory methods. 5) DI (dependency injection): assign values to the attributes of the object 1) use the setter method of the attribute for injection: note: after instantiating bean by calling the no-parameter constructor or no-parameter static factory method, and then calling the setter method of the bean, you can achieve setter-based dependency injection. For example: List1 List3 Map1 Value1 value2 2 ) use constructor injection: do this by calling the constructor with parameters: note: 1 > if there is no tag in the bean of spring's configuration file Call the default constructor 2 > if there is a tag in the bean of the configuration file of spring, the element determines the unique construction method. The attribute index of the tag refers to the location of the parameter, and starting from 0, type refers to the type of the parameter. If you do not write it, you can still determine the unique construction method. You can omit the type attribute value to assign values to basic types, ref assigns values to reference types, for example: Thank you for your reading. The above is the content of "detailed introduction of IoC in Spring". After the study of this article, I believe you have a deeper understanding of the detailed introduction of IoC in Spring, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.