Spring IOC
发布日期:2021-06-29 12:51:23 浏览次数:2 分类:技术文章

本文共 3805 字,大约阅读时间需要 12 分钟。

        Spring IOC是Spring最重要也是最基础的两个特性之一(别一个是AOP,现不在讨论)。Spring框架的实现控制反转(IoC)的原则,也被称为依赖注入(DI)。过程对象定义它们的依赖关系,也就是说,他们使用的其它对象,只能通过构造函数参数,参数工厂方法或对象实例上设置的属性构造或从工厂回来后的方法。然后容器注入这些依赖项时创建bean。这个过程从根本上是反,因此得名“控制反转(IoC),控制实例化bean本身或者它的位置依赖关系通过使用直接建设类,或者一个Service Locator模式等机制。

1、Spring IOC(DI)

分为set注入、构造器注入与静态工厂方法注入用得最多的还是set注入与构造器注入。

1.1 Set注入

对应的Java类为:
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;  }}

1.2 构造器注入

Spring还有一种DI方式是使用构造器注入值.

对应的Java实体类为:
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;  }}
构造器中的参数对应实体类 ExamleBean 中的构造器中的参数。

1.3  静态工厂方法

现在考虑一下下面的例子。Spring同样也可以用一个静态工厂方法取代构造器注入来返回对象的实例.
上面的代码对应的Java类为:

public class ExampleBean {  // a private constructor  private ExampleBean(...) {    ...  }    // a static factory method; the arguments to this method can be  // considered the dependencies of the bean that is returned,  // regardless of how those arguments are actually used.  public static ExampleBean createInstance (          AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {      ExampleBean eb = new ExampleBean (...);      // some other operations...      return eb;  }}

2、p-namespace -- 更加简洁的set注入

我们的开发中一定要涉及到数据库的开发。一般我们配置数据库使用下面的方法:

如果我们在Spring的XML配置文件中引用p-namespace命名空间。我们就可以这样配置上面的文件
是不是更加简洁了?

3、c-namespaces -- 更加简洁的构造器注入

与p-namespaces一样我们同样需要引入c-namespaces命名空间。
<-- 'traditional' declaration -->
<-- 'c-namespace' declaration -->

4、idref -- 检测ID bean是否存在

这个 idref 元素只是一简单的检测一下配置为这个当前bean在<constructor-arg/>或者<property/>标签中使用别一个ID的bean文件在Spring容器标签中是否存在。
等同于下面的配置

5、inner beans

一个bean元素包含在<constructor-arg/>或者<property/>之中的配置被称为inner bean也就是Java中的内部类。

6、Collection

你可以使用Spring中的<list/>, <set/>, <map/>和<props/>标签,用来设置Java中Collection类型,分别对应的是List,Set,Map和Properties。

administrator@example.org
support@example.org
development@example.org
a list element followed by a reference
just some string
对于Map的key或者value,set的值同样也可以使用下面的标签。

bean | ref | idref | list | set | map | props | value | null
同样对于Collection,Spring也支付Collection包含Collection。但是对于<list/>, <map/>, <set/>或者<props/>这些集合类型,Spring只支付配置它们本身。就是<list/>的子标签只能是</list>.<props>子标签只能包含<props/>.

administrator@example.com
support@example.com
sales@example.com
support@example.co.uk

7、null值或者空String

Spring对待空参数就像空字符串一样。下面的基于xml的配置元数据片段将email属性设置为空字符串值(" ")

上在的例子同样等价于Java中的 exampleBean.setEmail("");<null />标签代表null值,例如:
上在的例子同样等价于Java中的 exampleBean.setEmail(null);

转载地址:https://carlzone.blog.csdn.net/article/details/51578875 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Web系统大规模并发——电商秒杀与抢购
下一篇:三种妙法搞定冗余表数据一致性

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月30日 05时55分20秒