如何使用spring 

1.spring容器

    a.spring容器是什么?

        用来管理对象的一个程序。


    b.如何启动spring容器?


      step1,将spring相关的jar文件拷贝到WEB-INF\lib下。

        

java spring SimpleKey用法 spring怎么使用_xml

      step2,在src下,添加一个配置文件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"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	
	
</beans>

      step3,启动容器。


2.容器如何管理对象?


       1)如何实例化?


         方式一:

              容器调用不带参的构造器。

              <bean id="标识符" class="Bean类"/>


            方式二: (了解)

                 容器调用类的静态方法(静态工厂)。

                 使用factory-method属性指定静态方法。


                 <bean id="标识符" class="Bean类"  factory-method="..."/>           

            方式三: (了解)


                 实例工厂。

                 <bean id="标识符" factory-bean="id"


                    factory-method="..."/>

代码示例:

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"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!-- id要求唯一,class为完整包名.类名 -->
	<bean id="eb" class="container.ExampleBean"/>
	
	<bean id="cal" class="java.util.GregorianCalendar"/>
	
	<bean id="student" class="container.Student"/>
	
	<!-- 静态工厂方式 -->
	<!-- factory-method必须为静态方法 -->
	<bean id="cal2" class="java.util.Calendar" 
		factory-method="getInstance"/>

	<!-- 实例化工厂方式 -->	
	<bean id="date1" factory-bean="cal" 
		factory-method="getTime"/>
		
</beans>

ExampleBean.java


package container;

import java.io.Serializable;

/**
 * javabean
 *   一个java类满足如下条件:
 *   a,实现序列化接口
 *   b,有无参构造器
 *   c,有属性及对应的get/set方法
 *   
 */
public class ExampleBean implements Serializable{
	public ExampleBean(){
		System.out.println("ExampleBean()构造器...");
	}
}

Test.java


package container;

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

public class Test {
	public static void main(String[] args) {
		String conf = "container/applicationContext.xml";
		ApplicationContext ac = 
			new ClassPathXmlApplicationContext(conf);
		Student stu = ac.getBean("student",Student.class);
		System.out.println(stu);
	}

}





      2)作用域

           容器会依据scope属性来决定创建多少个实例。默认情况下,容器对于一个bean的配置,只会创建一个实例。


           scope属性值可以为以下两个


                   prototype:(原型),每getBean一次,就会创建一个新的实例。


                     singleton:(单例,缺省值),只创建一个实例。


      3)生命周期相关的几个方法

           类似servlet容器可以对servlet进行生命周期的管理,spring容器同样也可以对bean进行生命周期的管理。

              init-method:指定初始化方法,容器在创建实例之后,会调用该实例的初始化方法。

                detroy-method:指定销毁方法,容器在关闭之前,会调用该实例的销毁方法。


                注意,只有当scope="singleton"时,销毁方法才起作用。   


      4)延迟加载(了解)

            在默认情况下,容器启动之后,会将所有配置scope="singleton"的bean创建好。


                 如果需要延迟加载(即不事先创建好),需要将lazy-init配置为true。

               在顶级的<beans/>元素中的default-lazy-init属性,可以为容器所有的<bean>指定延迟加载。

代码示例:

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"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!-- 
		作用域:scope 
		prototype:(原型),每getBean都创建一个新的实例。
		singleton:(单例,缺省值),只创建一个实例。
	-->
	<!--  
	<bean id="mb" class="container.other.MessageBean"
		scope="prototype"/>
	-->
	
	<!-- 
		生命周期相关的方法
		init-method:指定初始化方法,容器在创建实例之后,会调用该实例的初始化方法
		destroy-method:指定销毁方法,容器在关闭之前,会调用该实例的销毁方法。
		注:销毁方法只有scope="singleton"时,销毁方法才会被调用。
	-->
	<!--  
	<bean id="mb" class="container.other.MessageBean"
		init-method="init" destroy-method="destroy"/>
	-->
	
	<!-- 
		延迟加载
		lazy-init="true"表示延迟加载,在getBean时才创建实例。
		默认情况容器会将所有scope属性
		为singleton的bean创建好。
	 -->
	<bean id="mb" class="container.other.MessageBean"
		lazy-init="true"/>
	
	
</beans>



MessageBean.java


package container.other;

import java.io.Serializable;

public class MessageBean implements Serializable{
	public MessageBean(){
		System.out.println("MessageBean构造器...");
	}
	
	public void init(){
		System.out.println("MessageBean的init方法被调用...");
	}
	
	public void sendMessage(){
		System.out.println("MessageBean的sendMessage方法被调用..");
	}
	
	public void destroy(){
		System.out.println("MessageBean的destroy方法被调用..");
	}
}



TestCase.java


package container.other;

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

public class TestCase {
	
	//@Test
	//演示作用域
	public void test1(){
		//启动spring容器
		ApplicationContext ac =
			new ClassPathXmlApplicationContext(
					"container/other/applicationContext.xml");
		//获得bean实例
		MessageBean mb = ac.getBean("mb",MessageBean.class);
		
		MessageBean mb2 = ac.getBean("mb",MessageBean.class);
		
		System.out.println(mb==mb2);
		
	}

	//演示生命周期相关的几个方法
	//@Test
	public void test2(){
		//启动spring容器
		//ApplicationContext接口中没有定义close方法,需要
		//使用其子类型
		AbstractApplicationContext ac =
			new ClassPathXmlApplicationContext(
					"container/other/applicationContext.xml");
		//获得bean实例
		MessageBean mb = ac.getBean("mb",MessageBean.class);
		mb.sendMessage();
		
		//关闭容器
		ac.close();
	}
	
	//演示延迟加载
	@Test
	public void test3(){
		ApplicationContext ac =
			new ClassPathXmlApplicationContext(
					"container/other/applicationContext.xml");
		MessageBean mb = ac.getBean("mb",MessageBean.class);
	}
	
}