Spring-01-HelloWorld

传统的HelloWorld

  • 编写java类
    package com.weixuan.spring;

    public class HelloWorld {

        public void hello() {
            System.out.println("Hello World .");
        }

        public static void main(String[] args) {
        }
    }
  • 编译成.class文件
  • 使用classloader加载class到jvm中
  • new一个实例
  • 所有的过程全程参与

使用Spring的HelloWorld

  • 写一个java类
  • 编写配置文件,将类放入spring容器
  • 启动spring 容器 (在类路径下寻找配置文件来实例化容器)
  • 从容器中取出java类
  • 对象.方法

    控制翻转的概念:把对象的创建,初始化,销毁等动作交给spring 容器来做,由spring容器来控制对象的生命周期。注意:销毁是在scope是单例的情况下

HelloWorld实例

HelloWorld类

package com.weixuan.spring;

public class HelloWorld {

    /**
     * 属性是共享的,可能会引发线程安全问题
     */
    public void hello() {
        System.out.println("Hello World .");
    }
}

配置文件

<?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-2.5.xsd">

    <!-- 把一个类放在spring容器中,该类就成为bean id 的首字母小写-->
    <bean id="helloWorld" class="com.weixuan.spring.HelloWorld">
        <!-- collaborators and configuration for this bean go here -->
    </bean>
    <!-- more bean definitions go here -->

</beans>

客户端测试文件

- 启动ioc容器

- 取出类 helloworld

- 使用对象(对象.方法)

package com.weixuan.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.weixuan.spring.HelloWorld;

public class TestOfHelloWorld extends HelloWorld {

    // 传统的写法
    @Test
    public void test() {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.hello();
    }

    // 使用IOC容器的写法

    @Test
    public void testIOC() {
        /**
         * 1、启动ioc容器 2、取出helloworld 3、对象.方法
         */
        // 1、启动ioc容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "com/weixuan/spring/applicationContext.xml");

        // 2、取出helloworld
        HelloWorld helloWorld = (HelloWorld) applicationContext
                .getBean("helloWorld");

        // 3、对象.方法
        helloWorld.hello();
    }
}

scope属性

@Test
    public void testSpring() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "com/weixuan/spring/applicationContext.xml");

        HelloWorld helloWorld1 = (HelloWorld) applicationContext
                .getBean("helloWorld");
        HelloWorld helloWorld2 = (HelloWorld) applicationContext
                .getBean("helloWorld");

        System.out.println(helloWorld1);
        System.out.println(helloWorld2);
    }

可以看出输出是一样的,说明是默认是单例模式

 com.weixuan.spring.HelloWorld@42e366c1
 com.weixuan.spring.HelloWorld@42e366c1

单例模式就要考虑线程安全问题,可以设置scope属性是原型模式

<bean id="helloWorld" class="com.weixuan.spring2.HelloWorld" scope="prototype"> </bean>

其他属性配置

  • init-method : 在构造函数之后,有spring容器立即执行,此方法的目的是在构造函数之后,调用方法之前,做一些准备工作。
  • destroy-method:当scope设置为单例的时候,在spring容器关闭或者销毁的时候销毁这个方法。否则,关闭资源必须是开发者手动关闭。
  • 测试文件
package com.weixuan.initdestory;

public class HelloWorld {

    public HelloWorld() {
        System.out.println("Default constructor ...");
    }

    public void init() {
        System.out.println("init...");
    }

    public void destory() {
        System.out.println("destory...");
    }

    public void hello() {
        System.out.println("Hello World .");
    }
}
<?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-2.5.xsd">

    <!-- 把一个类放在spring容器中,该类就成为bean id 的首字母小写 -->

    <!--
        init-method : 在构造函数之后,有spring容器立即执行
        此方法的目的是在构造函数之后,调用方法之前,做一些准备工作

        destroy-method:当scope设置为单例的时候,在spring容器关闭或者销毁的时候销毁这个方法
        否则,关闭资源必须是开发者手动关闭
     -->
    <bean id="helloWorld" class="com.weixuan.initdestory.HelloWorld"
        init-method="init" destroy-method="destory" >
        <!-- collaborators and configuration for this bean go here -->
    </bean>
    <!-- more bean definitions go here -->

</beans>

测试文件及结果

package com.weixuan.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.weixuan.initdestory.HelloWorld;

public class TestOfInitDestory extends HelloWorld {

    private ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "com/weixuan/initdestory/applicationContext.xml");

    @Test
    public void test() {
        HelloWorld helloWorld = (HelloWorld) applicationContext
                .getBean("helloWorld");

        helloWorld.hello();
        /**
         * 输出,并没有destory方法
         * Default constructor ...
         * init...
         * Hello World .
         */
    }

    @Test
    public void test2() {

        ClassPathXmlApplicationContext classPathXmlApplicationContext = (ClassPathXmlApplicationContext)applicationContext;

        HelloWorld helloWorld = (HelloWorld) classPathXmlApplicationContext
                .getBean("helloWorld");

        helloWorld.hello();
        classPathXmlApplicationContext.close();

        /**
         * scope默认为单例模式
         * 此时输出:
         * Default constructor ...
         * init...
         * Hello World .
         * ....
         * destory...
         */

        /**
         * scope为prototype模式
         * 输出
         * Default constructor ...
         * init...
         * Hello World .
         */
    }
}

这就是最简单的HelloWorld

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-08 04:43:49

Spring-01-HelloWorld的相关文章

Spring学习--HelloWorld

Spring: Spring 是一个开源框架. Spring 是为简化企业级应用开发而生,使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. Spring 是一个 IOC 和 AOP 容器框架. Spring 框架图: 下面我们开始 Spring 第一个经典项目:HelloWorld 普通模式的Helloworld: 1 package com.itdjx.spring.beans; 2 3 /** 4 * @author Wáng Chéng Dá 5

Webpack + React 开发 01 HelloWorld

1.项目依赖 安装所需要依赖的其它第三方开源库,项目依赖如下: "dependencies": { "babel-core": "^6.21.0", "babel-loader": "^6.2.10", "babel-preset-es2015": "^6.18.0", "babel-preset-react": "^6.16.0&quo

Spring MVC HelloWorld

我使用Maven + Idea 开始了Spring MVC的学习之路,Hello World教程参照:http://www.programcreek.com/2014/02/spring-mvc-helloworld-using-maven-in-eclipse/ 1 首先使用Maven脚手架生成一个基于maven 的web 框架 在命令行使用命令生成项目结构框架: mvn archetype:generate -DgroupId=com.mmj.app -DartifactId=hellowo

IDEA+Maven+Spring MVC HelloWorld示例

用Maven创建Web项目 选择webapp模板 创建成功后点Enable Auto-Import idea给我们创建出来的结构是这样的,这还不标准,需要自己修改. 在main文件夹下创建java文件夹,这是放置源码的地方,标记为sources.创建resources文件夹且标记为resource. 初始结构如下. 配置Spring pom.xml的配置 在<dependecies>与</dependecies>之间插入以下代码,添加依赖包. <dependency>

小试牛刀 spring的HelloWorld

先导入包: commons-logging-1.1.1.jar  : spring依赖的包: spring-beans-4.0.0.RELEASE.jar: spring-context-4.0.0.RELEASE.jar: spring-core-4.0.0.RELEASE.jar: spring-expression-4.0.0.RELEASE.jar: 1 package com.model; 2 3 public class HelloWorld { 4 5 private String

创建一个Spring的HelloWorld程序

Spring IOC IOC指的是控制反转,把对象的创建.初始化.销毁等工作都交给Spring容器.由spring容器来控制对象的生命周期.下图能够说明我们传统创建类的方式和使用Spring之后的差别: 创建Java类: package com.yihai.springioc; //IOC指的是控制反转.把对象的创建.初始化.销毁等工作都 //交给Spring容器.由spring容器来控制对象的生命周期. public class HelloWorld { public void hello()

ExtJS实战 01——HelloWorld

前言 Extjs5的发布已经有些日子了,目前的最新稳定版本是Extjs5.1.0,我们可以在官方网站进行下载.不过笔者今天访问得到的是502Bad Gateway,原因可能是sencha的nigix没有配置好缓冲. 如果E文比较好,建议跳过本系列教程,直接阅读官方5.1文档. 正文 Extjs是开源的遵循GPL协议,我们下载到的文件包含了源代码,我们要在这堆源代码中找到Extjs5的真正需要引用的文件. 这些文件在build文件夹下.我们首先需要引用的是ext.all.js,(ext.all-d

Spring笔记①--helloworld

Spring Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架,它主要是为了解决企业应用开发的复杂性而诞生的: 目的:解决企业应用开发的复杂性 功能:使用基本的Javabean代替EJB 范围:任何Java应用 ? 第一个helloWorld 新建一个Java 项目 添加spring能力 勾选spring3.0 选择aop. ????core. ????Pre core .????????jdbc.????????j2ee ? 写一个HelloWorld package

Spring Data HelloWorld(三)

在 Spring Data 环境搭建(二) 的基础之上 我们改动  http://www.cnblogs.com/fzng/p/7253068.html 定义个一个接口  继承Repository类  咱们先实现一个根据名字查询 package org.springdata.repository; import org.springdata.domain.Employee; import org.springframework.data.repository.Repository; import

spring security helloworld级别

applicationContext-security.xml文件 <?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans=