Spring基本功能-依赖注入
一、Spring的依赖注入(DI)
1.1 xml形式注入
(1)普通变量的注入
//普通变量的注入,xml配置property,实体类配置set方法注入
<bean id="person" class="com.jyk.spring.simpletest.Person">
<property name="id" value="1"></property>
<property name="name" value="tom"></property>
</bean> public class Person { public String id;
public String name;
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
} public void printInfo(){
System.out.println("id=="+id);
System.out.println("name=="+name);
}
}
(2)对象的引用注入,该种形式的xml配置灵活,建议使用内部bean的配置形式
//多对象的引用注入,同样可采用常规的xml配置,对象中配置set方法来实现注入
<bean id="dao" class="com.jyk.spring.simpletest.Dao">
</bean> <bean id="service" class="com.jyk.spring.simpletest.Service">
<property name="dao" ref="dao"></property>
</bean> <bean id="action" class="com.jyk.spring.simpletest.Action">
<property name="service" ref="service"></property>
</bean> //内部bean的配置形式 <bean id="action" class="com.jyk.spring.simpletest.Action">
<property name="service">
<bean class="com.jyk.spring.simpletest.Service">
<property name="dao">
<bean class="com.jyk.spring.simpletest.Dao"></bean>
</property>
</bean>
</property>
</bean>
public class Action {
private Service service;
public void setService(Service service) {
this.service = service;
}
public void execute(){
service.execute();
}
}
public class Service {
private Dao dao;
public void setDao(Dao dao) {
this.dao = dao;
}
public void execute(){
dao.execute();
}
}
public class Dao {
public void execute(){
System.out.println("dao执行逻辑");
}
}
(3)p 名称空间给对象的属性注入值(该功能仅Spring3.0以上支持)
<bean id="person" class="com.jyk.spring.simpletest.Person" p:id="1" p:name="cat"></bean>
<bean id="dao" class="com.jyk.spring.simpletest.Dao"></bean> <bean id="service" class="com.jyk.spring.simpletest.Service" p:dao-ref="dao"></bean> <bean id="action" class="com.jyk.spring.simpletest.Action" p:service-ref="service"></bean>
(4)装配list,set,map,properties集合
<bean id="person" class="com.jyk.spring.simpletest.Person">
</bean> <bean id="person1" class="com.jyk.spring.simpletest.Person1">
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="person"/>
</list>
</property>
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="person"/>
</set>
</property>
<property name="map">
<map>
<entry key="key1">
<value>map1</value>
</entry>
<entry key="key2">
<value>map2</value>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="01">prop1</prop>
<prop key="02">prop2</prop>
</props>
</property>
</bean> public class Person1 { private List list;
private Map map;
private Set set;
private Properties properties;
public void setList(List list) {
this.list = list;
}
public void setMap(Map map) {
this.map = map;
}
public void setSet(Set set) {
this.set = set;
}
public void setProperties(Properties properties) {
this.properties = properties;
} public void printInfo(){
System.out.println("list=="+list);
System.out.println("map=="+map);
System.out.println("set=="+set);
System.out.println("properties=="+properties);
}
}
1.2 自动装配
什么是自动装配?就是对属性,方法参数,引用的其他bean,无需通过property的形式来配置,而是通过一定的自动装配策略,交由Spring进行自动适配和装载。强烈建议使用自动装配,因为扩展性高,代码可维护性强,比如在java代码中新增一个属性或引用,只需添加set方法,以及配置文件中配置对应的bean即可。而且自动装配也可以显著减少bean配置的繁琐过程。
(1)byName 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。
public class Action {
private Service service;
public void setService(Service service) {
this.service = service;
}
public void execute(){
service.execute();
}
}
public class Service {
private Dao dao;
public void setDao(Dao dao) {
this.dao = dao;
}
public void execute(){
dao.execute();
}
}
public class Dao {
public void execute(){
System.out.println("dao执行逻辑");
}
}
<bean id="dao" class="com.jyk.spring.simpletest.Dao"></bean>
<bean id="service" class="com.jyk.spring.simpletest.Service" autowire="byName"></bean>
<bean id="action" class="com.jyk.spring.simpletest.Action" autowire="byName"></bean>
(2)byType 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如没找到匹配的bean需要抛出异常,则可以通过设置通过设置
dependency-check="objects"来使Spring抛出异常。
<bean id="dao" class="com.jyk.spring.simpletest.Dao"></bean> <bean id="service" class="com.jyk.spring.simpletest.Service" autowire="byType"></bean> <bean id="action" class="com.jyk.spring.simpletest.Action" autowire="byType"></bean>
(3)constructor 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。并且 constructor会优先查找参数比较多的构造函数
(4) autodetect 通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式
1.3 注解形式注入
Spring提供了一系列注解,通过在Java类中添加各种注解,同样可以达到依赖注入的效果。
@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。
@Resource注解默认按名称装配,如果没有指定name属性,并且按照默认的名称找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。
区别:@Resource注解和@Autowired一样,都可以标注在字段或属性的setter方法上,不同的是当@Resource注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当@Resource注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。而@Autowired则是以bean类型为基准。
@Qualifier注解按名称装配。
@PostConstruct 指定Bean的初始化方法
@PreDestroy 指定Bean的销毁方法
Spring基本功能-依赖注入的更多相关文章
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转
Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...
- Spring学习(一)——Spring中的依赖注入简介【转】
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- JavaEE开发之Spring中的依赖注入与AOP
上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...
- JavaEE开发之Spring中的依赖注入与AOP编程
上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...
- Spring学习(一)——Spring中的依赖注入简介
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring Boot2(007):关于Spring beans、依赖注入 和 @SpringBootApplication 注解
一.关于Spring beans 和 依赖注入(Dependency Injection) spring boot 和 Spring 全家桶无缝衔接,开发过程中可以很轻松地使用 Spring 全家桶的 ...
- 谈谈自己了解的spring.NET的依赖注入
spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...
- Spring学习(三)——Spring中的依赖注入的方式
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- spring六种种依赖注入方式
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...
随机推荐
- ci框架简单出现的错误[Undefined property: MContacts::$db]
出现这样的错误时说明自己忘记加载数据库了, application/config/aotuload.php $autoload['libraries'] = array('database') ...
- 数据库 数据库SQL语句四
多表查询 等值连接 --查询员工信息,员工号,姓名,月薪,部门名称 select e.empno,e.ename,d.dname from emp e,dept d where e.deptno=d. ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- datatable详解(angular-datatable)+后台分页(springmvc)
datable常规配置,百度一大堆 function prepareDatatable(selector, options) { var defaultOptions = { autoWidth: t ...
- JQuery------图片幻灯片插件
下载地址: http://www.jq22.com/jquery-info36
- 部分常用dos命令
Microsoft Windows XP [版本 ] (C) 版权所有 - Microsoft Corp. C:\Documents and Settings\Administrator>d: ...
- Cocos2d-x学习之 整体框架描述
1.Cocos2d-x 整体描述 和传统的游戏引擎一样,cocos2d-x作为一个2d的游戏引擎,其也有以下几大概念组成: 导演(CCDiretor): 在cocos2d-x引擎中,CCDirecto ...
- HDU2604—Queuing
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 题目意思:n个人排队,f表示女,m表示男,包含子串‘fmf’和‘fff’的序列为O队列,否则为E ...
- input即时————模糊匹配(纯html+jquery简单实现)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- ArcGIS Data Store 初体验
1. 什么是ArcGIS Data Store? ArcGIS Data Store 是10.3新推出的一个组件,从官方对其定位来说,该组件是用来优化发布托管服务到portal for ArcGIS的 ...