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

Analysis of IOC instance in Spring

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article editor for you to introduce in detail "IOC case analysis in Spring", the content is detailed, the steps are clear, and the details are handled properly. I hope this "IOC case analysis in Spring" article can help you solve your doubts.

The derivation of IOC 1.1, simulates a normal business process of querying information:

① mapper layer: because there is no connection to the database, here we write an implementation class of mapper to simulate data queries

Public interface PerMapper {void getPerInfo ();} public class StudentMapperImpl implements PerMapper {@ Override public void getPerInfo () {System.out.println ("I am a student");}

② service layer: the function of service is to query people's information.

Public interface PersonService {void getPersonInfo ();} public class PersonServiceImpl implements PersonService {private PerMapper studentMapper = new StudentMapperImpl (); @ Override public void getPersonInfo () {studentMapper.getPerInfo ();}}

③ contorller layer

Import service.PersonService;import service.impl.PersonServiceImpl;public class IOCTest {public static void main (String [] args) {PersonService service = new PersonServiceImpl (); service.getStudentInfo ();}} 1.2, multiple types of queries

① mapper, adding teacher implementation classes

Public class TeacherMapperImpl implements PerMapper {@ Override public void getPerInfo () {System.out.println (I am a teacher);}}

②, what should we do when we inquire about teachers and students at the same time?

Public class PersonServiceImpl implements PersonService {private PerMapper student = new StudentMapperImpl (); private PerMapper teacher = new TeacherMapperImpl (); @ Override public void getPersonInfo () {student.getPerInfo (); teacher.getPerInfo ();}}

What if the requirements change again? What can I do if I only need the teacher's information?

There is no doubt: two approaches

One is to change the getPersonInfo () of PersonServiceImpl directly.

@ Override public void getPersonInfo () {/ / student.getPerInfo (); teacher.getPerInfo ();}

The second is to extend the interface of the service layer to provide interfaces for teachers and students to query:

Public interface PersonService {void getPersonInfo (); void getPersonInfo1 ();} public class PersonServiceImpl implements PersonService {private PerMapper student = new StudentMapperImpl (); private PerMapper teacher = new TeacherMapperImpl (); @ Override public void getPersonInfo () {teacher.getPerInfo ();} @ Override public void getPersonInfo1 () {student.getPerInfo ();}}

There seems to be nothing wrong with ⑤, right? What if there are a hundred kinds of people? People will go crazy if they need to change. This must be unreasonable!

1.3. Optimize the query mode

① smart kids can think of stripping out the object of the query: adding the set () method, not implementing the interface, but only doing the reserved work.

Public class PersonServiceImpl implements PersonService {private PerMapper per; public void setPer (PerMapper per) {this.per = per;} @ Override public void getPersonInfo () {per.getPerInfo ();}

② controller layer implementation:

Public class IOCTest {public static void main (String [] args) {PersonServiceImpl service = new PersonServiceImpl (); / / Student service.setPer (new StudentMapperImpl ()); service.getPersonInfo (); / / teacher service.setPer (new TeacherMapperImpl ()); service.getPersonInfo ();}}

③ summary: this way is to give the initiative to the caller, the program does not have to worry about how to create, how to achieve. It is only responsible for providing an interface.

We no longer manage the creation of objects, pay more attention to the implementation of the business, and the coupling is greatly reduced, which is the prototype of IOC!

1.4.The essence of IOC

Inversion of control is a way to produce or acquire specific objects through description (XML or annotations) and through third parties. It is IOC container that implements control inversion in Spring, which is implemented by dependency injection (Dependency Injection,DI).

After reading this, the article "IOC instance Analysis in Spring" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

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