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>
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>
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 使用jQuery.wechat构建微信WEB应用
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- Java利用Sping框架编写RPC远程过程调用服务的教程
- springmvc 发送ajax出现中文乱码的解决方法汇总
- Spring MVC中Ajax实现二级联动的简单实例
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- 详解Java的MyBatis框架和Spring框架的整合运用