Spring是为了解决企业应用开发的复杂性而创建的一个开源框架;是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
核心容器:
-
spring-core:提供框架的基本组成部分,包括IoC和依赖注入功能;
-
spring-beans:提供BeanFactory,可以把配置和依赖从实际编码逻辑中解耦;
-
context:继承自Bean模块,且添加了国际化、事件传播、资源加载和透明地创建上下文等功能;
-
ApplicationContext接口是context模块的焦点;
-
spring-context-support提供了对第三方库集成到spring上下文的支持,如缓存、邮件、调度、模板引擎等;
-
-
spring-expression:提供强大的表达式语言,用于在运行时查询和操作对象图;
IoC容器
Spring容器是Spring框架的核心。容器创建对象,连接、配置他们,并管理他们的整个生命周期。通过阅读配置元数据(XML、Java注释或Java代码来表示)提供的指令,容器知道对哪些对象进行实例化、配置和组装。
-
BeanFactory:最简单的容器,给DI提供了基本支持;在org.springframework.beans.factory.BeanFactory中定义;
-
XmlBeanFactory类:从XML文件中读取配置元数据来生成一个配置化的系统或应用;
-
-
ApplicationContext:包含BeanFactory的所有功能(更好的选择),并添加了更多企业特定的功能。由org.springframework.context.ApplicationContext接口定义。
-
FileSystemXmlApplicationContext:从XML文件中加载已被定义的bean(需要完整路径);
-
ClassPathXmlApplicationContext:从XML文件中加载已被定义的bean(不需要完整路径,从CLASSPATH中搜索bean配置分解);
-
WebXmlApplicationContext:在一个Web应用程序范围内加载在XML文件中定义的bean;
-
bean是一个被实例化、组装,并通过Spring IoC容器所管理的对象。bean定义属性:
-
id:确定bean的唯一标识符;
-
class:强制的,用于创建的bean类;
-
scope:创建对象的作用域;
-
singleton:(默认方式)单例方式存在,容器中仅存在一个实例;
-
prototype:每次从容器中调用bean时,都返回一个新的实例;
-
request:每次HTTP请求时,都返回一个新的;
-
session:同个Session中共享一个;
-
global-session:一般用于Portlet应用环境;
-
-
constructor-arg:用于注入依赖关系;
-
properties:用于注入依赖关系;
-
autowiring mode:用于注入依赖关系;
-
lazy-initialization mode:延迟初始化(在第一次请求时初始化);
-
initialization方法(init-method):bean的所有必需属性被设定后,调用的回调方法;
-
destruction方法(destroy-method):被销毁时,调用的回调方法;
依赖注入
向一个对象传递引用,使用ref属性,传递值使用value属性。用constructor-arg注入构造函数:多个参数时,可用index标识(默认按顺序)。
<beans>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="2001"/>
<constructor-arg type="java.lang.String" value="Zara"/>
</bean>
</beans>
<beans>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="2001"/>
<constructor-arg index="1" value="Zara"/>
</bean>
</beans>
用property注入属性(类变量),可用p-namespace简化(以下为等价的两种注入方式):
<bean id="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean>
<bean id="john-classic" class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/>
</bean>
集合也可方便注入:
-
list:允许重复的一列值;
-
set:不允许重复的一列值;
-
map:键值对(可为任何类型);
-
props:键值对(只能为字符串);
<bean id="javaCollection" class="com.tutorialspoint.JavaCollection">
<!-- results in a setAddressList(java.util.List) call -->
<property name="addressList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</list>
</property>
<!-- results in a setAddressSet(java.util.Set) call -->
<property name="addressSet">
<set>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</set>
</property>
<!-- results in a setAddressMap(java.util.Map) call -->
<property name="addressMap">
<map>
<entry key="1" value="INDIA"/>
<entry key="2" value="Pakistan"/>
<entry key="3" value="USA"/>
<entry key="4" value="USA"/>
</map>
</property>
<!-- results in a setAddressProp(java.util.Properties) call -->
<property name="addressProp">
<props>
<prop key="one">INDIA</prop>
<prop key="two">Pakistan</prop>
<prop key="three">USA</prop>
<prop key="four">USA</prop>
</props>
</property>
</bean>
自动装配
通过autowire属性可制动自动装配模式:
-
no:默认设置,没有自动装配;
-
byName:由属性名自动装配;查找与属性名相同的bean(id);
-
byType:由属性类型自动装配;查找与属性类型相同的bean(class);
-
constructor:与byType类似,但适用于构造函数参数类型;
-
autodetect:先尝试constructor,再尝试byType;
基于注解的配置
从spring2.5开始可使用注解来配置注入。要启用注解,需要在配置文件中设定启用(表明自动连接值到属性、方法和构造函数):
<context:annotation-config/>
注解属性说明:
-
@Required:应用于bean的setter方法;表明属性值必须在XML配置文件中,否则会抛出BeanInitializationException;
-
@Autowired:可应用于bean的setter、非setter方法,构造函数和属性;
-
setter方法中,会试图执行byType自动连接;
-
@Autowired(required=false):关闭默认行为, 表示依赖非必须(XML配置中可不配);
-
-
@Qualifier("bean名字"):与Autowried配合,解决类型出现多个匹配的问题。
-
@Configuration:注解类,表示这个类可用IoC容器为bean定义的来源;
-
@Bean:表示方法将返回一个对象,该对象应注册为Spring应用程序上下文中的bean;
-
@Import(类名):允许ongoing另一个配置类中加载@Bean定义;
当加载beans时,ApplicationContext发布某些类型的事件;通过ApplicationEvent类和ApplicationListener接口处理事件:
-
ContextRefreshedEvent:ApplicationContext被初始化或刷新时;或ConfigurableApplicationContext接口中调用refresh时;
-
ContextStartedEvent:ConfigurableApplicationContext接口中调用start时;
-
ContextStoppedEvent:ConfigurableApplicationContext接口中调用stop时;
-
ContextClosedEvent:ConfigurableApplicationContext接口中调用close时;
-
RequestHandledEvent:web-specific事件,告诉bean HTTP请求已被服务;
public class CStopEventHandler
implements ApplicationListener<ContextStoppedEvent>{
public void onApplicationEvent(ContextStoppedEvent event) {
System.out.println("ContextStoppedEvent Received");
}
}
数据库操作JDBC框架
Spring JDBC框架中使用JdbcTemplate处理数据库Sql操作,SimpleJdbcCall 处理存储过程
通过DataSource设定数据源:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
通过RowMapper做数据映射
public class StudentMapper implements RowMapper<Student> {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
return student;
}
}