美文网首页JavaWeb
Spring-001-环境搭建与第一个HelloWorld

Spring-001-环境搭建与第一个HelloWorld

作者: 53b3f4658edc | 来源:发表于2017-12-06 17:34 被阅读13次

    Spring 是什么

    • Spring 是一个开源框架.
    • Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.
    • Spring 是一个 IOC(DI) 和 AOP 容器框架.
    • 具体描述 Spring:
      • 轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
      • 依赖注入(DI - - - dependency injection、IOC)
      • 面向切面编程(AOP - - - aspect oriented programming)
      • 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
      • 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
      • 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)

    Spring 模块

    微信公众号:JavaWeb架构师

    安装 SPRING TOOL SUITE

    • SPRING TOOL SUITE 是一个 Eclipse 插件,利用该插件可以更方便的在 Eclipse 平台上开发基于 Spring 的应用。
    • 安装方法说明(springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip):
      • Help --> Install New Software...
      • Click Add...
      • In dialog Add Site dialog, click Archive...
      • Navigate to springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip and click Open
      • Clicking OK in the Add Site dialog will bring you back to the dialog 'Install'
      • Select the xxx/Spring IDE that has appeared
      • Click Next and then Finish
      • Approve the license
      • Restart eclipse when that is asked

    搭建 Spring 开发环境

    • 把以下 jar 包加入到工程的 classpath 下:
    微信公众号:JavaWeb架构师
    • Spring 的配置文件: 一个典型的 Spring 项目需要创建一个或多个 Bean 配置文件, 这些配置文件用于在 Spring IOC 容器里配置 Bean. Bean 的配置文件可以放在 classpath 下, 也可以放在其它目录下.

    测试代码

    微信公众号:JavaWeb架构师

    HelloWorld.java

    package spring.hello;
    
    public class HelloWorld {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        
        public void hello() {
            System.out.println("hello: " + name);
        }
    }
    

    TestHelloWorld.java

    package spring.test;
    
    import static org.junit.Assert.*;
    
    public class TestHelloWorld {
    
        /*
         * 常规步骤:
         *  1.创建HelloWorld对象
         *  2.调用set方法,进行赋值
         *  3.调用hello方法打印
         */
        @Test
        public void testHelloWorld() {
            // 1.创建HelloWorld对象
            HelloWorld helloWorld = new HelloWorld();
            
            // 2.调用set方法,进行赋值
            helloWorld.setName("冯强");
            
            // 3.调用hello方法打印
            helloWorld.hello();
        }
        
        /*
         * Spring步骤:
         *      0. 导入相关包
         *      1. 创建相关类、方法、属性
         *      2. 创建Spring bean config文件、并配置
         *      3. 创建Spring IOC 容器对象
         *      4. 从 IOC 容器中获取实例
         *      5.进行相关操作
         * 
         * 说明:
         *      属性的赋值都是在配置文件中完成的。
         */
        @Test
        public void testHelloSpring() {
            // 1.创建 IOC 容器对象,参数是配置文件的路径
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            
            // 2.从 IOC 容器中获取实例,参数是配置文件中实例的名字
            HelloWorld helloWorld = ctx.getBean("helloWorld",HelloWorld.class);
            
            // 3.进行相关操作
            helloWorld.hello();
        }
    }
    

    applicationContext.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 -->
        <!--  
            解释:
                1. Spring根据class 帮我们创建一个id为helloWorld的对象(id值将是获取该实例时候的标志)
                2. 对其中的属性name赋值为 周杰伦 (根据set方法)
        -->
        <!-- 1. Spring根据class 帮我们创建一个名为helloWorld的对象 -->
        <bean id="helloWorld" class="spring.hello.HelloWorld">
            <!-- 2. 对其中的属性name赋值为 周杰伦 (根据set方法) -->
            <property name="name" value="周杰伦"></property>
        </bean>
        
    </beans>
    

    运行结果:


    微信公众号:JavaWeb架构师


    其它


    • 源码下载
    关注下方公众号,回复:java_course.code
    
    完整教程PDF版本下载

    相关文章

      网友评论

        本文标题:Spring-001-环境搭建与第一个HelloWorld

        本文链接:https://www.haomeiwen.com/subject/qzrlixtx.html