[Spring in Action] 学习笔记之一

使用Spring开发项目有一年多时间,对Spring的IOC、AOP建立在”what”,”how”的阶段.没有深入学习其底层实现机制.
近期从杭图借阅[Spring in Action]该书,深入学习,这是第一篇笔记.

版本为:Springframework-2.0.8.

Spring的几大核心模块jar包:

1.spring-core.jar 核心的Spring容器和实体类.

2.spring-aop.jar Spring的AOP框架和元数据的支持

3.spring-context.jar Application context,验证框架,模板的支持(Velocity,FreeMarker),远程服务(JAX-RPC、Hessian、Burlap),EJB支持和作业调度服务

4.spring-dao.jar JDBC和DAO的支持.实务架构.

5.spring-web.jar Web application context和实体类.Multipart 文件上传的支持.

6.spring-webmvc.jar Spring的MVC框架.

另外,旧版本中的spring-orm.jar包分成了spring-hibernate2.jar、spring-hibernate3.jar、spring-ibatis.jar、spring-jdo.jar等四个jar包.

ApplicationContext & BeanFactory 都是载入Bean的定义信息,装配Bean,根据需要分发Bean.但是ApplicationContext有以下额外支持:

1.应用上下文提供了文本信息解析工具,包括对国际化的支持.

2.应用上下文提供了载入文件资源的通用方法,如载入图片.

3.应用上下文可以向注册为监听器的bean发送事件.

其中三个常用的ApplicationContext常用的实现类是:

ClasspathXmlAppicationContext:顾名思义就是从类路径中的XML文件载入上下文定义信息,把上下文定义文件当成类路径资源.

FileSystemXmlApplicationContext:从文件系统中的XML文件载入上下文定义信息.

XmlWebApplicationContext:从Web系统中的XML文件载入上下文定义信息.

ApplicatonContext 与BeanFactory 另一个重要的区别是:BeanFactory 延迟加载所有的Bean,当getBean()方法被执行时,利用反射机制生成Bean.而ApplicationContext在上下文启动时就载入所有的单实例Bean.

Spring 中的Bean 缺省情况下是单例模式.为何要这么设计呢?

首先要明白单例模式是什么?单例模式与多线程有什么关系?

单例引出并发的问题,既然实例只能有一个,那么多线程就可能同时或不同时访问它,那么它的状态(属性成员)的一致性就是个问题,写入方法必须同步(Synchronized).

Spring支持Constructor 和 Seter 的Independence Injection.

<constructor-arg  index=”0″>

<value>StringValue</value>

</constructor-arg>

构造函数的参数位置,可以通过index属性来设置.索引从0开始.

<bean>标签中的auto-wire属性:

四种不同的类型:1.byName -试图在容器中寻找和需要自动装配的的属性名相同的Bean(或ID),如果没有找到相符的Bean,这个属性就没有被装配上.

2.byType -试图在容器中寻找一个与需要自动配置的属性类型相同的Bean.如果没有找到相符的Bean,这个属性就没有被装配.如果找到超过一个相符的Bean,会抛出org.springframework.beans.factory,UnsatisfiedDependencyException.

3.constructor -试图在容器中查找与需要自动装配的bean的构造器函数参数一致的一个或多个bean.

4.autodetect -首先尝试constructor来自动装配,然后使用byType方式.

不建议让Spring容器自动装配,应该显示的装配Bean,这样可以在XML文件中看出各个bean之间的关系.

-未完待续-

About heely

Currently,I was working at ISOFTSTONE as a Java SoftWare Engineer since May 2008. I guaduated as a Bachelor of Hebei University in June 2008. I was interested in OpenSource Software,J2EE,Linux&Unix,Web Development,Photography,Outdoor Sports,Traveling.
This entry was posted in J2EE and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>