Spring

本文深入解析Spring框架的核心组件、事务管理及依赖注入机制,包括事务配置、应用范围、Web层整合与XML配置方式。重点阐述了Spring的IOC容器、依赖注入(DI)的概念及实现方式,如构造子注入、setter注入和接口注入。同时,介绍了Spring AOP用于面向方面编程,增强POJO运行能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。图1是Spring的总体架构图。


图1 Spring 框架的总体架构


  • Core Container

在图1中的Core Container中的Core组件 和Bean组件提供了Spring最基本的特性:IOC(控制反转)和DI(依赖注入)特性。这里最主要的概念是BeanFactory。

Data Access/Integeration

Data Access/Integeration中的Transaction组件,用于事务编程和声明事务管理。如下所示为Spring+struts+hibernate框架中的applicationContext.xml文件中的

事务管理器的配置:

<!-- 配置事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>  
<!-- 配置Advice(事务的传播特性) -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务管理器应用的范围 -->
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.chengbin.client.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>

  • Web

Web层包括Web、Web-Servlet。

1)       Spring IOC

控制反转(IOC)是Spring整个框架的核心部份,所谓IOC,其实就是将设计好的类交给系统Spring框架去控制。基于IOC基础上的依赖注入(DI)方式来管理对象之间的依赖关系,使组件之间的依赖关系由组件的代码中来管理维护转移到容器来管理配置,有助于黏合程序中的各个不同模块,形有一个有逻辑意义的整体,简化业务层的工作量,提高模块的复用度。


Spring的IOC容器

Spring的两个最基本最重要的包是:org.springframework.beans和org.springframework.context包。这两个包中的代码为Spring的反向控制特性(也叫作依赖注射)提供了基础。如图二是 Spring IOC容器,其中的元数据通常是通过XML文件进行配置。当运用基于XML的形式配置元数据时,为Spring IOC 容器需要

管理的Bean 进行定义。



图二 Spring IOC容器


最基本的配置方式:

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">




<bean id=" " class=" " scope=" ">
<property name=" ">
<ref bean=" " />
</property>
</bean>
<bean id=" " class=" " scope=" ">
<property name=" ">
<ref bean=" " />
</property>
</bean>

 <!--more beans to go here -->

</beans> 


public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public ExampleBean(
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}


BeanFactory提供了能够管理任何种类beans(对象)的先进的配置机制,潜在地利用任何一种存储设备。ApplicationContext建立在BeanFactory之上并增加了其他的功能,比如同Spring AOP特性更容易整合,信息资源处理(用于国际化),事件传播,声明式机制用于创建ApplicationContext和可选的父上下文以及与应用层相关的上下文(比如WebApplicationContext),以及其他的增强。

简而言之,BeanFactory提供了配置框架和基本的功能,而ApplicationContext为它增加了更强的功能,这些功能中的一些或许更加J2EE和企业中心(enterprise-centric)。一般来说,ApplicationContext是BeanFactory的完全超集,任何BeanFactory功能和行为的描述也同样被认为适用于ApplicationContext。

用户有时在特定的场合下不确定BeanFactory和ApplicationContext哪一个更适于使用。通常大部分在J2EE环境中构建的应用最好的选择是使用ApplicationContext,因为它不仅提供了BeanFactory所有的特性以及它自己附加的特性,而且还提供更声明化的方法去使用一些功能,这通常是令人满意的。你最好选择BeanFactory的主要场景通常是当内存使用是最主要的关注(比如在一个每kb都要计算的applet中),而且也不需要ApplicationContext所有特性的时候。


依赖注入(DI)

依赖注入存在的最常用的两种形式:构造子注入(Constructor Injection)和setter注入(Setter Injection),还有一种方式是接口注入。

  • 构造子注入(Constructor Injection)

<bean id="exampleBean" class="examples.ExampleBean">
<!-- constructor injection using the nested <ref/> element -->
<constructor-arg>
<ref bean="anotherExampleBean"/>
</constructor-arg>
<!-- constructor injection using the neater 'ref' attribute -->
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg type="int" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

  • setter注入(Setter Injection)

<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter injection using the nested <ref/> element -->
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<!-- setter injection using the neater 'ref' attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

  • 接口注入

接口注入指的就是在接口中定义要注入的信息,并通过接口完成注入。

public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}


2)       Spring AOP

Spring AOP可以以面向方面编程的方式增强POJO的运行能力,弥补OOP之缺憾,从另一个角度方便的解决了开发中所需的公共处理,例如它可向POJO组件提供方法拦截,透明的为方法执行增加服务如事务、检测、日志记录等公共服务。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值