Spring IOC

Ioc(Inversion of control)控制反转。

在Spring中我们将beans的创建交给了 Ioc容器。它将读取配置文件来创建beans实例。

Spring提供了两种Ioc容器的实现:一种是通过bean工厂(Bean Factory),更高级的一种是bean工厂的扩展应用程序上下文(Application context)。

bean工厂和应用程序上下文的两个接口分别是BeanFactory和ApplicationContext(是BeanFactory的子接口)。

ApplicationContext的实现:

  ClassPathXmlApplicationContext:用于装载classpath下的配置文件。

  FileSystemXmlApplicationContext:用于从文件系统或URL装载配置文件。

  XmlWebApplicationContext和XmlPortletApplicationContext:用于从网络装载配置文件。

如果只是简单的bean配置只需要声明如下头:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
</beans>

使用简写定义bean属性,必须在头重声明:

<?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:p="http://www.springframework.org/schema/p
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        <bean id="" class="" p:name=value p:name-ref=beanId/> </beans
>

map和Properties的区别:Properties的value只能是字符串。

如果用继承定义bean,子bean的集合可以设置Merge属性为true与父bean合并。

如果要是用构造器来初始化bean,可以在bean中使用标签指定构造参数。

<Constructor-arg type="" index="" value="" />

 

posted @ 2015-01-31 19:49  心灵的呼声  阅读(100)  评论(0编辑  收藏  举报