您的位置:首页 > 编程语言 > Java开发

Spring-01-HelloWorld

2015-07-18 22:07 615 查看

传统的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: