Spring之旅第一章

1.简化Java开发

       Spring的核心功能是简化Java的开发。为了降低Java开发的复杂性,Spring采取了以下四种关键性的策略:

  • 基于POJO的轻量级和最小侵入性编程;
  • 通过依赖注入和面向接口实现松耦合;
  • 基于切面和惯例进行声明式编程;
  • 通过切面和模板减少样板式代码。

2.激发POJO的潜能

      Spring竭力避免因自身的API而弄乱你的应用代码,即彻底抛弃了继承和接口。Spring的非侵入编程模型意味着这个类在Spring应用和非Spring应用中都可以发挥同样的作用。

3.依赖注入(DI)

       如果一个对象只通过接口(而不是具体的实现类)来表明依赖关系,那么这种依赖就能够在对象本身毫不知情的情况下,用不同的具体实现进行替换。依赖注入主要有三种方式:构造器注入、setter方法注入、接口注入。接口注入的具体实现如下所示:

interface AnimalInterface{
    void initDog(Dog dog); //这里必须是被注入对象依赖的对象
}
class Animal implements AnimalInterface{
    private Dog dog;
    public Person(){}
    @Override
    public void initDog(Dog dog){
        this.dog = dog;//实现注入方法接口外部通过此方法注入dog对象
    }
}

      创建应用组件之间协作的行为通常称为装配。Spring有二种Bean的装配方式:基于XML方式、基于Java配置方式(注解方式)。Spring通过应用上下文(Application Context)装载Bean的定义并把它们组装起来。

4.应用切面AOP

      DI能够让相互协作的软件组件保持松耦合,而面向切面编程(AOP)允许你把遍布应用各处的功能分离出来形成可重用的组件,并以声明的方式将模块化的服务应用到需要影响的业务组件中去。面向切面编程往往被定义为促使软件系统实现关注点(日志、事物管理、安全、权限认真)分离的一项技术,如果将这些实现分散到多个组件中去,将造成代码的双重复杂性:实现系统关注点功能的代码会重复出现在多个组件中;组件会因为那些与自身核心业务无关的代码而变得混乱。引入Spring AOP实现的依赖包:aopalliance.jar和aspectjweaver.jar来实现一个简单的AOP例子:核心业务接口HelloWorld,

public interface HelloWorld
{
    void printHello();
    void printSpring();
}

核心业务接口实现类HelloWorldImpl:

public class HelloWorldImpl implements HelloWorld
{
    public void printHello()
    {
        System.out.println("Hello, I am Tom.");
    }    
    public void printSpring()
    {
        System.out.println("Hello, I am Spring.");
    }
}

模块化服务打印时间:

public class TimeHandler
{
    public void printTime()
    {
        System.out.println("CurrentTime = " + System.currentTimeMillis());
    }
}
以上三个类就可以实现SpringAOP,其的配置文件如下aop.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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">       
        <bean id="helloWorldImpl" class="com.luna.aop.HelloWorldImpl" />
        <bean id="timeHandler" class="com.luna.aop.TimeHandler" />
        
        <aop:config>
            <aop:aspect id="time" ref="timeHandler">
                <aop:pointcut id="addAllMethod" expression="execution(* com.luna.aop.HelloWorld.*(..))" />
                <aop:before method="printTime" pointcut-ref="addAllMethod" />
                <aop:after method="printTime" pointcut-ref="addAllMethod" />
            </aop:aspect>
        </aop:config>
</beans>

针对以上AOP测试Main函数的实现如下所示:

public static void main(String[] args)
{
    ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml");      
    HelloWorld hw = (HelloWorld)ctx.getBean("helloWorldImpl");
    hw.printHelloWorld();
    System.out.println("++++++++++++++++");
    hw.doPrint();
}
      总结:AOP实现的核心原理是动态代理,即针对目标对象生成加强或者减弱的代理对象,以此来实现AOP相关的业务功能。AOP默认会使用JDK动态代理,如果目标对象没有实现接口则使用CGLIB动态代理,更多关于动态代理的内容常见传送门: 代理模式极简实现

原文地址: https://blog.csdn.net/u011635492/article/details/80658779

猜你喜欢

转载自blog.csdn.net/u011635492/article/details/80658779