Spring simplifies the next concept of Java: Tangent-based declarative programming
3. Application plane
The purpose of dependency injection is to keep the components of a collaborative component loosely coupled, while AOP programming allows you to separate functionality across applications to form reusable components .
AOP aspect-oriented programming is defined as a technique that drives applications to isolate concerns. The system consists of a number of different components, each of which, in addition to being responsible for a particular function, takes on additional responsibilities, such as logging, transaction management, security, and so on, which are often incorporated into their core business logic, which is collectively referred to as horizontal concerns because they always span the components of the system.
Spreading this code across multiple components leads to double complexity:
- If you want to modify the logic of the focus, you must modify the relevant implementations of the individual components. Even if you abstract these concerns into a separate module, the other modules simply invoke the method, but the invocation of the method repeats itself in each module (coupling);
- Component code becomes chaotic because of code that is not related to its core business.
Suppose you need to use the Bard service class to record all the deeds of the Knight (Braveknight) and to establish the Minstrel (Bard) class.
Package com.test.knights; public class Minstrel {public void Singbeforequest () { System.out.println ("Fa la La; The knight is so Brave! "); } public void Singafterquest () { System.out.println ("Tee hee he; The Brave Knight do a Quest ");}
Let's make the appropriate adjustments for Braveknight to use minstrel
Package com.test.knights; public class Braveknight implements Knight { private Quest Quest; Private Minstrel Minstrel; Public Braknight (Quest Quest, Minstrel Minstrel) { this.quest = Quest; This.minstrel = Minstrel; } public void Embrakonquest () throws Questexception { minstrel.singbeforequest (); Quest.embrak (); Minstrel.singafterquest (); }}
This achieves the desired effect: The knight performs the Singbeforequest () method before the task, executes the Singafterquest () method after the task. But is it really the knight's responsibility to manage the Bard? It is clearly illogical for a knight to provide a bard when he relies on injection. Bard should be independent of himself, he has his own duties.
Improved, minstrel is abstracted as a tangent, and what you do is declare it in the spring configuration file.
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xsi:sche malocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd "> <bean id =" Knight "class =" Com.test.knight.BraveKnight "> <constructor-arg ref = "Quest"/> </bean> <bean id = "quest" class = "Com.test.knight.SlayDragonQuest"/> <! ---add--<bean id = "Minstrel" class = "Com.test.knight.Minstrel"/> <aop:config> <aop:as Pect ref= "Minstrel" > <aop:pointcut id= "Embark" expression= "Execution (* *.embrakonquest (..))"/> <aop:before pointcut-ref= "Embark" > method= "singbeforeqUest "/> <aop:after pointcut-ref=" Embark "> method=" singafterquest "/> </aop:aspect> </aop:config></beans>
The minstrel Bean is declared here as a tangent, and pointcut defines a type of pointcut embark, which means that the Embrakonquest method of any return type, arbitrary object invocation method, and any incoming parameter is valid.
<aop:before> called the,<aop:after> notification is called a post-notification. Get two important points from this example:
- Minstrel is still a pojo, and no code indicates that it will be used as a facet.
- Minstrel can be applied to braveknight, and Braveknight does not need to be called in the display. In fact, Braveknight completely unaware of the existence of minstrel.
Spring Tour (2)