1th Chapter-spring Tour-Simplifying the Java development of spring

Source: Internet
Author: User
Tags aop

Simplifying the Java development of Spring 1.1 introduction

In order to reduce the complexity of Java development,Spring has adopted the following 4 key strategies to simplify the javabean of features that differ from EJB:

    • Pojo-based lightweight and minimally invasive programming
    • Loose coupling via Dependency injection and interface-oriented
    • Declarative programming based on facets and conventions
    • Reduce boilerplate code with facets and templates
1.2 Dependency Injection (DI)

? What is dependency injection in the end? Any meaningful application will consist of two or more classes that work together to accomplish specific business logic. Traditionally, no object is responsible for managing references to objects that it relies on (that is, the objects that it depends on). This will result in highly coupled and hard-to-test code.

Example:

Knight to Adventure

/** * 骑士 */publicinterface Knight {    publicvoidembarkOnQuest();}
/** * * 探险类型 */publicinterface Quest {    public  voidembark();}
/** * 勇敢的骑士 */publicclassimplements Knight {    private Quest quest;    //quest被注入进来(构造注入)    publicBraveKnight(Quest quest) {        this.quest = quest;    }    publicvoidembarkOnQuest() {        quest.embark();    }}

The quest in this example is passed through the construction parameters, where the point is that braveknight is not coupled with any particular quest implementation. For it, the quest for the challenge requires a quest interface, So what kind of adventure is that? That's the biggest benefit of di--loose coupling.

When you replace a dependency, you only need to give the quest interface an implementation class.

/**   * Task Implementation class   */ public  class  Questimpl implements  quest{//engage in Start method  public void  embark  () {system.. println     ( "start Adventure" ); }}
/**     * 进行依赖的测试     */    @Test    publicvoidKinghtShouldEmbarkOnQuest() {        //向上转型        newQuestImpl();        //注入Quest的实现类QuestImpl        newBraveKnight(questimpl);        //执行方法        knight.embarkOnQuest();    }
1.3 Configuration implementation with spring XML

Application.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--注入BraveKnight--><bean id="braveKnight"class="BraveKnight">    <constructor-arg ref="quest"/></bean>    <!--注入QuestImpl-->    <bean id="quest"class="QuestImpl"></bean></beans>

Perform the spring test

publicstaticvoidmain(String[] args) {        //加载配置文件 加载Spring的上下文        newClassPathXmlApplicationContext("classpath:application.xml");        //获取bean        BraveKnight bean = app.getBean(BraveKnight.class);        //调用方法        bean.embarkOnQuest();    }
1.4 Application Plane Programming (AOP)

? As described above, Di enables loosely coupled software organizations that work together, while aspect-oriented programming (AOP) allows you to separate functionality throughout your application to form reusable components.

? Common AOP extraction components are: log module, transaction module, security module, etc.

? AOP enables services to be modular and declaratively applied to the components they need to influence. The benefit of this is that these components can be more cohesive and focus more on their core business, without needing to understand the complexities of designing system services, in short, AOP ensures the simplicity of Pojo. We can think of facets as a shell that covers many components, as shown in:

Example: or in the case of a knight just now, we are familiar with the Bard and the service class will record all the deeds of the knight.

The main classes are as follows:

//吟游诗人作为切面publicclass Minstrel {    publicvoid  singBeforeQuest(){        //探险之前        System.out.println("Before: Fa la la ,the Knight is so breave!");    }    publicvoid  singAfterQuest(){        //探险之后        System.out.println("After: Tee hee hee ,the breave Knight!");    }}

An example of an AOP XML configuration is as follows:

<?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:schemalocation="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.xsd "> <!--injection braveknight--> <bean id="Braveknight" class="Braveknight"> <constructor-arg ref="Quest"/> </bean> <!--injection questimpl--> <bean id="Quest" class="Questimpl"/> <!--AOP instances--<!--injection minstrel--> <bean id="Minstrel" class="Com.aop.Minstrel"/> <aop:config> <!--declarations--<aop:aspect ref="Minstrel"> <!--defining pointcuts Here is the knight's method--<aop:pointcut id="Embark"expression="Execution (* Questimpl.embark (..))"/> <!--statement Pre-notification-<aop:before pointcut-ref="Embark"Method="Singbeforequest"/> <!--statement Post notification-<aop:after pointcut-ref="Embark"Method="Singafterquest"/> </aop:aspect> </aop:config></beans>

The main configuration of AOP involves tangent and tangent, we first define the bean of the cross-sectional class and inject it, and then we need the method of the relevant class as the tangent point, the main usage of AOP is: pre-notification, post-notification, surround notification , The implementation of the code used to add the related facets behind the front of the core code.

1th Chapter-spring Tour-Simplifying the Java development of spring

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.