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
相关文章推荐
- java static关键字
- Java初涉之4--浅谈静态块和非静态块
- java中初始化对象变量的方法
- 使用缓冲流时记得恰当的使用flush()方法
- JAVA_SE基础——15.循环嵌套
- java中String s="abc"及String s=new String("abc")详解
- 【J2EE之web应用】java集群概念
- 使用myeclipse将Javaj项目标ar套餐邂逅classnotfound解决问题的方法
- Java基础
- 浅谈java main()
- Myeclipse验证js语法报错
- 10行Java代码实现最近被使用(LRU)缓存
- Java HashMap 的实现原理详解
- java中RowSet用法
- libsvm中java.lang.NoSuchFieldException: rand
- spring基础知识汇总
- 利用spring获取ip地址
- java中有成员访问权限private,public等原因
- Spring 源码
- Eclipse,MyEclipse代码自动提示功能设置