Spring Learning (II) -- a preliminary understanding of AOP in Spring

Source: Internet
Author: User

Spring is too important to me. I need to use Spring for a web-related project. Every time I look at Spring-related knowledge, I always feel a little confused. I don't have a good system to study, now, take some time to learn about Spring. I don't know why I have an inexplicable liking for Spring, maybe because of his name and hope. Summary and experiences in Spring learning. This article introduces the understanding of AOP when I first studied Spring. I hope I can continue to learn it later. Please read it carefully. This article was first published by livestock TT in the blog Park. Please keep the integrity of the article and note: Author: livestock TT. Link: http://www.cnblogs.com/xt0810/p/3596347.html [related articles] Spring Learning (a) -- Introduction to dependency injection in Spring [AOP Introduction] side-oriented programming (aspect-oriented programming, AOP, (Aspect-Oriented Programming) is a term in computer science and refers to a programming paradigm. This model is based on a language structure called the side (aspect), and the side is a new modular mechanism, used to describe the cross-concern (crosscutting concern) distributed in objects, classes, or functions ). The concept on the side is derived from the improvement of object-oriented programming, but it is not limited to this. It can also be used to improve traditional functions. The aspect-related programming concepts also include meta-Object Protocol, subject, mixin, and delegate. [Aspect-Oriented] 1. What does AOP do? Dependency injection ensures loose coupling of software components that collaborate with each other, and AOP programming allows you to separate functions distributed throughout the application to form reusable components. That is to say, AOP modularize some common services and uses these components to other business components in declarative manner. The result is that each business component only cares about its own business logic and does not need to know some common service components. This ensures a higher cohesion. Ii. Do I have trouble using AOP? Because the system has many different components, each component is responsible for a specific function. We hope that each component only cares about its core functions. However, in the system, there are some components such as the Log Module, these components, such as the transaction management and security module, are frequently integrated into other core business logic components. These commonly used components are dispersed into multiple other components, the trouble is that if these commonly used service components change, we need to modify them in multiple other components. This makes our component code messy because we have inserted service components unrelated to our core business. 3. What are the advantages of using AOP? In my understanding, component A does not care about other commonly used service components B. However, when component A uses component B, it is not called by component A itself, but through other methods such as configuration, such as Spring through the xml configuration file. In this way, A does not need to know what service component B is like. Love does not exist. It has nothing to do with. A only cares about its own business logic. When A uses B, the configuration file is executed, which has nothing to do with the specific A component. Iv. Case 1: we will continue to use the example of Person Car in the previous article to describe the code without using the AOP idea. Now we add an Assistant class. This is an Assistant. What we need to do is register before I drive and receive the Assistant when I return when I drive. If we use the encoding method of the general idea, we need to call the two methods of the Assistant class in the Person class, the following code. However, the problem is: whether the management Assistant is the responsibility of the Person class. The Assistant should do his/her own internal tasks and do not need to run the Person command. When the Person leaves, he registers, this is his job and does not require a Person reminder. Similarly, because the Person class needs to use Assistant, he must inject Assistant into his class, which makes his code complicated. What should we do if one Person has no Assistant? 1. person. java copy code 1 public class Person {2 private Car car; 3 private Assistant assistant; 4 public Person (Car car, Assistant assistant Assistant) {// constructor injection, passed in car, that is, an interface that all models must implement. 5 this. car = car; // here, we can respond to the implementation of any vehicle, such as Audi and BMW. 6 this. assistant = assistant; 7} // The Person class is not coupled with any specific type of Car. For a Person, you only need to implement the Car interface for any specific Car. The specific model is irrelevant to the Person. 8 public void driver () {// It can be seen from here that if no AOP is used, that is, everyone needs an assistant, this is unreasonable. 9 assistant. beforeDepart (); // The Person class uses the Assistant method 10 car. guaDang (); 11 car. caiYouMen (); 12 car. daFangXiang (); 13 assistant. afterBack (); // The Person class uses the Assistant method 14} 15} to copy Code 2. assistant. java copy code 1 public class Assistant {2 public void BeforeDepart () {3 System. out. println ("Hello, you drove Audi today, I have registered"); 4} 5 public void AfterBack () {6 System. out. println ("You're back! Please hand over the car to me, and I can import the database myself "); 7} 8 9} copy code 3. car. java 1 public interface Car {2 public abstract void GuaDang (); 3 public abstract void CaiYouMen (); 4 public abstract void DaFangXiang (); 5} 4. auDi. java copy code 1 public class AuDi implements Car {2 public void GuaDang () {3 System. out. println ("I'm an Audi car, I am mounting a file"); 4} 5 public void CaiYouMen () {6 System. out. println ("I'm Audi, I'm stepping on the accelerator"); 7} 8 public void DaFangXiang () {9 Sys Tem. out. println ("I'm Audi, I'm playing the steering wheel"); 10} 11} copy Code 5. mainTest. java copy code 1 public class MainTest {2 public static void main (String [] args) {3 AuDi audi = new AuDi (); 4 Assistant assistant = new Assistant (); 5 Person boy = new Person (audi, assistant); 6 boy. driver (); 7} 8} copy Code 6. result 1: Hello, you drove Audi today. I have already done registration. 2. I am Audi, I am mounting a file. 3. I am Audi, i'm stepping on the throttling. 4. I'm in Audi. 5. You're back! Please hand over the car to me. I can import the database myself. 5. Case 2: If the code using the AOP idea uses AOP, the Person class does not need to care about the Assistant class, we only need to declare what Assistant needs to do, and the Person class will not directly access the Assistant class method. This article is only about understanding the idea of AOP. How does AOP work in Spring? In the future, you will learn how to use it. 1. person. java copy code 1 public class Person {2 private Car car; 3 public Person () {4 5} 6 public Person (Car car) {// constructor injection, the input is a car, that is, an interface that must be implemented by all models. 7 this. car = car; // here, we can respond to the implementation of any vehicle, such as Audi and BMW. 8} // The Person class is not coupled with any specific type of Car. For a Person, you only need to implement the Car interface for any specific Car. The specific model is irrelevant to the Person. 9 public void driver () {10 car. guaDang (); 11 car. caiYouMen (); 12 car. daFangXiang (); 13} 14} copy Code 2. assistant. java copy code 1 public class Assistant {2 public void BeforeDepart () {3 System. out. println ("Hello, you drove Audi today, I have registered"); 4} 5 public void AfterBack () {6 System. out. println ("You're back! Please hand over the car to me, and I can import the database myself "); 7} 8 9} copy code 3. car. java 1 public interface Car {2 public abstract void GuaDang (); 3 public abstract void CaiYouMen (); 4 public abstract void DaFangXiang (); 5} 4. auDi. java copy code 1 public class AuDi implements Car {2 public void GuaDang () {3 System. out. println ("I'm an Audi car, I am mounting a file"); 4} 5 public void CaiYouMen () {6 System. out. println ("I'm Audi, I'm stepping on the accelerator"); 7} 8 public void DaFangXiang () {9 Sys Tem. out. println ("I'm Audi, I'm playing the steering wheel"); 10} 11} copy Code 5. cartest. xml copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xmlns: aop = "http://www.springframework.org/schema/aop" 5 xmlns: context = "http://www.springframework.org/schema/context" 6 xsi: schemaLocation = "http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.o Rg/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.0.xsd10 http://www.springframework.org/schema/aop11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd> 12 <bean id = "cartest" class = "Person"> 13 <constructor-arg ref = "Car"/> 14 </ bean> 15 <bean id = "Car" class = "AuDi"/> 16 <bean id = "assistant" class = "Assistant"/> 17 <aop: config> 18 <aop: aspect ref = "assistant"> 19 <aop: pointcut id = "ass" expression = "execution (* driver (..)) "/> 20 <aop: before pointcut-ref =" ass "method =" BeforeDepart "/> 21 <aop: after pointcut-ref = "ass" method = "AfterBack"/> 22 </aop: aspect> 23 </aop: config> 24 </beans> copy Code 6. mainTest. java copy code 1 import org. springframework. context. applicationContext; 2 import org. springframework. context. support. classPathXmlApplicationContext; 3 public class MainTest {4 Public static void main (String [] args) {5 ApplicationContext context = new ClassPathXmlApplicationContext ("cartest. xml "); 6 Person boy = (Person) context. getBean ("cartest"); 7 boy. driver (); 8} 9} copy code 7. result 1: Hello, you drove Audi today. I have already done registration. 2. I am Audi, I am mounting a file. 3. I am Audi, i'm stepping on the throttling. 4. I'm in Audi. 5. You're back! Please hand over the car to me. I can import it to the warehouse on my own.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.