Spring学习第一天

来源:互联网 发布:中国馆建筑 知乎 编辑:程序博客网 时间:2024/05/29 18:22

1、Spring基本特征

spring框架图

Spring是一个非常活跃的开源框架;它是基于Core来构架多层JavaEE系统的框架,它的目的是简化企业开发。
Spring以一种非侵入式的方式来管理你的代码,Spring提倡“最少侵入”,这也就意味着你可以以适当的时候安装或卸载Spring。
所谓非侵入式简单说就是你的代码不需要依赖框架的代码,不需要继承框架的接口或某个类。

2、开发Spring所需要的工具

2.1Spring的jar包

--Spring的核心类库在Spring文档的dist下dist\spring.jar
只用Spring还需引入lib下的lib\jakarta-commons\commons-logging.jar
--如果使用切面编程(AOP),还需要引入下列jar文件
lib\aspectj\aspectjweaver.jar和aspectjrt.jar
lib\cglibcglib-nodep-2.1.3.jar
--如果使用了JSR-250中的注解,如@Resource、@PostConstruct、@PreDestroy,还需要下列jar文件

lib\j2ee\common-annotation.jar
更多关于jar包的介绍参见:http://blog.sina.com.cn/s/blog_783fd24b01011moa.html

2.2Spring配置文件

默认情况下是applicationContext.xml文件,可以建立多个xml文件,工程中一般都是这样配置的。

3、Spring基本功能详解

3.1SpringIOC

Spring的控制反转:把对象的创建、初始化、销毁等工作交给Spring容器来做。由Spring容器控制对象的生命周期。

步骤:
A:启动Spring容器
1、在类路径下寻找配置文件来实例化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(new Spring[]{"beans.xml"});//在整个类路径下寻找xnl文件
注意:*通过这种方式加载,需要将Spring的配置文件放到当前项目的classpath路径下。
    *classpath路径指的是当前项目的src目录,该目录是java源文件的存放路径(也即项目部署后的classes目录)
2、在系统路径下寻找配置文件来实例化容器
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{"d:\\beans.xml"});//Spring的配置文件可以指定多个,盘符根据情况修改
注意:常用第一种方式(还有其它方式,此处不做介绍了)
B、从Spring容器中提取对象

3.2别名

<bean>
<alias name="person" alias="p"/>
<bean name="person" class="com.test.aliasspring.Person"/>
</bean>
该配置可以完成,在一个地方命名,在多个地方使用不同的名字的效果。

3.3Spring容器内部对象

3.3.1创建对象的方式

1、无参构造函数
<bean id="personService" class="com.text.bean.impl.PersonServiceImpl"/>
2、静态工厂
<bean id="personService" class="com.text.factory.PersonServiceFactory"
factory-method="createPersonServie"/>
对应的类为:
public class PersonServiceFactory{
public static PersonService createService(){
return new PersonServiceIml();
}
}
3、实例工厂
了解概念即可

3.3.2对象的scope

1、singleton(默认值)
在每个Spring IoC容器中一个bean只有一个对象实例(共享)。
默认情况下会在容器启动时初始化bean,但是可以通过指定bean结点的lazy-init="true"来延迟初始化bean,
这个时候,只有第一次获取bean才会初始化bean。
如:
<bean id="xxx" class="..." lazy-init="true"/>
如果想对所有的bean都使用延迟初始化,可以在根节点beans设置default-lazy-init="true",如下:
<beans default-lazy-init="true"/>
2、prototype
允许bean可以被多次实例化(使用一次就创建一个人实例),Spring不能对一个prototype bean的整个生命周期负责,这就意味着清楚prototype
作用域的对象并释放并释放prototype bean所持有的昂贵资源都是客户端的责任。
3、Request
在一次Http请求中,一个bean定义对应一个实例;即每次Http请求将会有各自的bean实例,它们依据某个bean定义创建而
成,该作用域仅在基于Web的Spring ApplicationContext情形下有效。
4、Session
在一个Http Session中,一个bean定义对应一个实例,该作用域仅在基于Web的Spring ApplicationContext情形下有效。
5、Global Session
在一个全局的Http Session中,一个bean定义对应一个人实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅
在基于web的Spring ApplicationContext情形下有效。

3.3.3初始化beam时机

Spring默认情况在启动时将所有singleton bean提前进行实例化。提前实例化意味着作为初始化的一部分,ApplicationContext会
自动创建并配置所有的singleton bean。通常情况下这是件好事,因为这样配置中有任何错误能立即发现。
Lazy-init=“true or false”
为false时,Spring容器将在启动时报错(默认便是)。
为true时,Spring容器将在调用该bean时报错。

3.3.4 init、destory

Spring初始化bean或销毁bean时,有时需要做一些处理工作,因为Spring可以在创建和销毁bean的时候调用Spring的
两个生命周期方法。
<bean id="foo" class="...Foo"
init-method="setUp"
destory-method="teardown"/>
当foo被加载入Spring容器时调用init-method方法,当foo从容器中删除时调用destory-method(scope=singleton有效)。

3.4 依赖注入(DI)

3.4.1 xml形式

1、使用构造器注入
A:通过参数的顺序
...<construct-arg index="0">
<value>张三</value>
    </construct-arg>...
B、通过参数的类型
...<construct-arg type="java.lang.String">
<value>张三</value>
   </construct-arg>...
C、通过参数的名字(推荐使用)
...<construct-arg name="name">
<value>张三</value>
   </construct-arg>...
C、直接注入(同顺序注入)
...<construct-arg value="张三">
   </construct-arg>...
2、使用属性的setting方法进行注入
A、简单bean注入
简单bean包括两种类型:包装类型和String
<bean id="PersonService" class="com.test.bean.PersonServiceImpl">
<property name="age" value="20"/>
</bean>
B、引用其它bean
<bean id="person" class="com.test.bean.Person"/>
<bean id="personService" class="com.test.bean.impl.PersonServiceImpl">
<property name="person" ref="person">
</bean>

3、装配list集合
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="person"/>
<list>
</property>
4、装配set集合
<property>
<set>
<value>set1</value>
<value>set2</value>
<ref bean="person"/>
</set>
</property>
5、装配map
<property>
<map>
<entry key="p1" value="map01"/>
<entry key="p2" value="map02"/>
</map>
</property>
需要注意的是,map中key值必须是String。

6、装配properties
<property>
<props>
<props key="p1">prop1</props>
</props>
</property>

3.4.2 注解

步骤:
A、在配置文件中,引入context命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instace"
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">


B、在配置文件中加入context:annotation-config标签
<context:annotation-config/>
这个配置隐式注册了多个对注释进行解析处理的处理器
AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
注意:@Resource注解在Spring安装目录的lib\j2ee\common-annotations.jar中
1、@Autowired
这两个注解的区别是:@Autowried默认按类型装配;@Resource默认按名称装配,当找不到与名称匹配的bean时
才会按类型装配。(注解可写在属性上,也可以写在setter上)
如果存在多个或者没有则会抛出BeanCreationException异常。
@Autowired注解是按类型装配依赖对象,默认情况下它要我依赖对象必须存在,如果允许null值,可以设置它的required属性为false。
2、@Qualifier
如果想用按名称匹配,可以结合@Qualifier注解一起使用。则自动注入的策略从byType转成byName。
3、@Resource
A、@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上。
B、@Resource注解默认按名称装配
名称可以通过@Resource的name属性指定,如果没有指定name属性,则取属性字段的名字寻找依赖对象。
注意:如果没有指定name属性,并且按照默认的名称找不到依赖对象时,@Resource注解会回退到按类型装配。但是一旦指定了name属性,就
只能按名称装配了。
4、@PostConstruct
指定bean的初始化方法。
5、@PreDestroy
指定bean的销毁方法。

3.5 扫描

以上为使用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/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="com.test"/>
</beans>
2、在配置文件中添加context:component-scan标签
<context:compoent-scan base-package="com.test"/>
其中base-package为需要扫描的包(包含子包)。

注意:
A、在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor会隐式地被包含
进来。也就是说,每个组件都会被自动检测并织入,所有都不需要在xml中提供任何bean配置元数据。、
B、功能介绍
@Service用于标记业务层组件、@Controller用于标注控制层组件(如Struts中的action)
@Repository用于标注数据访问组件,即DAO组件、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标记

3.6 Spring中的继承

在bean结点中使用属性parent指向父类的bean。

3.7 IOC和DI的意义

IOC(inverse of control)控制反转:所谓控制反转就是把对象(bean)对象和维护对象(bean)之间的关系的权利转移到Sqring容器中去了(ApplicationContext.xml)而 程序本身不在维护了
DI(dependency injection)依赖注入:实际上DI和IOC是同一个概念,因为在ApplicationContext.xml配置文件中bean和bean之间通过ref来维护的时候是相互依赖 的,所以又叫做依赖注入。也就是控制反转。




1 0
原创粉丝点击