【Spring】Spring之依赖注入(DI)传递参数的方式
DI依赖注入传入参数的方式,这里介绍了基本数据类型,集合,符合数据类型的传递(String类型比较特殊,其传递值和基本数据类型的方法一致)
注入基本数据类型和String类型
通过setter方法注入基本数据类型与String
案例:
<bean id="book" class="cn.xdl.bean.Book">
<!--
对于基本数据类型 与 String的注入操作 通过set方法 ,完成注入
name: 属性的名称
value: 属性要赋的值
<null/>: 节点表示null。
-->
<property name="bookName" value="西游记"></property>
<property name="bookId" value="10001"></property>
<!-- null值 -->
<property name="x">
<null/>
</property> </bean>
通过构造方法 ,注入基本数据类型与String
方式1:
<bean id="book" class="cn.xdl.bean.Book">
<!-- 通过构造器 传入形参名称 与 value值, 进行参数的注入-->
<constructor-arg name="bn" value="红楼梦"></constructor-arg>
<constructor-arg name="bi" value="10002"></constructor-arg>
</bean>
方式2:
<bean id="book" class="cn.xdl.bean.Book">
<!-- 通过构造器 传入形参列表的索引 ,与value进行参数的注入-->
<constructor-arg index="0">
<null/>
</constructor-arg>
<constructor-arg index="1" value="10003"></constructor-arg>
</bean>
注入集合类型的数据
注入List集合
<property name="v">
<!--向List集合 注入值-->
<list>
<!--每一个value节点, 表示集合中的一个元素-->
<value>A</value>
<value>B</value>
<value>C</value>
<value>D</value>
<value>E</value>
<value>F</value>
</list>
</property>
注入Set集合
<property name="v">
<!--向set集合 注入值-->
<set>
<!--每一个value节点, 表示集合中的一个元素-->
<value>A</value>
<value>B</value>
<value>C</value>
<value>D</value>
<value>E</value>
<value>F</value>
</set>
</property>
注入Map集合
<property name="v">
<map>
<!--每一个enrty表示集合中的一个键值对-->
<entry key="jiyou1" value="map1"></entry>
<entry key="jiyou2" value="map2"></entry>
</map>
</property>
注入Properties集合
<property name="v">
<!--对于properties的注入 ,value是编写在prop节点的文本内容区域的
每一个prop都表示proerties存储的一个键值对的数据-->
<props>
<prop key="v1">A</prop>
<prop key="v2">B</prop>
<prop key="v3">C</prop>
</props>
</property>
通过引入的方式, 实现集合的重复利用
原理:
1. 配置一个集合对象到容器中, 并指定ID
2. 在其他对象使用时, 直接通过对象注入的方式, 将其注入即可
案例:
向容器中添加集合对象:
<util:set id="list">
<value>张三</value>
<value>李四</value>
<value>王二</value>
<value>麻子</value>
</util:set>
将对象注入其他对象中:
<bean id="person" class="cn.xdl.bean.Person">
<property name="refriendship" ref="list"></property>
</bean>
对象的注入
通过Setter方法设置属性
property节点: 通过set方法, 设置一个属性的值
name属性: 表示的是 当前类中的属性的名称
ref: 表示的是, 本次要注入的对象在当前容器中的id值
案例:
<bean id="bookinstance" class="BookClass">
<property name="book" ref="bookinstance"></property>
</bean>
通过构造器设置属性
constructor节点: 通过构造器 , 注入属性的值
name属性: 这是一个容易混淆的地方 , 它指的其实是构造方法中形式参数列表中参数的名称
ref: 表示的是, 本次要注入的对象在当前容器中的id值
案例:
<bean id="bookinstance" class="BookClass">
<constructor-arg name="book" ref="bookinstance"></constructor-arg>
</bean>
对象的自动装配
我们可以在bean节点中, 添加autowire属性来完成自动装配的操作
属性取值范围:
1,no : 默认设置 , 表示关闭自动装配
2,byName : 通过名称完成自动装配:
例如: 当前我们容器中存在一个对象, id为book;
而刚好当前的这个person类中有一个名称为book的属性
那么这个时候, 我们的容器, 会自动完成两个对象的关联
byName 不会判断传入的参数类型是否匹配, 只要名称相同, 就会强行建立依赖关系 , 导致报错!
3, byType : 通过类型完成自动装配
例如: 当前我们容器中存在一个Book类的对象
而刚好我们当前的Person有一个Book类型的属性
我们的容器, 就会自动完成两个对象的关联
4,constructor 与byType的方式类似,不同之处在于它应用于构造器参数
5,autodetect 通过bean类来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式
案例:
Person类:
package cn.xdl.bean;
public class Person {
private String name;
private int age;
private Book book;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return "这个人叫做:" + name + ", 今年" + age + "岁了, 他有一本书:" + book ;
}
public Person(String name, int age, Book book) {
super();
this.name = name;
this.age = age;
this.book = book;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(Book book) {
super();
this.book = book;
}
}
Person.java
applicationContext文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <bean id="book" class="cn.xdl.bean.Book">
<property name="bookName" value="水浒传"></property>
<property name="bookId" value="10001"></property>
</bean>
<!--
<bean id="person" class="cn.xdl.bean.Person">
通过ref 关联对象之间的关系
<constructor-arg name="book">
<null/>
</constructor-arg>
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<property name="book" ref="book"></property>
</bean>
-->
<!-- 自动装配 byName 通过属性名称 与 容器中对象的id 匹配 -->
<bean id="person" class="cn.xdl.bean.Person" autowire="byName">
<property name="name" value="小李"></property>
<property name="age" value="18"></property>
</bean>
</beans>
applicationContext.xml
Test测试类:
package cn.xdl.test; import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.xdl.bean.Person;
import cn.xdl.test.util.ContextUtil; public class Test { @Test
public void test() throws Exception {
ClassPathXmlApplicationContext context = ContextUtil.getC("bean6.xml");
Person p= context.getBean("person", Person.class);
System.out.println(p);//这个人叫做:小李, 今年23岁了, 他有一本书:书名=水浒传, 编号=10001 }
}
Test.java
扫描组件:
扫描组件后,默认id值为类名首字母小写,也可以自定义id。
下面是一些常用的@式,@后面的式可以写值,也可以不写,还有就是@Value(value="abc"),可以简写为@Value("abc")。
@Compenent 通用注解
@Repository 持久层组件注解
@Service 业务层组件注解
@Controller 控制层组件处理
@Value 可以注入指定变量的值
@Scope 可以指定对象的作用域singleton(单例模式,默认)、prototype(多例模式)、request、session、global Session,
@Transactional 表示事务
Spring表达式
这种表达式在语法上和EL非常像,可以读取一个bean对象/集合中的数据
例如:
<!-- 进行扫描cn包 -->
<context:component-scan base-package="cn"></context:component-scan> <!--从db.propertise文件中读取配置信息-->
<util:properties id="db" location="classpath:db.properties"/> <bean id="demo"class="com.xdl.bean.OracleDataSource">
<!--读取name的值-->
<property name=“username" value="#{db.name}"/>
<!--读取password-->
<property name=“password"value="#{db.password}"/>
<!--读取url的值-->
<property name=“url" value="#{db.url}"/>
<bean>
【Spring】Spring之依赖注入(DI)传递参数的方式的更多相关文章
- 【Spring IoC】依赖注入DI(四)
平常的Java开发中,程序员在某个类中需要依赖其它类的方法.通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由程 ...
- Spring(2):依赖注入DI
依赖注入DI 当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在 传统的程序设计过程中,通常由调用者来创建被调用者的实例.但在Spring里,创建被 ...
- spring学习之依赖注入DI与控制反转IOC
一 Ioc基础 1.什么是Ioc? Ioc(Inversion of Control)既控制反转,Ioc不是一种技术,而是一种思想,在Java开发中意味着将设计好的对象交给容器来进行控制,并不是像传统 ...
- Spring 依赖注入(DI) 的三种方式 和 对集合类型的注入
// 分别省略了getter setter public class Student { private String name; private int age; private Teacher t ...
- spring中的依赖注入(DI)笔记
使用xml bean依赖注入有set注入和构造器注入 set注入用的比较多 <bean id="a" class="com.A"> <prop ...
- Spring框架学习笔记(1)——控制反转IOC与依赖注入DI
Spring框架的主要作用,就是提供了一个容器,使用该容器就可以创建并管理对象.比如说Dao类等,又或者是具有多依赖关系的类(Student类中包含有Teacher类的成员变量) Spring有两个核 ...
- spring(3)------控制反转(IOC)/依赖注入(DI)
一.spring核心概念理解 控制反转: 控制反转即IoC (Inversion of Control).它把传统上由程序代码直接操控的对象的调用权交给容器.通过容器来实现对象组件的装配和管理. 所谓 ...
- 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配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...
- 谈谈自己了解的spring.NET的依赖注入
spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...
随机推荐
- CMake can't find GLEW
Q: I'm on Windows and there is a FindGLEW.cmake file in my CMake modules folder, presumably put th ...
- 如何用Client OM获取页面上一个Content web part的内容
[解决方法] According to Wictor Wilén, The Client Object Model is fairly limited when it comes to working ...
- css 下边框
float: left; width: 1200px; height: 42px; background-color: #fff0; /* background-color: #4f4aff; */ ...
- C++ 函数适配器
1.考虑下面的需求,在一个int的vector中,找出一个比5的元素,容易想到的解决办法,定义一个方法对象,使用模板,如下:vector<int>::iterator iter = fin ...
- 关于ngModelOptions用法总结 让校验不过的验证绑定ngModel
updataOn 指定ng-model以什么绑定事件触发 default 就是默认的大家都知道blur 失去焦点的时候更新mouseover 鼠标滑过....... <input type=&q ...
- postman添加权限验证
Basic Auth 输入用户名和密码,点击 Update Request 生成 authorization header 一种身份验证 分类: postman学习笔记
- nginx做负载均衡,验证码总是不对(2台服务器)
问题原因:页面中有错误链接,导致跳转到另一台服务器,验证码显示的确是刚开始访问的 (有的静态文件一边没有,就需要去另外一边找,然后就跳了.) 解决方法:把静态文件都提取到了nginx上就解决了.
- JDK5.0 特性线程 同步装置之CountDownLatch 同步装置之CyclicBarrier 线程 BlockingQueue
来自:http://www.cnblogs.com/taven/category/475298.html import java.util.concurrent.CountDownLatch; imp ...
- Java I/O 操作及优化建议
Java I/O I/O,即 Input/Output(输入/输出) 的简称.就 I/O 而言.概念上有 5 种模型:blocking I/O.nonblocking I/O,I/O multiple ...
- 透彻理解Ioc
引述:IoC(控制反转:Inverse of Control)是Spring容器的内核,AOP.声明式事务等功能在此基础上开花结果.但是IoC这个重要的概念却比较晦涩隐讳,不容易让人望文生义,这不能不 ...