Spring4学习笔记----Spring的依赖注入详解
来源:互联网 发布:怎样在淘宝网卖闲置品 编辑:程序博客网 时间:2024/05/29 13:25
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>
- Spring4学习笔记----Spring的依赖注入详解
- spring学习笔记(4)依赖注入详解
- spring学习笔记 依赖注入详解
- Spring4学习笔记-泛型依赖注入
- J2EE系列之Spring4学习笔记(三)--IOC详解(依赖注入)
- Spring 学习笔记 ----依赖注入
- [学习笔记]Spring依赖注入
- Spring学习笔记 依赖注入
- 【Spring学习笔记】依赖注入
- Spring4第三讲学习笔记,依赖注入dependency injection
- Spring4学习笔记(三):依赖注入的方式与细节
- spring 依赖注入的学习
- Spring4.3入门 Spring 依赖注入
- Spring学习笔记(5)----依赖注入的简单实现
- Spring学习笔记----依赖注入的简单实现
- Spring学习笔记(5)----依赖注入的简单实现
- Javaweb学习笔记之Spring的依赖注入方式
- 【Spring学习笔记三】-依赖注入的两种方式
- Leetcode 377. Combination Sum IV
- POJ-3258 River Hopscotch
- input标签file 多文件上传
- Pie<poj3122>
- Google hacker工具使用和Google hacker防范
- Spring4学习笔记----Spring的依赖注入详解
- HTML5 Video标签的属性、方法和事件汇总介绍
- 完全弄懂JavaScript中的this
- nginx 菜鸟入门一
- 5.5 编写一个程序, 对于二维数组 b[2][3]={{1,2,3},{4,5,6}}, 验证*(*(b+i)+j)、 *(b[i]+j)、 (*(b+i))[j]与 b[i][j]是相同的。
- hdu 4786 Fibonacci Tree 思路
- 数据基本类型和Integer、String之间的转换
- Codeforces673C Bear and Colors(枚举区间)
- Java Web访问Linux的Mysql