后端开发基础-Spring框架学习-001——基础概念
spring是什么?
定义
是一个轻量级的开源的应用开发框架。
特点
. 简化
spring把一些常用的api做了一层封装,比如,
使用spring jdbc访问数据库,就不再需要编写
获得连接与关闭连接的代码。
. 解耦
spring容器帮我们管理对象与对象之间的关系,
这样一来,对象之间是松耦合的,方便以后代码
的维护。
. 集成
可以集成其它的第三方的框架,比如定时任务
处理(Quartz)。
spring容器
spring容器是什么?
spring框架中的一个重要模块,用来管理对象。
如何启动spring容器?
step1. 将spring相关的jar文件导入。
step2. 准备spring配置文件。
step3. 启动容器。
ApplicationContext ac =
new ClassPathXmlApplicationContext(“applicationContext.xml”);
如何创建一个对象?
. 方式一 无参构造器(重点)
. 方式二 静态工厂方法(了解)
. 方式三 实例工厂方法(了解)
注:
spring容器将所有的被其管理的java类都称之为
一个javabean。
一个java类如何满足如下几个条件,可以称之为
javabean。
. public类
. 实现Serializable接口
. 有无参构造器
. 如果有属性,有对应的get/set方法
生命周期的管理
. 初始化
. 销毁
作用域
. 默认情况下,对于一个bean,spring容器只会
创建一个实例。
. spring容器在启动之后,会扫描整个配置文件,
然后将单例的bean先创建好。
延迟加载
. spring容器在启动之后,会扫描整个配置文件,
然后将单例的bean先创建好。
. 如果希望只有当getBean时才创建,可以延迟
加载。
IOC(Inversion Of Controll 控制反转)
IOC是什么?
对象之间的依赖关系交给容器来管理。
DI是什么(Dependency injection 依赖注入)?
容器通过调用对象的构造器或者set方法
来建立对象之间的依赖关系。
注:
IOC是目标,DI是手段。
DI的几种方式
set方法(重点)
. 容器调用对象的set方法来建立对象之间的
依赖关系。
. 有无参构造器
. 有set方法
构造器
. 容器调用对象的构造器来建立对象之间的
依赖关系。
. 有带参构造器
generated by haroopad
案例演示:
工程案例目录结构:
需要使用到的Jar:
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>com.study</groupId>
<artifactId>springcase-day01</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>
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="eb1"
class="container.instantiation.ExampleBean"/>
<bean id="date1" class="java.util.Date"/>
<!--
使用静态工厂方法创建一个对象
factory-method属性:指定要调用的静态方法。
-->
<bean id="cal1" class="java.util.Calendar"
factory-method="getInstance"/>
<!--
使用实例工厂方法创建一个对象
factory-bean属性:要调用的对象的id。
factory-method属性:要调用的方法。
-->
<bean id="cal2"
class="java.util.GregorianCalendar"/>
<bean id="time1" factory-bean="cal2"
factory-method="getTime"/>
</beans>
StartSping.java
package container.first;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StartSping {
public static void main(String[] args) {
//启动spring容器(配置文件名不区分大小写)
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
System.out.println(ac);
}
}
-
演示启动spring容器
运行程序后,后台执行结果:
演示 创建对象的方式:
ExampleBean.java
package container.instantiation;
public class ExampleBean {
public ExampleBean() {
System.out.println("ExamleBean的无参构造器...");
}
}
TestCase.java
package test;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import container.instantiation.ExampleBean;
public class TestCase {
@Test
//测试容器创建对象的第一种方式(调用无参构造器)
public void test1(){
//启动容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
System.out.println(ac);
//向容器申请获得一个对象
ExampleBean eb1 = ac.getBean("eb1", ExampleBean.class);
System.out.println(eb1);
Date date1 = ac.getBean("date1", Date.class);
System.out.println(date1);
}
@Test
//测试容器创建对象的第二种方式(静态工厂方法)
public void test2(){
//启动容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ac);
Calendar cal = ac.getBean("cal1", Calendar.class);
System.out.println(cal);
}
@Test
//测试容器创建对象的第三种方式(实例工厂方法)
public void test3(){
//启动容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ac);
Date date = ac.getBean("time1", Date.class);
System.out.println(date);
}
}
依次运行test1,test2,test3,后台运行结果:
A.
B.
C.
生命周期的管理、作用域、延迟加载演示
MessageBean.java
package container.life;
public class MessageBean {
public MessageBean(){
System.out.println("MessageBean的无参构造器...");
}
public void init(){
System.out.println("init方法...");
}
public void sendMsg(){
System.out.println("发送消息...");
}
public void destroy(){
System.out.println("destroy方法...");
}
}
SomeBean.java
package container.life;
public class SomeBean {
public SomeBean(){
System.out.println("SomeBean的无参构造器...");
}
}
app2.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">
<!--
init-method属性:指定初始化方法。
容器在创建好相应的实例之后,会立即调用
该实例的初始化方法。
destroy-method属性:指定销毁方法。
容器在删除实例之前,会调用该实例的销毁方法。
注意:
只有当作用域为singleton时,destroy方法
才会执行。
-->
<bean id="mb1"
class="container.life.MessageBean"
init-method="init"
destroy-method="destroy"
scope="prototype">
</bean>
<!--
scope属性:指定作用域,缺省值是"singleton"(单例),
如果值为"prototype",则每次getBean一次,
会创建一个新的实例。
-->
<bean id="sb1" class="container.life.SomeBean"
scope="prototype">
</bean>
<!--
lazy-init属性:指定实例创建时机,
如果值为true,表示延迟加载,
即容器启动之后,不会创建实例,只有当getBean
时才会创建。
-->
<bean id="sb2" class="container.life.SomeBean"
lazy-init="true">
</bean>
</beans>
TestCae2.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 container.life.MessageBean;
import container.life.SomeBean;
public class TestCae2 {
@Test
//测试生命周期相关的几个
public void test1(){
//关闭容器的方法在ApplicationContext
//接口当中没有提供,需要使用其子接口
//AbstractApplicationContext
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("app2.xml");
MessageBean mb = ac.getBean("mb1",MessageBean.class);
mb.sendMsg();
System.out.println(mb);
ac.close();
}
@Test
//测试作用域
public void test2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("app2.xml");
SomeBean sb1 = ac.getBean("sb1", SomeBean.class);
SomeBean sb2 = ac.getBean("sb1", SomeBean.class);
System.out.println(sb1 == sb2);
}
@Test
//测试作用域 (lazy-init)
public void test3(){
ApplicationContext ac = new ClassPathXmlApplicationContext("app2.xml");
SomeBean sb2= ac.getBean("sb2", SomeBean.class);
}
}
依次运行test1、test2、test3,运行结果如下
A.
B.
C:
没有getBean获取对象时不会创建对象:
IOC(Inversion Of Controll 控制反转) 演示
app3.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">
<!--
name属性:要注入的属性名称。
ref属性:要注入的对象的id。
-->
<bean id="a1" class="container.ioc.set.A">
<property name="b" ref="b1"></property>
</bean>
<bean id="b1" class="container.ioc.set.B" />
<bean id="wt1" class="container.ioc.constructor.Waiter"></bean>
<!--
index属性:从0开始,指定参数的位置
(即要给第几个参数赋值)。
-->
<bean id="rest1" class="container.ioc.constructor.Restaurant">
<constructor-arg index="0" ref="wt1" />
</bean>
</beans>
Restaurant.java
package container.ioc.constructor;
public class Restaurant {
private Waiter wt;
public Restaurant(){
System.out.println("Restaurant的无参构造器...");
}
public Restaurant(Waiter wt){
System.out.println("Restaurant的带参构造器...");
this.wt = wt;
}
@Override
public String toString() {
return "Restaurant [wt=" + wt + "]";
}
}
Waiter.java
package container.ioc.constructor;
public class Waiter {
public Waiter() {
System.out.println("Waiter的无参构造器...");
}
}
A.java
package container.ioc.set;
public class A {
private B b;
public B getB() {
return b;
}
public void setB(B b) {
System.out.println("A的setB方法...");
this.b = b;
}
public A(){
System.out.println("A的无参构造器...");
}
public void execute(){
b.f1();
}
}
B.java
package container.ioc.set;
public class B {
public B(){
System.out.println("B的无参构造器...");
}
public void f1(){
System.out.println("B的f1方法...");
}
}
TestCase3.java
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import container.ioc.constructor.Restaurant;
import container.ioc.set.A;
public class TestCase3 {
@Test
//测试set方式注入
public void test1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("app3.xml");
A a = ac.getBean("a1", A.class);
a.execute();
}
@Test
//测试构造器注入
public void test2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("app3.xml");
Restaurant rest = ac.getBean("rest1", Restaurant.class);
System.out.println(rest);
}
}
依次运行test1、test2,执行后结果如下: