您的位置:首页 > 编程语言 > Java开发

Spring4学习笔记----Spring的依赖注入详解

2016-07-27 20:47 621 查看
Spring是一个IOC和AOP容器,Spring是非侵入型的,基于Spring开发的web应用中的对象可以不依赖Spring的API.

IOC:依赖注入 (让Spring Bean以配置文件的形式组织在一起)

所谓依赖注入:在程序运行过程中,如需要另外一个对象协作(调用他的属性、方法),无需在代码中创建该被调对象,而是依赖外部容器的注入。

一、Spring容器

(1)

Spring容器是Bean实例的工厂,并管理容器中的Bean。

Spring容器有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口。它们都可以代表Spring容器。

Bean是Spring容器的基本单位,在基于Spring的JavaEE应用中,所有的组件都被当成Bean处理,任何的java对象、java组件都被当成Bean处理,这些组件并不是标准的javabean.

(2)

BeanFactory负责配置、创建、管理Bean,还负责管理Bean与Bean之间的依赖关系。

ApplicationContextBeanFactory的子接口,作为Spring容器更方便。

二、Spring的核心机制:依赖注入

   依赖注入是目前最优秀的解耦方式,依赖注入让Spring的Bean以配置文件组织在一起,而不是以编码的方式耦合在一起。

   所谓依赖注入:在程序运行过程中,如需要另外一个对象协作(调用他的属性、方法),无需在代码中创建该被调对象,而是依赖外部容器的注入。

依赖注入:在Spring的xml配置文件中

Id:指定Bean的唯一标识,程序通过id属性值来访问Bean实例

class:指定Bean的实现类

二、依赖注入方式:

(1)属性注入

        <bean id="xx" class="">

<property name=""></property>

</bean>

 

(2)构造器注入

     构造函数注入;(通过类型;通过索引;联合使用)

<bean id="xx" class="">

<constructor-arg type="" value=""></constructor-arg>

</bean>

<bean id="xx" class="">

<constructor-arg index="" value=""></constructor-arg>

</bean>

 

(3)工厂方法注入;(非静态工厂,静态工厂)

非静态工厂:

<!-- 装配工厂bean -->

<bean id="serviceFactory" class="xx.xxx.ServiceFactory"></bean>

<!-- 工厂createXxx()
方法返回 xxxService
对象 -->

<bean id="xxxService" factory-bean="serviceFactory" factory-method="createXxx" >

</bean>

静态工厂:

<!-- 工厂createXxx()
方法返回 xxxService
对象 -->

<bean id="xxxService" class="xx.xxx.ServiceFactory" factory-method="createXxx" >

</bean>

 

(4)泛型依赖注入;

三:注入参数

(1)注入基本类型:

<bean id="xx" class="">

<property name=""></property>

</bean>

 

(2)注入bean(注入的属性为引用类型使用ref)

<bean id="people1" class="com.bean.People">

<property name="name" value="wendy"></property>

<property name=" idCard " ref=" idCard 1"></property>

</bean>

<bean id="idCard1" class="com.bean. IDCard "></bean>

(3)内部bean(即该bean对象只属于父类bean)

当 Bean 实例仅仅给一个特定的属性使用时,
可以将其声明为内部 Bean.
内部 Bean
声明直接包含在 <property>
或 <constructor-arg>
元素里,
不需要设置任何 id
或 name
属性,内部
Bean 不能使用在任何其他地方

 

<bean id=" idCard 2" class="com.bean. IDCard  ">

<property name="name" value="wendy"></property>

<property name=" idCard ">

<bean class="com.bean. IDCard ">

<property name="" value=""></property>

</bean>

</property>

</bean>

 

(4)null值

可以使用专用的 <null/> 元素标签为
Bean 的字符串或其它对象类型的属性注入
null 值

 

<bean id="people2" class="com.bean.People">

<property name="name" value="tom"></property>

<property name=" idCard ">

<null></null>

</property>

</bean>

 

(5)级联属性(注入属性的属性)

<bean id="people3" class="com.bean.People">

<property name="name" value="李四"></property>

<!-- 前提是People中的IDCard对象已经创建
-->

<property name="idCard.number" value=""></property>

<property name="idCard.address" value=""></property>

</bean>

 

(6)集合类型属性(注入的属性有多个值,用list)

<bean id="people4" class="com.bean.People">

<property name="name" value="张三"></property>

<property name="hobbies">

<list>

<value>唱歌</value>

<value>跳舞</value>

</list>

</property>

</bean>

(注入的属性为key-value形式)

<bean id="people5" class="com.bean.People">

<property name="pname" value="张三"></property>

<property name="works">

<map>

<entry>

<key><value>上午</value></key>

<value>写代码</value>

</entry>

<entry>

<key><value>下午</value></key>

<value>看电影</value>

</entry>

</map>

</property>

</bean>

Properties属性

<bean id="people6" class="com.bean.People">

<property name="name" value="张三"></property>

<property name="properties">

<props>

<prop key=""></prop>

<prop key=""></prop>

</props>

</property>

</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring java ee web应用 ioc