Change Dir

先知cd——热爱生活是一切艺术的开始

统计

留言簿(18)

积分与排名

“牛”们的博客

各个公司技术

我的链接

淘宝技术

阅读排行榜

评论排行榜

spring复习速记

正统的学习笔记没有记,为什么?提醒自己,spring你是学过的,这次复习,只是把要点记下来就可以了,也是为什么只看spring in action的原因:

 

Spring基础学习注记

Spring是轻量级的,AOP的,DI的,框架的,容器的

轻量级:体积(2.5M),开销

DI:通过DI来松耦。所谓依赖注入,即组件之间的依赖关系由容器在运行期决定,形象的来说,即由容器动态的将某种依赖关系注入到组件之中

AOP:理解为“切面”比较合适,AOP则是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。OOD/OOP面向名词领域,AOP面向动词领域。AOP还有另外一个重要特点:源码组成无关性。

容器的:负责AO的lifecycle和configuration。可以定义AO如何被创建、如何配置和如何相互关联。

框架:配置和组合应用,管理AO,通过XML配置,并提供很多基础设施服务,比如事务管理,持久化框架集成。

Spring’s core container:提供基本功能,包括BeanFactoryDI的基础,spring框架的基础)。

Application context modulecore使spring像个容器,而context则使其像一个框架。常用的一个应用就是集成velocityfreemarker的一个集成点。

Spring’s AOP module:顾名思义。

JDBC abstraction and the DAO module集成数据库操作。这个模块会利用AOP模块提供的事务管理功能。

Object-relational mapping (ORM) integration moduleDAO的基础上提供ORMsolutionSpring不实现自己的ORM,只是提供一个与ORM框架结合的接口,比如HibernateJPA,JDO,IBATIS等。同样spring的事务管理也与这些框架相结合。

Java Management Extensions (JMX)Exposing the inner workings of a Java application for management is a critical part of making an application production ready. Spring’s JMX module makes it easy to expose your application’s beans as JMX MBeans. This makes it possible to monitor and reconfigure a running application.

Java EE Connector API (JCA)

The enterprise application landscape is littered with a mishmash of applications

running on an array of disparate servers and platforms. Integrating these applications

can be tricky. The Java EE Connection API (better known as JCA) provides a

standard way of integrating Java applications with a variety of enterprise information

systems, including mainframes and databases.

In many ways, JCA is much like JDBC, except where JDBC is focused on database

access, JCA is a more general-purpose API connecting to legacy systems. Spring’s

support for JCA is similar to its JDBC support, abstracting away JCA’s boilerplate

code into templates.

The Spring MVC framework

ORM不同,MVC模块既结合已有的像StrutsTapestryWebWork这样的框架,也提供了自己的MVC框架。

Spring Portlet MVC

Many web applications are page based—that is, each request to the application

results in a completely new page being displayed. Each page typically presents a

specific piece of information or prompts the user with a specific form. In contrast,

portlet-based applications aggregate several bits of functionality on a single web

page. This provides a view into several applications at once.

If you’re building portlet-enabled applications, you’ll certainly want to look at

Spring’s Portlet MVC framework. Spring Portlet MVC builds on Spring MVC to provide

a set of controllers that support Java’s portlet API.

Spring’s web module

提供MVC模块需要的classes

Remoting

Not all applications work alone. Oftentimes, it’s necessary for an application to

leverage the functionality of another application to get its work done. When the

other application is accessed over the network, some form of remoting is used

for communication.

Spring’s remoting support enables you to expose the functionality of your Java

objects as remote objects. Or if you need to access objects remotely, the remoting

module also makes simple work of wiring remote objects into your application as

if they were local POJOs. Several remoting options are available, including

Remote Method Invocation (RMI), Hessian, Burlap, JAX-RPC, and Spring’s own

HTTP Invoker.

Java Message Service (JMS)

The downside to remoting is that it depends on network reliability and that both

ends of the communication be available. Message-oriented communication, on

the other hand, is more reliable and guarantees delivery of messages, even if the

network and endpoints are unreliable.

Spring’s Java Message Service (JMS) module helps you send messages to JMS

message queues and topics. At the same time, this module also helps you create message-driven POJOs that are capable of consuming asynchronous messages.

解释一个springxml

<beans>是根元素。

<bean>告诉容器如何配置一个classid属性表示这个bean对象的名字,class属性表示这个bean对象的具体class

<bean>下的<property>用于设置一个该bean对象的属性propertyname属性表示这个property的名字,value属性传递给这个property值。用property这种方式,等同于利用bean对象的set方法。

<bean>下的<constructor-arg><property>类似,只是这个等同于利用bean对象的赋值构造函数。

粗解DI

The key benefit of DI is loose coupling.

Interface programming

通过配置文件xml,写beanfactory等,来实现简单注入,从而松耦。

粗解AOP

解释aopxml配置元素:

<aop:config>解释要做aop配置,是spring aop基本必备的元素

——<aop:aspect>表示声明一个aspect,这个aspect的功能被定义在其ref属性中,ref一般会联系一个已经定义好的bean对象。一个aspectpointcuts(功能在哪里实现)和advice(功能怎样实现)组成。

————<aop:pointcut>定义一个pointcut,触发条件是某方法被执行。

————<aop:before>顾名思义,一个advice

————<aop:after>同上。

Basic bean wiring

The act of creating these associations between application objects is the

essence of dependency injection (DI) and is commonly referred to as wiring.

Spring container

BeanFactory

顾名思义,遵循工厂模式的一个类,管理和分发beans

通用的工厂,可以管理分发各种类型的beans

Beanfactory参与了bean的整个生命周期。

多种实现方式,最常用的是org.springframework.beans.factory.xml.XmlBeanFactory,通过加载xml来实现。当然必须要传递一个Resourcebeanfactory。例:BeanFactory factory = new XmlBeanFactory(new FileSystemResource("c:/beans.xml"));

得到一个bean的方法就是调用getBean()。比如:MyBean myBean = (MyBean) factory.getBean("myBean");

application context

等同于beanfactory,但是提供more

1. 支持I18N的消息

2. 提供一般基本方法去加载file resources

3. 发布events到已经注册为listenerbeans

ClassPathXmlApplicationContextFileSystemXmlApplicationContext比较常用。例:ApplicationContext context = new FileSystemXmlApplicationContext("c:/foo.xml"); ApplicationContext context = new ClassPathXmlApplicationContext("foo.xml");

bean’s life

Creating Beans

分两步,首先声明一个bean,即定义一个类。然后注入,即为其注入属性内容。

何时选择setter注入,何时选择constructor注入,见spring in action原书第45页。

Injecting into bean properties

通过<property>元素完成注入。其等价于调用bean对应类的setXXX方法。

例:<property name="song" value="Jingle Bells" />

其中value的值,spring会根据property的具体类别来选择不同的数据类型。

<property name="instrument" ref="saxophone" />这样的声明中,表示该property是一个类的对象,其类就是已经声明过的某个beansaxophone

<property>下直接定义bean叫做inner bean。(当然通过constructor道理相同)

对于复杂集合类型:

其中的map需要注意一下。Props的代码实现上与map也不同。

Autowiring

The four types of autowiring

byName:找到name相匹配的,wire

byType:找到type匹配的,wire

constructor:利用constructor参数匹配,wire

autodetect:先constructor,再byType

Controlling bean creation

有多少对象实例,就由多少bean

从静态的工厂去构造bean,而不是constructor

构造好bean后要及时初始化,在bean销毁之前要及时clean up

Bean scoping

所有的spring beans都是单例的。

通过设置<bean>scope=”prototype”spring可以在需要的时候产生新的bean的实例。



Initializing and destroying beans

<bean>中有init-methoddestroy-method负责设置初始化和销毁时调用的方法。

如果很多bean都有同样的初始化和销毁程序,那么可以在<beans>中设置defaultinit-

method default-destroy-method

还有种方法就是bean类的定义时,让类去实现InitializingBean DisposableBean.

Advising beans

重点讲述AOP

Introducing AOP

Aspect帮助我们建模那些cross-cutting concerns(一个应用中在多个位置起到作用的点,比如security)。


Defining AOP terminology

AOP一般用advicepointcutjoinpoint描述。

Adviceaspect必定是在做一个job,即要做某个功能。其中这个job就叫做adviceAdvice定义了aspectwhatwhen

Joinpoint:在整个application中有可能用到advice的连接点。

Pointcut:定义了aspectwhere

Aspect:是advicepointcutmerger

Spring’s AOP support

Spring支持4种风格的aop

Springadvice都是java写的。

Spring在运行时advise object


Spring只支持方法连接点method joinpoint

Spring MVC




其中,一个command object 就是一个像struts中的actionform一样的东西。

Wizard是一个可以将多个页面联合起来作为一个form的东西。


基本搞完了,总的来讲,学习的东西就要经常用,否则慢慢的就会淡忘了,尤其是技术上的一些细节。如果没遇到类似的问题,是很难记忆深刻的~~~

posted on 2011-03-19 13:12 changedi 阅读(562) 评论(0)  编辑  收藏 所属分类: Java技术


只有注册用户登录后才能发表评论。


网站导航: