Spring IoC容器之基于xml配置

  • 依赖注入(Dependency Injection)

    • 基于Constructor和Setter注入

    • Constructor(构造)注入

    • 基于setter注入

    • 解析依赖项过程

    • 基于xml的Ioc容器配置


依赖注入(Dependency Injection)

Dependency injection (DI) is a process whereby objects define their dependencies (that is, the other objects with which they work) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes or the Service Locator pattern. ——引自Spring官方文档

依赖注入(DI)是一个过程,通过该过程,对象仅通过构造函数参数、工厂方法的参数,或在构造、或在创建对象实力后,在对象实力上设置的属性来定义依赖关系。从工厂方法返回, 容器在创建Bean时注入依赖项。此过程从根本上讲是通过类的直接构造或服务定位模式来控制bean自身依赖或位置的bean本身逆过程(因此称之为Control Inversion)

使用DI原理,代码更加简洁,对象之间依赖耦合度更低。


基于xml的Ioc容器配置

xml基础配置如下

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
>

</beans>

基于Constructor和Setter注入


Constructor(构造)注入

例如以下代码,构造函数中有两个参数

public class IScoreDao {
private String name;
private int score;

public IScoreDao(String name, int score) {
this.name = name;
this.score = score;
}
}

此时在xml中配置有三类如下:

  1. 构造函数参数类型匹配,通过 type 属性显示指定构造参数类型

<beans>
<bean id="scoreDao" class="dao.IScoreDao">
<constructor-arg type="java.lang.String" value="English"/>
<constructor-arg type="int" value="90"/>
</bean>
</beans>

此种方式有着明显的缺陷,如果构造参数类型都一样,都是String或int类型,那么这种方式就不行

  1. 构造函数参数索引,使用 index 指定构造参数的索引(索引从0开始)

<beans>
<bean id="scoreDao" class="dao.IScoreDao">
<constructor-arg index="0" value="English"/>
<constructor-arg index="1" value="90"/>
</bean>
</beans>

此方式解决了构造参数类型相同的奇异问题

  1. 构造函数参数名称

<beans>
<bean id="scoreDao" class="dao.IScoreDao">
<constructor-arg name="name" value="English"/>
<constructor-arg name="score" value="90"/>
</bean>
</beans>

此方式可消除构造参数中的歧义

注意:使用构造函数注入依赖,value 属性只能用于基本类型(Integer,bollean…)和String类型的构造参数,其它类型则需使用 ref 属性


基于setter注入

通过调用无参构造函数或static的工厂方法创建bean实例后,在通过setter方法完成。

例如以下代码:

public class UserserviceImpl implements IUserService {

private IUserDao userDao;

public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
}

xml中的配置如下:

  <!--注入service-->
<bean id="service.IUserService" class="serviceImpl.UserserviceImpl">
<!--成员变量: 注入dao-->
<property name="userDao" ref="userDao"></property>
</bean>

<!-- 配置dao对象-->
<bean id="userDao" class="dao.UseDaoImpl"></bean>

  • spring可同时支持

    constuctor

     和 

    setter

    ,就是活,两者可联合使用。将构造函数用于强制依赖项,setter用于可选依赖项是一个很好的方式。


解析依赖项过程

  • 使用ApplicationContext描述所有bean的实例化和初始化(存入Map容器中)。可使用xml、java代码或注解来指定配置元数据。

  • 对于每个bean,其依赖项属性都是以属性、构造参数、static-factory方法的参数的形式表示。创建bean时,依赖项会提供给bean。

  • 每个属性或构造参数都要设置实际意义,或者是对另一个容器中的bean的引用。

  • 每个属性或构造参数的类型都会从指定格式转为实际类型。spring默认将string类型的所有值转为内置基础类型(long,boolean…)和string类型。

在创建容器时,spring会检查每个bean的配置,默认以单例模式创建bean的实例对象,但可通过 scope 属性指定作用范围。不适用单例模式创建bean实例时,会在请求(需要调用bean)时,才会创建bean。

以下代码是创建bean并使用的示例:

 ApplicationContext context = null;
IUserService userService = null;

@Before
public void getApplicationContext() {
context = new ClassPathXmlApplicationContext("bean.xml");
userService = context.getBean(IUserService.class.getName(),IUserService.class);
}

@Test
public void finAllTest() throws SQLException {
List<UserEntity> entityList = userService.findAll();
for (UserEntity userEntity : entityList) {
System.out.println(userEntity);
}
}

ApplicationContext初始化容器和创建bean,有了bean对象之后,就可以调用它的方法了。


~Spring IoC容器之基于xml配置未完待续Spring IoC容器之基于xml配置~


原文始发于微信公众号(程序员玄之):Spring IoC容器之基于xml配置

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/43040.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!