Spring之旅

Spring是一个开源框架,Spring根本使命:简化Java开发。

为了简化java开发的复杂性,Spring使用如下4种策略:

1、基于POJO(Plain Old Java Object  简单Java对象 )的轻量级和最小侵入性编程。

2、通过依赖注入和面向接口实现松耦合。

3、基于切面和惯例进行声明式编程。

4、通过切面和模板减少样板式代码。

这里以一个骑士出征的例子来解释一些相关概念

首先定义一个骑士接口,其中有一个embarkOnQuest方法。

package hoo.knights;

public interface Knight {

    void embarkOnQuest();

}

然后再定义一个远征行动接口。

package hoo.knights;

public interface Quest {

void embark();

}

下一步定义一个拯救少女行动类

package hoo.knights;

public class RescueDamselQuest implements Quest{

@Override

    public void embark() {

     System.out.println("Embarking on a quest to rescue the damsel");

}

}

再定义一个屠杀巨龙行动类

package hoo.knights;

import java.io.PrintStream;


public class SlayDragonQuest implements Quest{

private PrintStreamprintStream;


    public SlayDragonQuest(PrintStream printStream){

this.printStream = printStream;

}

@Override

    public void embark() {

printStream.println("Embarking on quest to slay the dragon");

}

}


接下来定义一个英勇的骑士类实现骑士接口

package hoo.knights;

public class BraveKnight implements Knight{

private Questquest;


    public BraveKnight(Quest quest){

this.quest = quest;

}

@Override

    public void embarkOnQuest() {

quest.embark();

}

}

到这里可能就有人将BraveKnight类写成:

package hoo.knights;

public class BraveKnight implements Knight{

private Questquest;

    public BraveKnight(){

quest = new RescueDamselQuest ();

}

@Override

    public void embarkOnQuest() {

quest.embark();

}

}

但是有一种情况:骑士不仅仅可以去杀龙还可以去拯救少女这里将它写死就不太合适,采用传递一个Quest对象,只要实现了Quest类的接口,任何类都可以传进来,骑士就可以去做很多行动,不是只执行一个任务。

这是依赖注入(DI)的一种方式:构造器注入

同样SlayDragonQuest类中也有依赖注入:将一个PrintStream对象注入到SlayDragonQuest中。

骑士出征之前和之后需要法师的吟唱,骑士类拥有法师类对象似乎不是一个好的选择,法师不应该被骑士拥有,有的骑士也不想法师吟唱这里先定义一个法师类。

package hoo.knights;

import java.io.PrintStream;

public class Minstrel {

private PrintStream printStream;

public Minstrel(PrintStream printStream){

this.printStream = printStream;

}

public void singBeforeQuest(){

printStream.println("Fa la la,the knight is so brave!");

}

public void singAfterQuest(){

printStream.println("Tee hee hee , the brave knight did embark on a quest");

}

}

法师的吟唱是骑士的出征非关键因素,这里利用AOP来实现法师吟唱模块化。

有多个Quest对象可以被注入到BraveKnight中,到底选择哪一个来注入呢??

这里利用XML来正确装配这些对象。

装配的方式有:自动装配、XML显式装配、Java显式装配。

先定义knight.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">

<bean id="knight" class="hoo.knights.BraveKnight">

<constructor-arg ref="quest"/>

</bean>

<bean id="quest" class="hoo.knights.SlayDragonQuest">

<constructor-arg value="#{T(System).out}"/>

</bean>

</beans>

使用<bean></bean>声明一个bean 其中id为自己指定的名字 class为bean的类

<constructor-arg> 元素为构造器参数 ref为依赖的bean,第一个constructor元素就决定了将一个id为quest的bean注入到id为knight的对象中。第二个constructor元素中 value代表将一个System.out值传入构造器。

第一个constructor元素就决定了骑士去杀龙而不是拯救少女。

等等,法师吟唱在哪???

<?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">

<bean id="knight" class="hoo.knights.BraveKnight">

<constructor-arg ref="quest"/>

</bean>

<bean id="quest" class="hoo.knights.SlayDragonQuest">

<constructor-arg value="#{T(System).out}"/>

</bean>

<bean id="minstrel" class="hoo.knights.Minstrel">

<constructor-arg value="#{T(System).out}"/>

</bean>

<aop:config>

<aop:aspect ref="minstrel">

<aop:pointcut id="embark" expression="execution(* *.embarkOnQuest(..))"/>

<aop:before pointcut-ref="embark" method="singBeforeQuest"/>

<aop:after pointcut-ref="embark" method="singAfterQuest"/>

</aop:aspect>

</aop:config>

</beans>

<aop:aspect ref="minstrel">这一句将法师类声明为一个切面(AOP)。

pointcut定义了一个切入点,before决定在切入点之前做什么,after决定在切入点之后做什么。

来到验证阶段,看看骑士到底干了啥事?

package hoo.knights;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class KnightMain {

public static void main(String[] args)throws Exception{

ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("minstrel.xml");

//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("knight.xml");

        Knight knight = context.getBean(Knight.class);

knight.embarkOnQuest();

context.close();

}

}

ClassPathXmlApplicationContext 从xml文件中读取bean装配信息,这玩意叫应用上下文。。

第一个决定了法师要出来吟唱,通过控制台可以看到法师出来吟唱了,并且骑士执行的是杀龙的行动。

法师吟唱

注释的第二行决定法师不出来吟唱,执行的也是杀龙任务,也可以将xml里面的配置改为拯救少女,可能这个骑士他不喜欢拯救少女吧。


法师不吟唱

这个例子 初步展示了DI、AOP的思想。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容