《Spring in action》之Spring之旅

Spring框架作用是简化java开发的复杂性。下面是spring in action 对spring初步介绍。

一、主要有4种关键策略:

1. 基于POJO的轻量级和最小侵入性编程 。

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

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

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

二、Spring的核心
Spring的两个重要核心是控制反转Inversion of Control (IoC)也叫依赖注入dependency injection (DI)和面向切面编程Aspect-Oriented Programming (AOP) 
1. 控制反转/依赖注入
目标对象依赖的其它对象会通过被动的方式传进来,而不是目标对象自己创建。
依赖注入方式:
a). 构造器注入。控制反转最大的收益--松散耦合。
构造器中注入的对象可以定义为接口、这样就可以传入不同的实现类对象从而实现松散耦合。
下面是实现代码:
public class People {
    private Play play;
    /**
     * play 接口注入进构造器
     * @param play
     */
    public People(Play play) {
        this.play = play;
    }
    public void doSomething(){
        play.playSomeThing();
    }
}

 现在有一个实现了Play接口的实现类 PlayBasketBall 。

   

public class PlayBasketBall implements Play {
    public void playSomeThing() {
        System.out.println("play basketball。。。。。。。");
    }
}

   我们怎么把PlayBasketBall交给People呢?

   这个问题就涉及到另一个行为:装配。装配就是创建组件之间协作的行为。

  Spring有多种装配方式:XML方式,java注解方式等等。

  我们用xml方式装配,新建services.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="people" class="com.spring.di.People">
        <constructor-arg ref="playBasketBall"/>
    </bean>

    <bean id="playBasketBall" class="com.spring.di.PlayBasketBall">
    </bean>
</beans>

 对象的创建和组装

Spring应用上下文(Application Context)负责对象的创建和组装。Spring有多种上下文实现。
bean使用Xml文件配置选择ClassPathXmlApplitionContext;如果是基于java的配置就选择AnnotationConfigApplicationContext.
 
代码实现:
public class PlayMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("services.xml");

         People people= context.getBean(People.class);
         //People people= (People) context.getBean("people");
         people.doSomething();
    }
}

 以上就是spring依赖注入的一个认识。

 推荐一篇国外博文IOC/DI:

 2. AOP面向切面编程

 AOP:通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

如上面的People我们可以对doSomething方法前后动态加入一些操作。

列如加入切面:

public class PlayUtil {

    public void playBefore(){
        System.out.println("play before do.........");
    }

    public void playAfter(){
        System.out.println("play after do.........");
    }

}

 配置文件:

<?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/aop
       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="people" class="com.spring.di.People">
        <constructor-arg ref="playBasketBall"/>
    </bean>

    <bean id="playBasketBall" class="com.spring.di.PlayBasketBall">
    </bean>

    <!-- 切面 -->
    <bean id="playUtil" class="com.spring.aop.PlayUtil"/>

    <aop:config>
        <aop:aspect ref="playUtil">
            <!-- 切点 expression表达式AspectJ切点表达式语言-->
            <aop:pointcut id="doSomething" expression="execution(* *.doSomething(..))"/>
            <!-- 前置通知-->
            <aop:before pointcut-ref="doSomething" method="playBefore"/>
            <!-- 后置通知 -->
            <aop:after pointcut-ref="doSomething" method="playAfter"/>
        </aop:aspect>
    </aop:config>

</beans>

 这样的方式可以把某些功能分离出来形成可重用的组件。

3.使用模板消除样板式代码

例如jdbc访问数据库代码。如果每个操作数据库的地方都写重复的连接数据库、关闭连接等操作那就太低效了。

spring的JdbcTemplate就避免了传统的样板式代码。

4.Spring容器

这是Spring框架的核心。负责创建对象、装配对象、配置对象并管理他们的整个生命周期。Spring的容器归为两类。一类是bean工厂、另一类是应用上下文。其实应用上下文也是基于BeanFactory构建。  

   a)应用上下文:
 ClassPathXmlApplicationContext:在所有的类路径(包含jar文件)下查找xml.
 FileSystemXmlApplicationContext:在指定文件系统路径下查找xml.
上下文就绪后、我们就可以通过上下文的getBean()方法从容器中获取bean.

b)bean的生命周期

    1、Spring对bean进行实例化

    2、Spring将值和bean的引用注入到bean对应的属性中

    3、如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBeanName()方法

    4、如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传入

    5、如果bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext方法,将bean所在应用上下文的引用传入进来

    6、如果ean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization()方法

    7、如果bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet()方法,类似,如bean使用init-methon声明初始方法,该方法会被调用

    8、如果bean实现BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization()方法

    9、此时、bean已经准备就绪。他们将一直驻留在应用上下文中、直到该上下文被销毁

              10、如果bean实现了DisposableBean接口,Spring将调用其destory()接口方法;同样、如果有指定destroy-method声明了销毁方法,该方法也会被调用

 

 

 

 

 

 
posted @ 2015-11-01 08:43  monkjavaer  阅读(185)  评论(0编辑  收藏  举报