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

Introduction to Spring-- IoC and AOP

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

Share

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

Unlike the MyBatis series, before officially getting started with Spring, let's take a look at two core concepts about Spring, IoC (Inverse of Control) control inversion and AOP () aspect-oriented programming.

1.IoC (Inversion of Control) control inversion

What is a reversal of control? To put it so popularly, we usually write code that new one class directly in that class when it associates another class, for example:

1 package day_30_spring; 2 3 / * * 4 * @ author Yu Linfeng 5 * 6 * 30 October 2016 7 * / 8 public class People {9 private Eat eat;10 11 / * * 12 * create an object instance of Eat in the constructor 13 * / 14 public People () {15 eat = new Eat (); 16} 17 18}

Now we "reverse" the control of the object and hand it over to the third-party container, that is, the Spring container to manage it for us, so that we are no longer tightly coupled to the Eat object in the People code. Look at the following code:

1 package day_30_spring; 2 3 / * * 4 * @ author Yu Linfeng 5 * 6 * 30 October 2016 7 * / 8 public class People {9 private Action eat;10 11 / * * 12 * create an object instance of Eat in the constructor 13 * / 14 public People (Action eat) {15 this.eat = eat;16} 17 18}

The Eat class inherits from the Action interface, which actually implements control inversion through dependency injection (DI,Dependency Inversion). To inject by construction. Instead of relying on the specific Action interface implementation, we just need to pass in an Action interface class. In contrast to the previous "traditional" code, which relies heavily on Action's concrete implementation of the Eat class, this code actually decouples the code. Spring can be constructed in two ways, one is the constructor injection we mentioned above, and the other is set method injection. So, IoC is very simple, the purpose is decoupling, the means is through dependency injection. The Spring container helped us solve everything.

2.AOP (Aspect Oriented Programming) aspect-oriented programming

So what is aspect-oriented programming? If you have ever understood the proxy pattern, it is easier to understand AOP, and the principle of AOP is actually implemented through dynamic proxies. Let's take a look at the following example: there will be a log record before the operation of the database, and there will be a log record after the operation of the database.

1 package day_30_spring; 2 3 / * * 4 * @ author Yu Linfeng 5 * 6 * 30 October 2016 7 * / 8 public class People {9 private Logger logger = new Logger (); 10 11 public void insert (int I) {12 logger.beforeInsert (); / / log record before insertion 13 / insert a data 14 logger.afterInsert () / / inserted log records 15} 16}

"single responsibility" tells us, is it good to write in this way? Is logging what People should do? Shouldn't it only be responsible for inserting data? The right thing to do is that there are only a few lines of code in the People class:

1 package day_30_spring; 2 3 / * * 4 * @ author Yu Linfeng 5 * 6 * 30 October 2016 7 * / 8 public class People {9 10 public void insert (int I) {11 / / insert a piece of data here 12} 13}

So how do we log first in the insert method? We can do this with a dynamic proxy, that is, to create an object instance of the proxy class, and Spring has done this for us, which only needs to be configured in a few words.

1 2 3 4 5 6 7 8 9 10 11

In fact, we can achieve the functionality of our "bad" code in the first place, and it's more beautiful. Here we only have a simple understanding of IoC and AOP. Later, like MyBatis, we will learn from the first step of configuration, then learn how to use it, and finally understand the principle of the framework source code and so on.

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

Network Security

Wechat

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

12
Report