【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),以达到解耦的目的,实现模块的组件化.程序 ...
随机推荐
- ubuntu16.04下部署tomcat9和java8启动一次需要七八分钟
一.环境如下 Ubuntu16.04 +tomcat9+openjdk1.8 二.问题 在tomcat的bin下执行./startup.sh 如下图没有问题 root@bogon:/usr/apac ...
- 鼠标上下滚动支持combobox选中
首先需要jquery插件来支持: 1.代码SVN检出https://github.com/jquery/jquery-mousewheel 2.点击这里下载jquery.mousewheel.zip ...
- 一起talk C栗子吧(第一百三十三回:C语言实例--创建进程时的内存细节)
各位看官们.大家好,上一回中咱们说的是从内存角度看进程和线程的样例.这一回咱们说的样例是:创建进程时的内存细节.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们.我们都知道使用fork函数能 ...
- Android NDK开发篇(六):Java与原生代码通信(异常处理)
一.捕获异常 异常处理是Java中的功能.在Android中使用SDK进行开发的时候常常要用到.Android原生代码在运行过程中假设遇到错误,须要检測,并抛出异常给Java层.运行原生代码出现了问题 ...
- 在线创建springboot项目
在线创建srpingboot项目的网址:https://start.spring.io/ 我只选了一个web,然后开始生成:
- Android 之 SharedPreferences应用
Android 平台给我们提供了一个 SharedPreferences 类,它是一个轻量级的存储类,特别适合用于保存共享数据.使用SharedPreferences保存数据,其背后是用xml文件存放 ...
- 低危漏洞- X-Frame-Options Header未配置
原文链接:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/X-Frame-Options?redirectlocale=en-US&redi ...
- angularjs图片上传和预览 base64
angularjs图片上传和预览 思路是从file中读取base64 Module.controller('controlName', ['$scope', '$http', function($sc ...
- nginx获得response自定义的header
Response header send by upstream is $upstream_http_x_uuid http://wiki.nginx.org/HttpUpstreamModule#. ...
- 【转】Android Studio开发应用桌面出现两个或多个图标
原文链接:http://blog.csdn.net/jia635/article/details/78259699 解决办法: 查找的是不是自己的AndroidManifest中 多个Activity ...