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

Example Analysis of IOC Code in Spring

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

Share

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

This article shares with you the content of sample analysis of IOC code in Spring. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Inversion of control (Inversion of Control, abbreviated as IoC) handing over the right to create objects to the framework is an important feature of the framework, not a special term for object-oriented programming. It includes dependency injection (DI) and dependency lookup (Dependency Lookup).

IOC changes the way the program acquires objects, from the initial new of an object to the creation and injection of third-party frameworks. Third-party frameworks typically specify which implementation to inject through configuration, thereby reducing source code coupling.

What are the biggest benefits of ioc?

Because the object generation is defined in XML, when we need to change the implementation of a subclass will become very simple (usually such objects are realistic in a certain interface), as long as modify the XML, so that we can even achieve hot plugging objects (a bit like the USB interface and SCIS hard disk).

According to the way of implementation, IOC can be divided into dependency injection (DI) and dependency lookup.

DI is the main technical way to control inversion, which is divided into setter injection and constructor injection.

Setter injection

After bean is instantiated by calling a no-parameter constructor or static no-parameter factory method, the setter method of bean is called to implement setter injection.

Public class JDBCDataSource () {private String driver; public void setDriver (String driver) {Class.forName (driver); this.driver=driver;}}. .

Next, implement the setter injection configuration xml file

Constructor injection

This is achieved by calling the constructor with parameters, and when the bean is instantiated, the container executes the constructor based on the parameter type.

Public class OracleUserDao implements UserDao {private JDBCDataSource dataSource; public OracleUserDao (JDBCDataSource dataSource) {this.dataSource=dataSource;}}.

Specify injection by construction parameter index

Thank you for reading! This is the end of this article on "sample Analysis of IOC Code in Spring". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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