介绍
概述
Spring 是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring 是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 创建。简单来说,Spring 是一个分层的 JavaSE/EE full-stack (一站式) 轻量级开源框架。
下载
入门
IoC
控制反转(Inversion of Control,缩写为 IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称 DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。
- 理解 IoC 和 DI :
-
IoC :控制反转,将对象的创建权交给了 Spring。
DI :依赖注入,前提必须有 IoC 的环境,Spring 管理一个类时将类依赖的属性注入(设置)进来,就是 DI 的过程。
引入jar包
添加配置文件
在 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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置文件约束在 spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html 下可以找到--> </beans>
简单使用
package com.zze.dao; public interface UserDao { void save(); }
com.zze.dao.UserDao
package com.zze.dao.impl; import com.zze.dao.UserDao; public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("from UserDaoImpl.save()"); } }
com.zze.dao.impl.UserDaoImpl
<?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.xsd"> <!--配置文件约束在 spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html 下可以找到--> <bean id="userDao" class="com.zze.dao.impl.UserDaoImpl"></bean> </beans>
applicationContext.xml
/** * 原有方式创建 Bean */ @Test public void test1(){ UserDao userDao = new UserDaoImpl(); userDao.save(); } /** * 从 Spring 中获取 Bean */ @Test public void test2(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) applicationContext.getBean("userDao"); userDao.save(); }
test
Spring的工厂类
BeanFactory :老版本的工厂类,调用 getBean 时才会生成类的实例。
ApplicationContext :新版本的工厂类,加载配置文件时就会将 Spring 管理的类实例化。
- ClassPathXmlApplicationContext :加载类路径下的配置文件。
- FileSystemXmlApplicationContext :加载文件系统下的配置文件。
配置相关
Bean标签
属性:
- id :使用了唯一约束,不能出现特殊字符。
- name :没有使用唯一约束(理论上是可以出现重复的,但是实际开发中是不能出现的),可以出现特殊字符。
- init-method :初始化时执行该方法(在构造方法之后)。
- destroy-method :销毁时执行该方法(默认情况下 Spring 创建的 Bean 是单例的,在工厂类 ApplicationContext 实例 Close 时执行)。
- scope :配置 Bean 的作用范围。
scope 有如下几个可选值:
singleton :默认值,Spring 会采用单例模式创建这个对象。
prototype :多例的。
request :应用在 web 项目中,Spring 创建这个类对象后,将这个对象存放到 request 范围中。
session :应用在 web 项目中,Spring 创建这个类对象后,将这个对象存放的 session 范围中。
globalsession :应用在 web 项目中,必须在 porlet 环境下使用。
Spring的属性注入
为方便下面测试,先新建如下类:
package com.zze.bean; public class Department { public Department() { } public Department(String name) { this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Department{" + "name='" + name + '\'' + '}'; } }
com.zze.bean.Department
package com.zze.bean; public class User { public User() { } public User(String name, Integer age, Department department) { this.name = name; this.age = age; this.department = department; } private String name; private Integer age; private Department department; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", department=" + department + '}'; } }
com.zze.bean.User
构造方法注入
<bean name="department" class="com.zze.bean.Department"> <constructor-arg name="name" value="信息部"/> </bean> <bean name="user" class="com.zze.bean.User"> <constructor-arg name="name" value="李四"/> <constructor-arg name="age" value="30"/> <constructor-arg name="department" ref="department"/> </bean>
@Test public void test1() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Object user = applicationContext.getBean("user"); System.out.println(user); /* User{name='李四', age=30, department=Department{name='信息部'}} */ }
test
set方法注入
<bean name="department" class="com.zze.bean.Department"> <property name="name" value="信息部"/> </bean> <bean name="user" class="com.zze.bean.User"> <property name="name" value="张三"/> <property name="age" value="12"/> <property name="department" ref="department"/> </bean>
@Test public void test2() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Object user = applicationContext.getBean("user"); System.out.println(user); /* User{name='张三', age=12, department=Department{name='信息部'}} */ }
test
p名称空间注入
<!-- 在 Spring 2.5 之后支持。 需在 beans 标签中添加属性 xmlns:p="http://www.springframework.org/schema/p" 来引入 p 名称空间 --> <bean name="department" class="com.zze.bean.Department" p:name="信息部"/> <bean name="user" class="com.zze.bean.User" p:name="张三" p:age="21" p:department-ref="department"/>
@Test public void test3() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Object user = applicationContext.getBean("user"); System.out.println(user); /* User{name='张三', age=21, department=Department{name='信息部'}} */ }
test
SpEL注入
<!-- 在 Spring 3.0 之后支持。 --> <bean name="department" class="com.zze.bean.Department"> <property name="name" value="#{'推广部'}"/> </bean> <bean name="user" class="com.zze.bean.User"> <property name="name" value="#{'李四'}"/> <!--在表达式中可以有计算操作,并且可以直接调用对象属性及方法--> <property name="age" value="#{10+22}"/> <property name="department" ref="department"/> </bean>
@Test public void test4() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Object user = applicationContext.getBean("user"); System.out.println(user); /* User{name='李四', age=32, department=Department{name='推广部'}} */ }
test
- 注入 Array 类型:
-
package com.zze.bean; import java.util.Arrays; public class Customer { private String name; private String[] hobbies; public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getHobbies() { return hobbies; } public void setHobbies(String[] hobbies) { this.hobbies = hobbies; } @Override public String toString() { return "Customer{" + "name='" + name + '\'' + ", hobbies=" + Arrays.toString(hobbies) + '}'; } }
com.zze.bean.Customer
<!-- List 和 Set 的注入方式与 Array 一致 --> <bean name="customer" class="com.zze.bean.Customer"> <property name="name" value="二狗"/> <property name="hobbies"> <list> <value>吃饭</value> <value>睡觉</value> <value>打豆豆</value> </list> </property> </bean>
@Test public void test5() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Object user = applicationContext.getBean("customer"); System.out.println(user); /* Customer{name='二狗', hobbies=[吃饭, 睡觉, 打豆豆]} */ }
test
- 注入 Map 类型:
-
package com.zze.bean; import java.util.Map; public class TestBean { private Map<String, Object> map; public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } @Override public String toString() { return "TestBean{" + "map=" + map + '}'; } }
com.zze.bean.TestBean
<bean id="testBean" class="com.zze.bean.TestBean"> <property name="map"> <map> <entry key="1" value="a"/> <entry key="2" value="b"/> <entry key="3" value="c"/> </map> </property> </bean>
@Test public void test6(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Object testBean = applicationContext.getBean("testBean"); System.out.println(testBean); /* TestBean{map={1=a, 2=b, 3=c}} */ }
test
分模块配置
第一种方式是在创建工厂实例时一次性加载所有指定的配置文件,例:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml","applicationContext2.xml");
第二种方式是通过配置文件中的 import 标签引入其它的配置文件,例:
<import resource="com/zze/dao/applicationContext-dao.xml"/>