Spring框架学习路线
- Spring的IOC
- Spring的AOP,AspectJ
- Spring的事务管理,三大框架的整合
Spring框架概述
什么是Spring?
Spring是分层的JavaSE/EE full-stack(一站式)轻量级开源框架。
所谓分层:
- SUN提供的EE的三层结构:web层、业务层、数据访问层(也称持久层,集成层)。
- Struts2是web层基于MVC设计模式框架。
- Hibernate是持久的一个ORM的框架。
所谓一站式:Spring框架有对三层的每层解决方案。
- web层:Spring MVC
- 持久层:JDBC Template
- 业务层:Spring的Bean管理
以IoC(Inverse of Control 反转控制)和AOP(Aspect Oriented Programming 面向切面编程为内核)
官网:http://www.springsource.org/
Spring的出现是为了取代EJB的臃肿、低效、脱离现实
官网:http://www.springsource.org/
Spring的出现是为了取代EJB的臃肿、低效、脱离现实
Spring的核心
- IOC:(Inverse of Control 反转控制) 所谓的控制反转就是将对象的创建权交给Spring完成。
- AOP:Aspect Oriented Programming 面向切面编程 是面向对象的延伸,不是替换面向对象,是用来解决OO中的一些问题。
- DI:依赖注入(即在Spring创建对象的时候 注入对象的属性)
Spring的版本:Spring3.x和Spring4.x Spring4需要整合hibernate4. 所以一般使用Spring3.
EJB:企业级JavaBean
- 2002 : Expert One-to-One J2EE Design and Development
- 2004 : Expert One-to-One J2EE Development without EJB (EE开发真正需要使用的内容.)
使用Spring的好处:
(1) 方便耦合,简化开发
Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理。
(2) AOP编程的支持
Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能。
(3)声明式事务的支持
只需要通过配置就可以完成对事务的管理,而无需手动编程。
(4)方便程序的测试
Spring对Junit支持,可以通过注解方便的测试Spring程序。
(5)方便集成各种优秀框架
Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如Struts、Hibernate、MyBatis、Quartz等)的直接支持
(6) 降低JavaEE API的使用难度
Spring对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低。
Spring体系结构
Spring框架是一个分层框架,它包含一系列的功能要素并被分为大约20个模块。这些模块分为Core Container、Data Access/Integration、Web、AOP(Aspect Oriented Programming)、Instrumentation和测试部分,如下图所示:
Spring IOC控制反转快速入门示例
大致流程:
- 下载Spring最新开发包。
- 复制Spring开发jar包到工程。
- 理解IOC控制反转和DI依赖注入。
- 编写Spring核心配置文件。
- 在程序中读取Spring配置文件,通过Spring框架获得Bean,完成相应的操作。
(1) 下载Spring开发包
官方下载Spring3.x最新开发版本
需要开发包和开发依赖包:
spring-framework-3.2.0.RELEASE-dist.zip — Spring开发包
* docs :spring框架api和规范
* libs :spring开发的jar包
* schema :XML的约束文档.
spring-framework-3.0.2.RELEASE-dependencies.zip — Spring开发中的依赖包
* docs :spring框架api和规范
* libs :spring开发的jar包
* schema :XML的约束文档.
spring-framework-3.0.2.RELEASE-dependencies.zip — Spring开发中的依赖包
(2) 创建web工程引入相应jar包
包括核心包和日志有关的包
核心包:
- spring-beans-3.2.0.RELEASE.jar
- spring-Context-3.3.0.RELEASE.jar
- spring-core-3.2.0.RELEASE.jar
- spring-expression-3.2.0.RELEASE.jar
日志有关的包:
- com.springsource.org.apache.commons.logging-1.1.1.jar 用于整合其他的日志的包(类似Hibernate中slf4j)
- com.springsource.org.apache.log4j-1.2.15.jar log4j的包
注意:commons-logging日志整合包以及log4j的包在Spring的依赖包中
提示:spring3.0.X 版本 asm jar包 已经被合并到 spring core包中
(3) 创建测试类。
在HelloTest类中使用He'llService类对象
传统方式:HelloService helloService=new HelloService();
IOC Inverse of Control 反转控制概念,就是将原本在程序中手动创建HelloService对象的创建权,交由Spring框架管理,简单说,就是创建HelloService对象控制权被反转到了Spring框架。
(4)创建Spring配置文件
在src下创建Spring的核心配置文件applicationContext.xml,将HelloService的创建交由容器管理。
引入XML约束:在解压后的开发包中搜索xsd-config.xml引入beans约束
配置如下图所示:
注意:DI与IOC的区别
IOC:控制反转,将对象的创建权交给Spring管理。
DI:依赖注入,在Spring创建对象的过程中,把对象依赖的属性注入到类中。
(5) 加载Spring框架配置文件
ApplicationContext应用上下文,加载Spring框架配置文件(两种方式):
加载classpath下:
new ClassPathXmlApplicationContext("applicationContext.xml");
加载磁盘路径下:
new FileSystemXmlApplicationContext("applicationContext.xml");
通过getBean方法获得Spring容器管理Bean对象
BeanFactory和ApplicationContext区别:
区别大致如下:
ApplicationContext类继承了BeanFactory。
BeanFactory采取延迟加载,第一次getBean时才会加载这个类。
ApplicationContext类加载配置文件的时候,创建所有的类。
ApplicationContext对BeanFactory进行了扩展,提供了更多的功能。如下:
- 国际化处理。
- 事件传递。
- Bean自动装配。
- 各种不同应用层的Context实现。
BeanFactory的使用方法如下:
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
HelloService helloService = (HelloService) factory.getBean("helloService");
helloService.sayHello();
HelloService helloService = (HelloService) factory.getBean("helloService");
helloService.sayHello();
注意:在BeanFactory加载配置文件时也分为classpath路径和磁盘路径(即ClassPathResource和FileSystemResource)
MyEclipse解决Spring配置文件无法提示的问题
方案一: 联网下载
- http://www.springframework.org/schema/beans/spring-beans.xsd
- Myeclipse window-preferences- 搜索xml catalog
- 选中User Specified Entries -- Add 操作
- location 浏览选中 解压spring 包中 schema\beans\spring-beans-3.2.xsd
- 修改Key type 为 Schema location
- key 修改为 http://www.springframework.org/schema/beans/spring-beans.xsd
- 点击OK
如下图所示:
Spring底层IOC的原理
在开发中一般由web层调用业务层业务层调用持久层
- 面向对象:最传统的web层调用业务层代码:UserService userService=new UserService(); 这种最原始的方式不具备扩展性。
- 面向接口:按照面向对象的面向接口的方式编程 将UserService提取为一个接口,可以实现接口提供各种真实实现类Impl 即第二个阶段:UserService userService=new UserServiceImpl(); 将程序具体的实现编写到程序中,根据需求切换底层的实现,虽然使程序具备了扩展性。但是在切换底层实现时需要修改源代码,造成程序紧密耦合。
- OCP原则—工厂模式:按照Java的OCP原则,即open-close原则:扩展功能尽量不要修改源程序,提供新的方法属性的形式扩展其功能,意思就是对扩展是开放的对修改是关闭的。可以使用工厂模式进行扩展。
public class BeanFactory{
public UserService getUserService(){
return new UserServiceImpl();
}
}
即把程序与实现类的耦合转成与工厂的耦合。与实现类完成解耦合的形式:
BeanFactory factory= ......;
UserService service=factory.getUserService();
为了解除与工厂的耦合,在利用反射在工厂内部动态的生成对象。
public class BeanFactory{
public UserService getUserService(){
// 反射+配置文件
return Class.forName(类名).newInStance();
}
}
xml、properties:className=cn.test.service.UserServiceImpl
Spring快速入门程序示例:
在web项目中导入jar包,并在src下新建log4j.properties日志配置文件和ApplicationContext.xml
log4j.properties
为了方便测试,暂时将日志关闭
src下的applicationContext.properties
为了测试磁盘路径的资源获取方式 并在工程根目录下新建applicationContext.xml,,如下所示:
HelloService
HelloServiceImpl
新建入门测试类SpringTest1
IOC容器装配Bean(XML配置方式)
Spring框架实例化Bean的方式
有三种实例化Bean的方式:构造方法实例化(默认无参数)、静态工厂实例化、实例工厂实例化
方式一:使用类构造器实例化(默认无参数)
<bean id=“personService" class="cn.test.bean.impl.PersonServiceImpl"/>
方式二:使用静态工厂实例化(简单工厂模式)
<bean id="personService" class="cn.test.factory.PersonServiceFactory" factory-method="createPersonService" />
public class PersonServiceFactory {
public static PersonService createPersonService(){
return new PersonServiceImpl();
}
}
public class PersonServiceFactory {
public static PersonService createPersonService(){
return new PersonServiceImpl();
}
}
方式三:使用实例工厂方法实例化(工厂方法模式)
<bean id="personServiceFactory" class="cn.test.factory.PersonServiceFactory" />
<bean id="personService" factory-bean="personServiceFactory" factory-method="createPersonService" />
public class PersonServiceFactory {
public PersonService createPersonService(){
return new PersonServiceImpl();
}
}
public PersonService createPersonService(){
return new PersonServiceImpl();
}
}
示例代码如下:
applicationContext.xml
发现每次调用获取ApplicationContext对象时都会重新全部加载配置中所有的Bean类
Bean的其他配置
id和name的区别
Bean的命名id属性和name属性:
- 一般情况下,装配一个Bean时,通过指定一个id属性作为Bean的名称。
- id属性在IOC容器中必须是唯一的。
- id的命名要满足XML对ID属性的命名规范:必须以字母开头,可以使用字母、数字、连字符、下划线、句号、冒号。
- 如果Bean的名称中含有特殊字符,就需要使用name属性:例如 <bean name="person" class="cn.test.bean.person"/>
- 因为name属性可以相同,所以后出现Bean会覆盖之前出现的同名的Bean。
Bean的作用域
在<bean>标签上的scope属性指对象的范围 取值可以有以下几种:
- singleton: 单例的。在Spring IOC容器中仅存在一个Bean实例,Bean以单例方式存在。
- prototype: 多例的。 每次从容器中调用Bean时,都会返回一个新的实例,即每次调用getBean()时,相当于执行new XxxBean()。
- request: 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境。相当于在web开发中创建了一个对象,并将这个对象存入request域范围,request.setAttribute();
- session: 同一个HTTP Session共享一个Bean,不同Session使用不同Bean,仅适用于WebApplicationContext环境。相当于在web开发中,创建了一个对象,将这个对象存入session范围,即session.setAttribute();
- globalSession: 一般用于Porlet应用环境,该作用域仅适用于WebApplicationContext环境。Porlet指的是分布式开发,如果不是Porlet环境,globalSession等同于session。
实际开发中主要使用singleton,prototype。
程序示例如下:
ApplicationContext.xml
Spring容器中Bean的生命周期
配置Bean的初始化和销毁的方法:Spring初始化或销毁bean时,有时需要做一些处理工作,因此Spring可以在创建和拆卸bean的时候调用bean的两个生命周期中的方法
注意:
销毁方法只对单例范围的bean有效,即只有单例范围的bean在销毁时调用配置到销毁方法。
web容器中会自动调用销毁方法,但是main()函数或测试用例需要手动调用,必须关闭工厂才能销毁bean。
Spring容器中Bean生命周期图解:
bean的生命周期大致分为11个步骤:
1. instantiate bean 对象实例化。
2. populate properties 封装属性。
3. 如果Bean实现BeanNameAware接口 执行setBeanName。
4. 如果Bean实现BeanFactoryAware 或者ApplicationContextAware接口设置工厂setBeanFactory或者上下文对象setApplicationContext。
5.
如果存在类实现BeanPostProcessor(后处理Bean),执行PostProcessBeforeInitialization。
6. 如果Bean实现InitializingBean 执行afterPropertiesSet。
7. 调用<bean init-method="init"> 指定初始化方法init。
8.
如果存在类实现BeanPostProcessor(处理Bean),执行postProcessAfterInitialization。
9. 执行业务逻辑处理。
10. 如果Bean实现DisposableBean接口 则执行destroy方法。
11. 调用<bean destroy-method="customerDestroy"> 指定销毁方法customerDestroy。
程序示例如下:
applicationContext.xml
依赖注入Bean的属性
对于类成员变量,注入方式有三种:
- 构造方法(构造器)注入。
- 属性setter方法注入。
- 接口注入。
如下所示:
Spring只支持构造器和Setter方法注入。
依赖注入Bean属性—构造方法注入
使用构造方法注入,在Spring配置文件中,通过<constructor-arg>设置注入的属性(可以通过index或者type注入)
注意:也可以使用<constructor-arg name="name" value="宝马"> 普通属性时value 对象属性为ref
依赖注入Bean属性—setter方法注入
使用setter方法注入,在Spring配置文件中,通过<property>设置注入的属性
以上设置的只是普通属性,还可以使用<property>引入其他Bean
如果bean对应的类中有与对象相关的成员变量时,可以使用ref替代value属性。
名称空间p: 注入属性(属于setter注入)
- 使用p命名空间。
- 为了简化XML文件配置,Spring2.5开始引入一个新的p名称空间。
- p:<属性名>="xxx" 引入常量值。
- p:<属性名>-ref="xxx" 引用其他Bean对象。
SpEL注入
Spring3.0 创建了一种新的方式用以配置对象的注入(set注入或者构造参数注入),便是SpEL(Spring Expression Language ) Spring表达式语言,对依赖注入进行简化。
语法:#{ 表达式 } 例如: <bean id="" value="#{表达式}" />
基础特性:SpEL 使用#{ ... }作为定界符,所有大括号中的字符都将被认为是SpEL。
(1) 字面量的表示
1>整数: <property name="count" value="#{5}">
2>小数: <property name="frequency" value="#{89.7}">
3>科学计数法: <property name="capacity" value="#{1e4}">
4>String可以使用单引号或者双引号作为字符串的定界符号。
<property name="
name
" value=
"#{'Chuck'}"
/>
<property name='
name
' value=
'#{"Chuck"}'
/>
5>Boolean: <property name="enabled" value="#{false}"/>
(2) 引用Bean,属性和方法
1> 引用其他对象
<bean id="saxophone" value="com.xxx.xxx.Xxx" />
<bean ...>
<property name="instrument" value="#{saxophone}"
</bean>
通过id:"saxophone" 将对象注入到instrument属性中,这与下面的配置是一样的:
<property name="instrument" ref="saxophone"/>
2> 引用其他对象的属性
<bean id="carl" class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="#{kenny.song}" />
</bean>
kenny 是 Bean Id 而 song 是属性的名称,这样配置就等同于写了如下一段代码:
<property name="song" value="#{kenny.song}" />
</bean>
kenny 是 Bean Id 而 song 是属性的名称,这样配置就等同于写了如下一段代码:
Instrumentalist carl = new Instrumentalist();
carl.setSong(kenny.getSong());
carl.setSong(kenny.getSong());
3> 调用别的对象的其它方法,并取得返回值。
<property name="song" value="songSelector.selectSong()"/>
调用了BeanId为"songSelector"的对象的selectSong()方法,并将返回值注入到song属性中。或者还可以进行链式操作。如:<property name="song" value="songSelector.selectSong().toUpperase()" />
在链式操作中,如果songSelector.selectSong()返回null的还会抛出异常,为了避免异常。我们要使用 ?. 表达式。这样如果songSelector.selectSong()为null就不会再调用后面的方法了。如下所示:
<property name="song" value="songSelector.selectSong()?.toUpperCase()"/>
4>调用静态方法
我们已经知道如何通过一个对象调用它的方法了,但是如何调用一个静态方法呢? 用T{}。它将返回一个Class Object,然后我们再调用相应的方法即可:
<property name="multiplier" value="T(java.lang.Math).PI"/>
(3) SPEL支持的运算符号
1> 算数运算符: +,-,*,/,%,^
<property name="adjustedAmount" value="#{counter.total + 42}"/>
<property name="adjustedAmount" value="#{counter.total - 20}"/>
<property name="circumference" value="#{2 * T(java.lang.Math).PI * circle.radius}"/>
<property name="average" value="#{counter.total / counter.count}"/>
<property name="remainder" value="#{counter.total % counter.count}"/>
<property name="area" value="#{T(java.lang.Math).PI * circle.radius ^ 2}"/>
<property name="circumference" value="#{2 * T(java.lang.Math).PI * circle.radius}"/>
<property name="average" value="#{counter.total / counter.count}"/>
<property name="remainder" value="#{counter.total % counter.count}"/>
<property name="area" value="#{T(java.lang.Math).PI * circle.radius ^ 2}"/>
加号还可以用作连接字符串
<property name="fullName" value="#{performer.firstName + ' ' + performer.lastName}"/>
2>比较运算符:<,>,==,<=,>=,lt,gt,eq,le,ge
<property name="equal" value="#{counter.total == 100}"/>
注意:不可以使用<和>号,因为在xml中它有特殊的含义,可以使用lt和gt代替
<property name="hasCapacity" value="#{counter.total le 100000}"/>
3> 逻辑运算符:and,or,not,|
<property name="largeCircle" value="#{shape.kind == 'circle' and shape.perim
eter gt 10000}
"/>
<property name="outOfStock" value="#{!product.available}"/>
<property name="outOfStock" value="#{not product.available}"/>
<property name="outOfStock" value="#{not product.available}"/>
4> If-else 运算符:?: (ternary), ?: (Elvis)
〇最基本的 ?:(这如同我们在使用 EL 表达式语言):
〇最基本的 ?:(这如同我们在使用 EL 表达式语言):
<property name="instrument" value="#{songSelector.selectSong() == 'Jingle Bells' ? piano : ' Jingle Bells '}"/>
〇变体的 ?:
<property name="song" value="#{kenny.song != null ? kenny.song : 'Greensleeves'}"/>
上下两种是同一语义,但下面的明显简洁
<property name="song" value="#{kenny.song ?: 'Greensleeves'}"/>
<property name="song" value="#{kenny.song != null ? kenny.song : 'Greensleeves'}"/>
上下两种是同一语义,但下面的明显简洁
<property name="song" value="#{kenny.song ?: 'Greensleeves'}"/>
5> 正则表达式
<property name="validEmail" value="#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}'}"/>
表达式返回逻辑值,如果匹配返回true,否则返回false
SPEL对集合的支持:
——环境
有实体City定义如下:
package com.habuma.spel.cities;
public class City {
private String name;
private String state;
private int population;
public class City {
private String name;
private String state;
private int population;
}
XML中有如下定义:
<util:list id="cities">
<bean class="com.habuma.spel.cities.City" p:name="Chicago" p:state="IL" p:population="2853114"/>
<bean class="com.habuma.spel.cities.City" p:name="Chicago" p:state="IL" p:population="2853114"/>
<bean class="com.habuma.spel.cities.City" p:name="Atlanta" p:state="GA" p:population="537958"/>
<bean class="com.habuma.spel.cities.City" p:name="Dallas" p:state="TX" p:population="1279910"/>
<bean class="com.habuma.spel.cities.City" p:name="Houston" p:state="TX" p:population="2242193"/>
<bean class="com.habuma.spel.cities.City" p:name="Odessa" p:state="TX" p:population="90943"/>
<bean class="com.habuma.spel.cities.City" p:name="El Paso" p:state="TX" p:population="613190"/>
<bean class="com.habuma.spel.cities.City" p:name="Jal" p:state="NM" p:population="1996"/>
<bean class="com.habuma.spel.cities.City" p:name="Las Cruces" p:state="NM" p:population="91865"/>
</util:list>
<bean class="com.habuma.spel.cities.City" p:name="Dallas" p:state="TX" p:population="1279910"/>
<bean class="com.habuma.spel.cities.City" p:name="Houston" p:state="TX" p:population="2242193"/>
<bean class="com.habuma.spel.cities.City" p:name="Odessa" p:state="TX" p:population="90943"/>
<bean class="com.habuma.spel.cities.City" p:name="El Paso" p:state="TX" p:population="613190"/>
<bean class="com.habuma.spel.cities.City" p:name="Jal" p:state="NM" p:population="1996"/>
<bean class="com.habuma.spel.cities.City" p:name="Las Cruces" p:state="NM" p:population="91865"/>
</util:list>
——1、获取Collection中的某个对象
1> 通过下标访问,如:<property name="chosenCity" value="#{cities[2]}"/>
我们就会获得 population 为"1279910"的 city(记住下标从 0 开始),对象下标可以通过变量指定,如下:
<property name="chosenCity" value="#{cities[T(java.lang.Math).random() * cities.size()]}"/>
2> 如果对象是从Map集合中获得,可以指定key值,如下所示:
<property name="chosenCity" value="#{cities['Dallas']}"/>
3> Bean对象也可以通过key访问properties的值,如下所示:
<util:properties id="settings" location="classpath:settings.properties"/>
<property name="accessToken" value="#{settings['twitter.accessToken']}"/>
<property name="accessToken" value="#{settings['twitter.accessToken']}"/>
4> 对象可以通过下标获取String串中的某个字符
'This is a test'[3]
——2、获取 Collection 中的子集-通过条件筛选(注意新对象是一个新的 Collection)
1>筛选子集(.?[])
1>筛选子集(.?[])
<property name="bigCities" value="#{cities.?[population gt 100000]}"/>
2>获取第一个(.^[])
<property name="aBigCity" value="#{cities.^[population gt 100000]}"/>
3>获取最后一个(.$[])
<property name="aBigCity" value="#{cities.$[population gt 100000]}"/>
——3、集合的投影(.![])
如果想获得所有城市的名称组成的列表,可用如下操作
<property name="cityNames" value="#{cities.![name]}"/>
将返回"Chicago", "Atlanta", "Dallas"
也可以组合两个列,如下:
<property name="cityNames" value="#{cities.![name + ', ' + state]}"/>
将返回"Chicago, IL", "Atlanta, GA", and "Dallas, TX".
—— 4、将投影和筛选结合
<property name="cityNames" value="#{cities.?[population gt 100000].![name +', ' + state]}"/>
<property name="aBigCity" value="#{cities.^[population gt 100000]}"/>
3>获取最后一个(.$[])
<property name="aBigCity" value="#{cities.$[population gt 100000]}"/>
——3、集合的投影(.![])
如果想获得所有城市的名称组成的列表,可用如下操作
<property name="cityNames" value="#{cities.![name]}"/>
将返回"Chicago", "Atlanta", "Dallas"
也可以组合两个列,如下:
<property name="cityNames" value="#{cities.![name + ', ' + state]}"/>
将返回"Chicago, IL", "Atlanta, GA", and "Dallas, TX".
—— 4、将投影和筛选结合
<property name="cityNames" value="#{cities.?[population gt 100000].![name +', ' + state]}"/>
实例代码如下:
Car
Car2
Person
PersonInfo
applicationContext.xml
SpringTest5
运行结果如下:
集合类型的属性注入
(1) List 数组
(2)Set 集合
(3) Map
(3)Properties
使用多个XML配置文件
方式一:可以在创建ApplicationContext对象时传入多个配置文件。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans1.xml", "beans2.xml");
方式二:可以在配置文件中通过<import>引入其他配置文件
<import resource="classpath:bean2.xml"/>
实例代码如下:
在src下新建两个Spring的配置文件
SpringTest6
applicationContext.xml
applicationContext2.xml
运行测试类
IOC容器装配Bean(注解方式)
Spring注解装配Bean
Spring2.5引入使用注解去定义Bean:
@Component描述Spring框架中Bean
需要在配置文件中引用Context的Schema名称空间,在解压后的dist包中搜索 xsd-config.html文件,打开后找到context的名称空间约束如下所示:
配置如下所示:
如果不使用XML注册Bean 单纯使用注解声明Bean的话,不需要使用<context:annotation-config>标签,只需要配置自动扫描即可。<context:annotation-config>标签仅仅适用于XML和注解混搭使用时才需要的。
除了@Component外,Spring提供了3个功能和@Component等效的注解,如下所示:
- @Repository用于对DAO实现类进行标注。
- @Service用于对Service实现类进行标注。
- @Controller用于对Controller实现类进行标注。
以上三个注解是为了让标注类本身的用途清晰,Spring在后续的版本会对其增强。
自动装配Bean
使用@Autowired进行自动注入,使用@Service标注业务类,@Repository标注DAO。@Autowired默认按照类型进行注入,如果存在两个相同的Bean类型相同,则按照名称注入。@Autowired注入时可以针对成员变量或者setter方法。
注意:如果在使用注解标识Bean的情况下,如果没有显式的指定Bean的name名称,那么在使用applicationContext获取Bean对象时,它的name默认是这个Bean类名称的驼峰式写法,并且需要指定是哪个类,如下所示:
通过@Autowired的required属性,设置一定要找到匹配的Bean,如果设置为true,一旦找不到所匹配的Bean类型就会抛出异常,默认是true。如果设置成false的话,即使搜索不到所匹配的Bean类也不会报错。
使用@Qualifier指定注入Bean的名称,必须注意的是使用@Qualifier指定Bean名称后,注解Bean必须指定相同的名称,如下图所示:
对于普通属性使用@Value,另外Spring提供对JSR-250中定义@Resource标准注解的支持,@Resource和@Autowired注解功能类似,@Autowired和@Qualifier结合使用等价于@Resource。@Qualifier意思是按照名称进行注入。
指定Bean的初始化和销毁方法
Spring初始化Bean或销毁Bean时,有时需要作一些处理工作,因此Spring可以在创建和拆卸Bean的时候调用Bean的两个生命周期方法。如下图所示:
Bean的作用范围@Scope
使用注解配置的Bean和<bean>的配置一样,默认作用范围都是singleton,@Scope注解用于指定Bean的作用范围。
示例代码如下:
UserDao
UserService
SpringTest1
applicationContext.xml
注意:<context:component-scan base-package="">会扫描当前包下的所有类的注解以及所有子包的类注解。
运行结果如下:
Spring3.0提供使用Java类提供Bean定义信息
Spring3.0以JavaConfig为核心,提供使用Java类定义Bean信息的方法。
- @Configuration指定POJO类为Spring提供Bean定义信息
- @Bean提供一个Bean定义信息
如果在Spring的配置文件中配置了<context:component base-package="">按照包进行注解扫描的属性标签,则无需再手动加载配置类。否则需要手动加载@Configuration配置类。手动加载配置类如下所示:
- Spring提供AnnotationConfigApplicationContext用于加载使用@Configuration配置注解工厂类。
- register方法用于向注解上下文对象添加一个配置类。
- refresh刷新容器以应用这些注册的配置类。
如果采用了手动加载@Configuration配置类的情况,即使没有Spring配置文件也能照常运行。
示例代码如下:
Car
Product
BeanConfig
applicationContext.xml
SpringTest2
运行结果:
传统XML配置和注解配置混合使用
如果混合使用,一般使用XML注册Bean 进行Bean的管理,使用注解进行属性的注入。混合使用配置方法如下:
1、引入context命名空间
2、在配置文件中添加<context:annotation-config>标签
注意:单独使用注解声明注册Bean的话只需要配置扫描的标签,混合使用时只需要使用<context:annotation-config>
示例代码如下:
CustomerDao
CustomerService
applicationContext2.xml
SpringTest3
运行结果如下:
多种装配Bean方式比较
Spring整合web开发
正常整合Servlet和Spring是没有问题的,但是每次执行Servlet的时候都会加载Spring配置,加载Spring环境。
解决方法:在Servlet的init方法中加载Spring配置文件,这种方法虽然可以解决每次请求都会加载Spring配置的问题,但是只能应用于当前的Servlet,其他的Servlet无法使用。所以最好的方法是将加载的信息内容放到ServletContext中。ServletContext对象是全局对象,在服务器启动的时候创建的,在创建ServletContext的时候就加载Spring环境。
ServletContextListener:用于监听ServletContext对象的创建和销毁的。
web应用中使用Spring的步骤如下:
1、导入响应的jar包
导入Spring开发基本的jar包:spring-beans-3.2.0.RELEASE.jar、spring-context-3.2.0.RELEASE.jar、spring-core-3.2.0.RELEASE.jar、spring-expression-3.2.0.RELEASE.jar;
导入日志相关jar包:commons-logging-1.1.1.jar、com.springsource.org.apache.log4j-1.2.15.jar
导入Spring web开发的jar包:spring-web-3.2.0.RELEASE.jar
2、配置web.xml
将Spring容器初始化,交由web容器负责。
配置核心监听器ContextLoaderListener(ContextLoaderListener实现了ServletContextListener接口)。
配置全局参数contextConfigLocation,用于指定Spring框架的配置文件的位置。
3、获得WebApplicationContext对象
因为Spring容器已经交由web容器初始化和管理。
获得WebApplicationContext对象,需要依赖ServletContext对象。通常在Servlet中完成。
WebApplicationContext applicationContext=WebApplicationContextUtils.getWebApplicationContext(getServletContext());
还可以用另外一种方式进行获取
WebApplicationContext applicationContext=(WebApplicationContext)getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
示例代码如下:
UserService
UserServlet
web.xml
applicationContext.xml
log4j.properties
访问Servlet 后台打印如下:
使用Junit测试Spring程序
导入Spring test测试jar包,需要导入spring-test-3.2.0.RELEASE.jar
示例代码如下:将applicationContext.xml中的命名空间改为Context的命名空间。导入整合Junit的jar包
运行结果如下: