【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),以达到解耦的目的,实现模块的组件化.程序 ...
随机推荐
- C# Winform 未能加载文件或程序集"System.Data.SQLite"或它的某一个依赖项。试图加载格式不正确的程序
在使用Winform 开发了一个小软件,其中使用了SQLite作为数据库 但在我的Win7 64位系统上却出现了以下错误: System.BadImageFormatException: 未能加载文件 ...
- java多线程之间的顺序问题
java 多线程: 这样写有问题的:这样写可以的: package com.test; import java.util.concurrent.CountDownLatch; import java. ...
- Flask刷新问题
修改页面中内容,特别是图片后,总是刷新不了.调试时,我常常通过修改端口来解决,从80-99不断改. 服务器部署,也遇到同样问题,重启web服务器,重启计算机都不行,网页已经改过来了,但是图片还是老图片 ...
- hadoop hue切换中文版
搭建了Hue之后发现只有英文的界面,非常不开心,于是百度谷歌了一大堆也没有发现可靠的办法,就自己上手了一把,亲测可行. 英文版: 中文版: hue切换使用中文版的方法如下: 1.修改配置文件 vi / ...
- 树莓派中GPIO针角定义图
一.上图 二.上图
- 转:NGNIX模块开发——nginx的配置系统
From:http://tengine.taobao.org/book/chapter_02.html nginx的配置系统 nginx的配置系统由一个主配置文件和其他一些辅助的配置文件构成.这些配置 ...
- java推断字符串中是否包括字母
1.java代码推断字符串中是否包括字母: 思路:使用正則表達式的来验证 1.1演示样例代码例如以下: /** * 该方法主要使用正則表達式来推断字符串中是否包括字母 * @author fengga ...
- 算法笔记_204:第四届蓝桥杯软件类决赛真题(Java语言C组)
目录 1 好好学习 2 埃及分数 3 金蝉素数 4 横向打印二叉树 5 危险系数 6 公式求值 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 好好学习 汤姆跟爷爷来中国旅游.一天,他帮助中国的 ...
- BFC特性 形成BFC
1.示例代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- Java多线程之ReentrantLock重入锁简介与使用教程
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6543947.html 我们知道,线程安全问题需要通过线程之间的同步来解决,而同步大多使用syncrhoize ...