java速记_spring复习速记

Spring基础学习注记

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

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

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

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

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

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

d00005bacd7e6e68e9671cb36dd69975.png

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

Application context module:core使spring像个容器,而context则使其像一个框架。常用的一个应用就是集成velocity和freemarker的一个集成点。

Spring’s AOP module:顾名思义。

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

Object-relational mapping (ORM) integration module:在DAO的基础上提供ORM的solution。Spring不实现自己的ORM,只是提供一个与ORM框架结合的接口,比如Hibernate,JPA,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’sJMXmodule makes it easy to expose your application’s beans asJMXMBeans. 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 JavaEEConnectionAPI(better known asJCA) provides a

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

systems, including mainframes and databases.

In many ways,JCAis much likeJDBC, except whereJDBCis focused on database

access,JCAis a more general-purposeAPIconnecting to legacy systems. Spring’s

support forJCAis similar to itsJDBCsupport, abstracting awayJCA’s boilerplate

code into templates.

The Spring MVC framework

与ORM不同,MVC模块既结合已有的像Struts、Tapestry、WebWork这样的框架,也提供了自己的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 PortletMVCframework. Spring PortletMVCbuilds on SpringMVCto provide

a set of controllers that support Java’s portletAPI.

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 localPOJOs. Several remoting options are available, including

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

HTTPInvoker.

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 toJMS

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

解释一个spring的xml:

是根元素。

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

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

下的与类似,只是这个等同于利用bean对象的赋值构造函数。

粗解DI

155b7fa5c67a5d917b032337ba4be5a9.png

The key benefit ofDIis loose coupling.

Interface programming

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

粗解AOP

解释aop的xml配置元素:

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

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

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

————顾名思义,一个advice

————同上。

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

60f4d8c4b14c711a7b606da1d67575f6.png

BeanFactory

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

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

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

多种实现方式,最常用的是org.springframework.beans.factory.xml.XmlBeanFactory,通过加载xml来实现。当然必须要传递一个Resource给beanfactory。例: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到已经注册为listener的beans

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

bean’s life

3406f7a048c61bdf14fce4cc3132394e.png

e11248f86d19ede8b5adaf607746da2c.png

Creating Beans

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

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

Injecting into bean properties

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

例:

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

这样的声明中,表示该property是一个类的对象,其类就是已经声明过的某个bean:saxophone。

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

对于复杂集合类型:

3bb91daa85696bd9ed0515fa7d8ef856.png

其中的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都是单例的。

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

5836944a1d2ce5800313346844a29dcb.png

Initializing and destroying beans

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

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

method 和 default-destroy-method

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

Advising beans

重点讲述AOP。

Introducing AOP

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

cad361837b8eb6925c81ff5b8226c120.png

Defining AOP terminology

AOP一般用advice、pointcut和joinpoint描述。

5d0981f64fe562fdaaf4d01e64f82a05.png

Advice:aspect必定是在做一个job,即要做某个功能。其中这个job就叫做advice。Advice定义了aspect的what和when。

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

Pointcut:定义了aspect的where。

Aspect:是advice和pointcut的merger。

Spring’s AOP support

Spring支持4种风格的aop:

Spring的advice都是java写的。

Spring在运行时advise object。

61a1811a72ee46d3e719108dc59d3d1a.png

Spring只支持方法连接点method joinpoint。

Spring MVC

1abc1417d485d47e57070c81098f046d.png

9e7eb26573cfd748da5363d6021d9ec3.png

a54b8e91b077b5cb00c3aa4d247265e9.png

2dc0726378b379a577da8785620ae9aa.png

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

8593e82d6b9997f3508e90b597db27ea.png

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

25f4907050f1c81e340c001398425300.png

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值