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提出了依赖注入的思想,即依赖类不由程 ...
随机推荐
- 使用css全面美化input标签
做网站时经常有这样那样的需要,要美化input ,于是CSS的美化必不可少.和程序人生的站长交流,他发给我这个. 下面是CSS样式 input { border:1px solid #B3D6EF; ...
- 【BZOJ】1649: [Usaco2006 Dec]Cow Roller Coaster(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1649 又是题解... 设f[i][j]表示费用i长度j得到的最大乐趣 f[i][end[a]]=ma ...
- PyQt的QString 和 QStringList
在Qt的C++实现中的QString 和 QStringList 在Python的实现中等效替换为 "str1" 和 ["str1","str2&qu ...
- commit命令
git commit -m "测试提交"
- 经典SQL面试题(转)
http://www.cnblogs.com/kcher90/archive/2013/03/13/2957932.html 有三个表,如果学生缺考,那么在成绩表中就不存在这个学生的这门课程成绩的记录 ...
- ThreadLocal并不是一个Thread
ThreadLocal是什么? 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁 ...
- 从远程(包括ftp,http等协议)地址获取文件流信息
URL url = new URL("ftp://172.18.251.155:8010/recordsImg/2019-01-28/000008_1548649813267.jpg&quo ...
- post 与get
GET:从服务器上获取数据,也就是所谓的查,仅仅是获取服务器资源,不进行修改. POST:向服务器提交数据,这就涉及到了数据的更新,也就是更改服务器的数据. 补充: PUT:PUT的英文含义是放置,也 ...
- docker的本地仓库换成阿里云的镜像仓库
1,阿里云上注册账号,我的已经注册好了,仓库名称:registry.cn-hangzhou.aliyuncs.com/woccb2/chen 2,本地安装docker: yum -y install ...
- github 's usage
author:headsen chen date: 2018-05-30 10:50:56 notice:This article is created by headsen chen him ...