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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值