spring框架
Spring
一. Spring是什么?
Spring是一个开源的用来简化应用开发的框架。
二. Spring的特点?
1)简化开发
Spring对很多常用的api做了简化(比如,Spring对jdbc就做了很好的 封装,我们利用Springjdbc访问数据库,就不用考虑如何获取连接和关闭 连接)。
2)管理对象(解耦)
Spring帮我们管理对象之间的依赖关系,这样,便于代码的维护。
3)集成(其他框架)
Spring可以集成其它的一些框架。集成之后,这些框架的使用会变得 更加的简单。(将其他框架集成进来)
三 .Spring容器
1)Spring容器是什么?
Spring框架当中的一个核心模块,用来管理对象。
2)如何将一个bean组件(普通的java类)交给spring容器?
在配置文件里添加bean的属性:<bean id ="" class=""></bean>
3)如何启动Spring容器?(war)
step1. 导包。(spring-webmvc org.springframework..3.2.8)
step2. 添加配置文件。(将bean组件交给spring容器)
src/main/resources --applicationContext.xml、
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
6 xmlns:jee="http://www.springframework.org/schema/jee"
7 xmlns:tx="http://www.springframework.org/schema/tx"
8 xmlns:aop="http://www.springframework.org/schema/aop"
9 xmlns:mvc="http://www.springframework.org/schema/mvc"
10 xmlns:util="http://www.springframework.org/schema/util"
11 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
12 xsi:schemaLocation="
13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
22 <!--
23 利用无参构造器创建对象。
24 id属性:bean的名称,要求唯一。
25 class属性:类的全限定名。
26 -->
27 <bean id="stu1" class="first.Student">
28 </bean>
29
30 <bean id="date1" class="java.util.Date">
31 </bean>
32
33 <!-- 使用静态工厂方法创建对象。
34 factory-method属性:指定一个静态方法,
35 Spring容器会调用这个方法来创建对象。
36 -->
37 <bean id="cal1" class="java.util.Calendar"
38 factory-method="getInstance">
39 </bean>
40
41 <!-- 使用实例工厂方法创建对象
42 factory-bean属性:指定一个bean的id ,
43 Spring容器会调用该对象对应的方法
44 来创建对象。
45 -->
46 <bean id="time1" factory-bean="cal1"
47 factory-method="getTime">
48 </bean>
49
50
51
52 </beans>
step3. 编写启动Spring容器的代码。
src/main/java
1 package first;
2
3 import java.util.Calendar;
4 import java.util.Date;
5
6 import org.springframework.context.ApplicationContext;
7 import org.springframework.context.support.ClassPathXmlApplicationContext;
8
9 public class FirstSpring {
10
11 public static void main(String[] args) {
12 //step1. 启动Spring容器
13 /*
14 * ClassPathXmlApplicationContext
15 * 会依据类路径查找配置文件。
16 */
17 ApplicationContext ac =
18 new ClassPathXmlApplicationContext(
19 "applicationContext.xml");
20 //System.out.println(ac);
21 //step2. 通过容器,获得对象。
22 Student stu1 =
23 ac.getBean("stu1",Student.class);
24 System.out.println(stu1);
25
26 Date date1 =
27 ac.getBean("date1",Date.class);
28 System.out.println(date1);
29
30 Calendar cal1 =
31 ac.getBean("cal1",Calendar.class);
32 System.out.println("cal1:" + cal1);
33
34 Date time1 =
35 ac.getBean("time1",Date.class);
36 System.out.println("time1:" + time1);
37 }
44
45 }
如何创建对象?
方式一 无参构造器(*)
step1. 给类应用提供无参构造器(或者缺省构造器)。
step2. 在配置文件当中,添加一个bean元素。
step3. 启动Spring容器,调用容器提供的getBean方法获得要创建的 对象。
无参构造器(或者缺省构造器):
1 package first;
2
3 public class Student {
4
5 public Student() {
6 System.out.println("Student()");
7 }
8
9 }
添加bean元素
<!--
23 利用无参构造器创建对象。
24 id属性:bean的名称,要求唯一。
25 class属性:类的全限定名。
26 -->
27 <bean id="stu1" class="first.Student">
28 </bean>
获得对象
1 //step2. 通过容器,获得对象。
2 Student stu1 =
3 ac.getBean("stu1",Student.class);
4 System.out.println(stu1);
方式二 静态工厂方法 (了解)
方式三 实例工厂方法 (了解)

生命周期的管理
初始化方法 :
(用于获取资源,只执行一次)。
销毁方法:
(用于释放资源,只执行一次)。
注意,销毁方法只有在作用域为singleton才起作用。
作用域
默认情况下,对于一个bean元素,容器只会创建一个对应的实例。
可以设置作用域为prototype(原型,多个实例)。
延迟加载 (了解)
默认情况下,当容器启动之后,会将所有作用域为单例的bean创建好。
lazy-init属性:如果值为true,表示延迟加载,即容器启动之后,不会立即创建该实例。
1 package basic;
2
3 public class MessageBean {
4
5 public MessageBean() {
6 System.out.println("MessageBean()");
7 }
8
9 public void init(){
10 System.out.println("init()");
11 }
12
13 public void sendMsg(){
14 System.out.println("sendMsg()");
15 }
16
17 public void destroy(){
18 System.out.println("destroy()");
19 }
20
21 }
配置文件
1 <!--
2 init-method属性:指定初始化方法。
3 destroy-method属性:指定销毁方法。
4 lazy-init属性:如果值为true,表示延迟加载,
5 即容器启动之后,不会立即创建该实例。
6 -->
7 <bean id="mb1" class="basic.MessageBean"
8 init-method="init"
9 destroy-method="destroy"
10 lazy-init="true">
11 </bean>
12
13 <!--
14 scope属性:指定作用域,缺省值是
15 singleton(单例,即单个实例);
16 可以设置作用域为prototype(原型,多个实例)。
17 -->
18 <bean id="sb1" class="basic.ScopeBean"
19 scope="prototype">
20 </bean>
(4)IOC (Inversion Of Controll 控制反转)
什么是IOC?
对象之间的依赖关系由容器来建立。
什么是DI? (Dependency Injection 依赖注入)
容器通过调用set方法或者构造器来建立对象之间的依赖关系。
注: IOC是目标,而DI是手段。 
依赖注入的两种方式
把b注入a: 1:在a中声明private属性的b变量(private B b);
2:在a中添加一个setB()
//3:在a中添加无参构造器
方式一 :set方法。
step1. 添加set方法。
step2. 配置set方法注入。

1 package ioc;
2
3 public class A {
4 private B b;
5
6 public void setB(B b) {
7 System.out.println("A's setB()");
8 this.b = b;
9 }
10
11 public A() {
12 System.out.println("A()");
13 }
14
15 public void service(){
16 System.out.println("A's service()");
17 //调用B的f1()方法
18 b.f1();
19 }
20
21 }
1 package ioc;
2
3 public class B {
4
5 public B() {
6 System.out.println("B()");
7 }
8
9 public void f1(){
10 System.out.println("B's f1()");
11 }
12
13 }
方法二:构造器注入
step1. 添加构造器。
step2. 配置构造器注入。
方法三:自动装配 (了解)
什么是自动装配?
容器依据某些规则,自动建立对象之间的依赖关系。
注:默认情况下,容器禁止自动装配。
注入基本类型的值
使用value属性来注入。
1 <bean id="vb1" class="value.ValueBean">
2 <property name="name" value="小月"/>
3 <property name="age" value="22"/>
4 <property name="interest">
5 <list>
6 <value>钓鱼</value>
7 <value>旅游</value>
8 <value>看电视</value>
9 <value>看电视</value>
10 </list>
11 </property>
12 <property name="city">
13 <set>
14 <value>北京</value>
15 <value>长沙</value>
16 <value>南京</value>
17 </set>
18 </property>
19 <property name="score">
20 <map>
21 <entry key="英语" value="60"/>
22 <entry key="math" value="80"/>
23 </map>
24 </property>
25 <property name="db">
26 <props>
27 <prop key="username">King</prop>
28 <prop key="password">1234</prop>
29 </props>
30 </property>
31 </bean>
注入集合类型的值
List,Set,Map,Properties
引用的方式注入集合类型的值

读取properties文件的内容

使用Spring表达式


浙公网安备 33010602011771号