Spring学习手册(1)—— HelloSpring

环境相关:
JDK : 1.8.0_121
IDE : IntelliJ IDEA 2016.3.4
Spring-Framework: 4.3.6.RELESE

Spring框架为基于JAVA的企业级应用提供了一套通用、可配置的编程模型,并使应用可较容易的部署在不同的平台。Spring致力于企业应用“基础设施”建设,以便于应用开发团队专注于应用业务逻辑。Spring框架为我们提供了以下特性:

Spring特性:

  • 依赖注入(Dependency Injection)
  • 面向切面编程(Aspect-Oriented Programming)
  • Spring MVC应用和Spring RESTful 服务器框架
  • 对JDBC、JPA和JMS的支持
  • 更多......

一、 创建Java项目

  1. 使用IDEA创建Java应用:File->new->project,选择Gradle项目,Project SDK选择JDK1.8,Additional Libraries and Frameworks选择Java,点击Next。


    Spring学习手册(1)—— HelloSpring_第1张图片
    IDEA创建项目
  2. 填写GroupId、ArtifactId选择Next。
  3. 如下图所示,☑️Use auto-import
    ☑️Create directories for empty content roots automatically
    选择Next。


    Spring学习手册(1)—— HelloSpring_第2张图片
  4. 设置Project name、设置Project location,完成项目设置。

二、设置Gradle配置信息

在工程Project选项框中打开build.gradle,在dependencies中添加

compile 'org.springframework:spring-context:4.3.6.RELEASE'
Spring学习手册(1)—— HelloSpring_第3张图片
修改Gradle配置

IDEA会自动将项目依赖的Spring jar包下载到本地(此处需要互联网)。

三、HelloSpring简单版

创建接口文件MessageService.java

public interface MessageService {

    String  getMessage();
}

创建MessageService接口的实现类MessageServiceImpl

public class MessageServiceImpl  implements  MessageService{

    public String getMessage() {
        return "MessageServiceImpl:Hello Spring";
    }
}

创建MessagePrinter类

public class MessagePrinter {

   private MessageService  messageService;

   public MessagePrinter(){
   }

   public void printMessage(){
       System.out.println(this.messageService.getMessage());
   }

   public void setMessageService(MessageService messageService){
       this.messageService = messageService;
   }
}

在resources中创建bean.xml文件,配置bean关系




    

    
        
    

创建应用文件Application,使用Spring的BeanFactory、Resource、Reader组装各组件。

public class Application {

    public static void main(String[] args){


        //由于XmlBeanFactory已经打标废弃
        //这里使用DefaultListableBeanFactory 和 Resource

        Resource resource = new ClassPathResource("bean.xml");
        DefaultListableBeanFactory  beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions(resource);

        MessagePrinter messagePrinter = beanFactory.getBean("messagePrinter",MessagePrinter.class);

        messagePrinter.printMessage();
    }
}

HelloSpring源代码地址

四、HelloSpring--ApplicationContext版本

ApplicationContextBeanFactory的超集,一般情况下使用ApplicationContext替代BeanFactoryClassPathXmlApplicationContextFileSystemXmlApplicationContext对接口ApplicationContext完成了实现,这里我们先简单用ClassPathXmlApplicationContext实现我们的HelloSpring应用。
在原有工程的基础上,我们只需要更改Application.java类。

public class Application {

    public static void main(String[] args){

        ApplicationContext applicationContext = 
                        new ClassPathXmlApplicationContext("bean.xml");
        MessagePrinter messagePrinter = 
                 applicationContext.getBean("messagePrinter",MessagePrinter.class);
        messagePrinter.printMessage();
    }
}

HelloSpring--ApplicationContext源代码地址

五、运行程序

前面我们已经完成代码的开发和配置工作,接下来我们就可以运行应用程序验证结果。右键选中Application.java文件选择"Run Application.main()"或快捷键control+R,可以在输出框看到运行结果:

MessageServiceImpl:Hello Spring

六、总结

这样我们就完成了一个基于Spring框架的应用程序。回顾代码你会发现我们通过bean配置文件(bean.xml)来将MessageServiceImpl)和MessagePrinter关联,MessagePrinter并不与具体的MessageService实现耦合,而是在运行时由Spring通过解析bean.xml文件将具体的MessageServiceImpl注入到MessagePrinter。通过这样我们就完成了将业务代码的解耦合,如果我们未来新开发了MessageService实现,我们只需要修改Bean.xml文件就完成了项目改造。
总结起来我们的代码具有如下优点:

  • 业务代码解耦合,使代码扩展性增强;
  • 代码面向接口开发,使代码具有可测试性;
  • ...

现在我们已初窥Spring框架,前面总结中我们也提到了“注入”、“依赖”等词汇,我们会在后面的文章中逐步揭开Spring的面纱。

你可能感兴趣的:(Spring学习手册(1)—— HelloSpring)