- Spring基本特征
Spring是一个非常活跃的开源框架;它是一个基于Core来构架多层JavaEE系统的框架,它的主要目地是简化企业开发.
Spring以一种非侵入式的方式来管理你的代码,Spring提倡”最少侵入”,这也就意味着你可以适当的时候安装或卸载Spring
- 开发spring所需要的工具
- Spring的jar包
到http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
--spring的核心类库 在spring文档的dist下
dist\spring.jar
--引入的第三方类库 都spring文档的lib下
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar
注:JSR(Java 规范请求)是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。任何人都可以提交JSR(Java 规范请求),以向Java平台增添新的API和服务。JSR已成为Java界的一个重要标准
-
- Spring配置文件
默认情况下是applicationContext.xml文件。可以建立很多xml文件,工程中一般都是这样配置的。
- Spring基本功能详解
- SpringIOC
Spring的控制反转:把对象的创建、初始化、销毁等工作交给spring容器来做。由spring容器控制对象的生命周期。
步骤:
- 启动spring容器
- 在类路径下寻找配置文件来实例化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});可以在整个类路径中寻找xml文件
* 通过这种方式加载。需要将spring的配置文件放到当前项目的classpath路径下
* classpath路径指的是当前项目的src目录,该目录是java源文件的存放位置。
- 在文件系统路径下寻找配置文件来实例化容器
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});Spring的配置文件可以指定多个,可以通过String数组传入。
注:经常用第一种方法启动容器
- 从spring容器中提取对象
- 别名
<beans>
<alias name="person" alias="p"/>
<bean name="person" class="cn.itcast.aliasspring.Person"/>
</beans>
通过这样的配置,可以达到在一个地方命名,在多个地方使用不同的名字的效果。
-
- Spring容器内部对象
- 创建对象的方式
- 无参构造函数
- 创建对象的方式
- Spring容器内部对象
<bean id=“personService" class="cn.itcast.bean.impl.PersonServiceImpl"/>
-
-
-
- 静态工厂
-
-
<bean id="personService" class="com.itcast.factory.PersonServiceFactory" factory-method="createPersonService" />
public class PersonServiceFactory {
public static PersonService createPersonService(){
return new PersonServiceImpl();
}
}
-
-
-
- 实例工厂
-
-
记住概念即可
-
-
- 对象的scope
- singleton(默认值)
- 对象的scope
-
在每个Spring IoC容器中一个bean定义只有一个对象实例(共享)。
默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:
<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>
-
-
-
- prototype
-
-
允许bean可以被多次实例化(使用一次就创建一个实例) . Spring不能对一个prototype bean的整个生命周期负责.这就意味着清楚prototype作用域的对象并释放任何prototype bean所持有的昂贵资源都是客户端的责任。
-
-
-
- Request
-
-
-
-
-
- Session
-
-
-
-
-
- Global session
-
-
-
-
- 初始化bean时机
-
Spring默认在启动时将所有singleton bean提前进行实例化。提前实例化意味着作为初始化的一部分,ApplicationContext会自动创建并配置所有的singleton bean.通常情况下这是件好事。因为这样在配置中有任何错误能立即发现。
Lazy-init=”true or false”
Lazy-init 为false,spring容器将在启动的时候报错(比较好的一种方式)
Lazy-init 为true,spring容器将在调用该类的时候出错。
-
-
- init、destroy
-
Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法。
<bean id=“foo” class=“...Foo”
init-method=“setup”
destory-method=“teardown”/>
当foo被载入到Spring容器中时调用init-method方法。当foo从容器中删除时调用destory-method(scope = singleton有效)
-
- 依赖注入(DI)
- xml形式
- 使用构造器注入
- xml形式
- 依赖注入(DI)
使用xml的注入方式
- 通过参数的顺序
<constructor-arg index="0">
<value>张三</value>
</constructor-arg>
<constructor-arg index="1">
<value>56</value>
</constructor-arg>
- 通过参数的类型
<constructor-arg type="java.lang.Integer">
<value>56</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>张三</value>
</constructor-arg>
-
-
-
- 使用属性setting方法进行注入
-
-
使用xml的注入方式:
- 简单Bean的注入
简单Bean包括两种类型:包装类型和String
<bean id="personService" class="com.itcast.bean.impl.PersonServiceImpl">
<!-- 基本类型,string类型 -->
<property name="age" value="20"></property>
<property name="name" value="张无忌"></property> </bean>
- 引用其他Bean
<bean id="person" class="com.itcast.bean.Person" />
<bean id="personService" class="com.itcast.bean.impl.PersonServiceImpl">
<property name="person" ref="person" />
</bean>
-
-
-
- 装配list集合
-
-
-
-
-
- 装配set集合
-
-
-
-
-
- 装配map
-
-
<property name="maps">
<map>
<entry key="01">
<value>map01</value>
</entry>
<entry key="02">
<value>map02</value>
</entry>
</map>
</property>
map中的<entry>的数值和<list>以及<set>的一样,可以使任何有效的属性元
素,需要注意的是key值必须是String的。
-
-
-
- 装配Properties
-
-
<property name="props">
<props>
<prop key="01">prop1</prop>
<prop key="02">prop2</prop>
</props>
</property>
-
-
- 注解
-
步骤:
- 在配置文件中,引入context命名空间
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring- beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- 在配置文件中加入context:annotation-config标签
<context:annotation-config/>
这个配置隐式注册了多个对注释进行解析处理的处理器
AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,
PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar
-
-
-
- @Autowired
-
-
这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。
-
-
-
- @Qualifier
-
-
如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
-
-
-
- @Resource
-
-
- @Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上.
- @Resource注解默认按名称装配。
名称可以通过@Resource的name属性指定,如果没有指定name属性,
- 当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象
- 当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。
- 注意:如果没有指定name属性,并且按照默认的名称找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。
-
-
- @PostConstruct
-
-
指定Bean的初始化方法
-
-
-
- @PreDestroy
-
-
指定Bean的销毁方法
-
- 扫描
前面的例子我们都是使用XML的bean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找及维护起来也不太方便。spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件是一样的。要使用自动扫描机制,我们需要打开以下配置信息:
1、引入context命名空间 需要在xml配置文件中配置以下信息:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="cn.itcast"/>
</beans>
2、在配置文件中添加context:component-scan标签
<context:component-scan base-package="cn.itcast"/>
其中base-package为需要扫描的包(含子包)。
注:
1、在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 所有这一切都不需要在XML中提供任何bean配置元数据。
2、功能介绍
@Service用于标注业务层组件、
@Controller用于标注控制层组件(如struts中的action)、
@Repository用于标注数据访问组件,即DAO组件。
而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
-
- spring中的继承
Person类如图所示:
Student类如图所示:
配置文件中:
图中的配置文件中,parent为student在容器中继承person.如果去掉person是不行的。
-
- IOC和DI的意义