1. Spring是什么?
开源的,用来简化企业级应用开发的应用开发框架。
(1)简化开发
Spring对常用的api(比如jdbc) 做了封装,这样,可以大大简化这些api的使用。(比如使用springjdbc访问数据库,就不用考虑如何获取连接和关闭连接了)。
(2)解耦
Spring帮我们建立对象之间的依赖关系,这样,对象之间的耦合度会大大降低,代码的维护性会大大提高。
(3)集成其它框架
Spring可以将其它的一些框架集成进来(比如用于定时任务处理的Quartz等),方便这些框架的使用。
2.Spring容器
(1)什么是Spring容器?
Spring框架中的一个核心模块,用于管理对象。
(2)启动Spring容器
step1.导包。spring-webmvcstep2. 添加spring配置文件。step3. 启动容器。
(3)创建对象
1)方式一 无参构造器step1. 为类添加无参构造器(或者缺省构造器)step2. 在配置文件里面,添加bean元素。注:bean就是容器所创建的对象。step3. 启动容器,调用容器的getBean方法。
2)方式二 静态工厂方法 (了解)
3) 方式三 实例工厂方法(了解)
(4)作用域
1)默认情况下,容器对于每个bean元素,只会创建一个实例。2) 如果将作用域设置为"prototype",则每调用一次getBean方法,就会创建一个新的实例。
(5)延迟加载 (了解)
1)默认情况下,容器启动之后,会将作用域为"singleton"的bean创建好。2)延迟加载指的是,容器启动之后,对作用域为"singleton"的bean不再创建,直到调用了getBean方法才会创建。
(6)生命周期
1)初始化方法容器创建好bean的实例之后,会立即调用初始化方法。2)销毁方法容器关闭之前,会调用销毁方法。
代码演示
/spring01/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>welkin</groupId><artifactId>spring01</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
</project>
/spring01/src/main/java/basic
MessageService.java
package basic;public class MessageService {public MessageService() {System.out.println("MessageService()");}public void init(){System.out.println("init()");}public void destroy(){System.out.println("destory()");}
}
Teacher.java
package basic;public class Teacher {public Teacher() {System.out.println("Teacher()");}}
/spring01/src/main/java/first
FirstSpring.java
package firstimport java.util.Calendar
import java.util.Dateimport org.springframework.context.ApplicationContext
import org.springframework.context.support.ClassPathXmlApplicationContextpublic class FirstSpring {public static void main(String[] args) {String config = "applicationContext.xml"ApplicationContext ac = new ClassPathXmlApplicationContext(config)Student stu = ac.getBean("stu1" , Student.class)System.out.println(stu)Date date = ac.getBean("date1",Date.class)System.out.println(date)Calendar cal = ac.getBean("cal1",Calendar.class)System.out.println(cal)Date time = ac.getBean("time1",Date.class)System.out.println(time)}}
Student.java
package first;public class Student {public Student(){System.out.println("Student()");}
}
/spring01/src/main/resources
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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><bean id="stu1" class="first.Student"/><bean id="date1" class="java.util.Date"/><bean id="cal1" class="java.util.Calendar" factory-method="getInstance"/><bean id="time1" factory-bean="cal1" factory-method="getTime"/></beans>
basic.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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><bean id="t1" class="basic.Teacher" scope="singleton" lazy-init="true"/><bean id ="ms1" class="basic.MessageService" init-method="init" destroy-method="destroy"/>
</beans>
/spring01/src/test/java/test/TestCase.java
package test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import basic.MessageService;public class TestCase {
@Testpublic void test1() {String config = "basic.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(config);}@Testpublic void test2() {String config = "basic.xml";AbstractApplicationContext ac = new ClassPathXmlApplicationContext(config);MessageService ms1 = ac.getBean("ms1", MessageService.class);ac.close();}
}