2.Spring【DI】XML方式
依赖:
在A类中引用了B类,说明A依赖于B。
注入:
使用Spring框架给A类中的B对象的属性赋值。
直接上代码:
1.只使用IOC
public class Person {
private String name;
private int age;
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
<bean id="person" class="com.spring.demo1.Person"/>
/**
* 只使用IOC
*/
@Test
public void m01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person p = (Person) ac.getBean("person");
p.setAge(10);
p.setName("10岁的boy");
System.out.println(p);
}

2.使用IOC+DI
<bean id="person" class="com.spring.demo1.Person">
<property name="name" value="30岁的man"/>
<property name="age" value="30"/>
</bean>
/**
* 使用IOC+DI
*/
@Test
public void m02(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person p = (Person) ac.getBean("person");
System.out.println(p);
}

两种注入方式
1.通过setter方法注入------开发常用
上述的依赖注入方法↑↑↑↑↑↑↑↑↑
在编写的JAVA类中加入属性的set方法
配置文件:(property)
<bean id="person" class="com.spring.demo1.Person">
<property name="name" value="30岁的man"/>
<property name="age" value="30"/>
</bean>
2.通过构造方法注入
在编写的JAVA类中加入有参构造方法
配置文件:(constructor-arg)
<bean id="person" class="com.spring.demo1.Person">
<constructor-arg name="name" value="40岁的person"/>
<constructor-arg name="age" value="40"/>
</bean>
注意:
如果属性是另一个JAVA类,应该将 value属性 改为 ref
<bean id="car" class="com.spring.Car"/>
<bean id="person" class="com.spring.Person">
<property name="name" value="man"/>
<property name="car" ref="car"/>
</bean>
数组,集合(List,Set,Map),Properties等的注入
1. 如果是数组或者List集合,注入配置文件的方式是一样的
<bean id="collectionBean" class="com.spring.CollectionBean">
<property name="arrs">
<list>
<value>呵呵</value>
<value>哈哈</value>
</list>
</property>
</bean>
2. 如果是Set集合,注入的配置文件方式如下:
<property name="sets">
<set>
<value>哈哈</value>
<value>呵呵</value>
</set>
</property>
3. 如果是Map集合,注入的配置方式如下:
<property name="map">
<map>
<entry key="老王1" value="38"/>
<entry key="老王2" value="38"/>
<entry key="老王3" value="29"/>
</map>
</property>
4. 如果是properties属性文件的方式,注入的配置如下:
<property name="pro">
<props>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
2.Spring【DI】XML方式的更多相关文章
- Spring基于XML方式的使用
一.IoC配置 IoC的配置是通过Spring的xml文件的bean标签进行的. 1.bean标签介绍 bean标签一般是在xml文件进行配置的,xml文件一般样式如下: <?xml versi ...
- 【Spring】XML方式实现(无参构造 有参构造)和注解方式实现 IoC
文章目录 Spring IoC的实现方式 XML方式实现 通过无参构造方法来创建 1.编写一个User实体类 2.编写我们的spring文件 3.测试类 UserTest.java 4.测试结果 通过 ...
- Spring通过XML方式实现定时任务
package com.wisezone.service; import java.text.SimpleDateFormat; import java.util.Date; import org.s ...
- Spring基于XML方式加载Bean定义信息(又名:Spring IOC源码时序图)-图解
- Spring 简单使用IoC与DI——XML配置
目录 Spring简介 导入jar包 Spring配置文件 Spring的IoC IoC简介 快速使用IoC Spring创建对象的三种方式 使用构造方法 使用实例工厂 使用静态静态工厂 Spring ...
- Spring总结四:IOC和DI 注解方式
首先我们要了解注解和xml配置的区别: 作用一样,但是注解写在Bean的上方来代替我们之前在xml文件中所做的bean配置,也就是说我们使用了注解的方式,就不用再xml里面进行配置了,相对来说注解方式 ...
- spring aop 使用xml方式的简单总结
spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...
- 使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”
使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”. 原 ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- Spring声明式事务管理(基于XML方式实现)
--------------------siwuxie095 Spring 声明式事务管理(基于 XML 方式实现) 以转账为例 ...
随机推荐
- 最大流——poj1459
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...
- 多源多汇费用流——poj2516
网络流的题好难..感觉有点遭不住了 这题用矩阵存图,然后把k个物品,每个物品都求一次费用流 /* 多源多汇的费用流 其实是k个费用流 */ #include<bits/stdc++.h> ...
- 蒙特卡罗定位(Particle Filter Localization)
1. 蒙特卡罗定位 定位:机器人知道地图信息的情况下如何利用传感器信息确定自己的位置(Localization). 有人会说,定位是不需要地图信息的.机器人知道初始位置,知道左右轮的速度,就可以算出在 ...
- UpdateLayeredWindow与SetLayeredWindowAttributes
首先使用透明之前必须设置该窗口为层级窗口,即增加窗口的扩展风格WS_EX_LAYERED,增加的时候最好使用GetWindowlong获取Ex风格,然后加入后在SetWindowLong设置,最好不适 ...
- springboot跨域问题解决
package com.qif.xdqdm.config; import org.springframework.context.annotation.Bean; import org.springf ...
- Grunt入门
Grunt 新手一日入门 2014.06.20 前端相关 TOC 1. 用途和使用场景 2. 开发一个任务自动处理器 3. 开始学习 Grunt 3.1. 安装 Grunt 3.2. 生成 packa ...
- VS2010-MFC(工具栏:工具栏资源及CToolBar类)
转自:http://www.jizhuomi.com/software/215.html 上一节讲了菜单及CMenu类的使用,这一节讲与菜单有密切联系的工具栏. 工具栏简介 工具栏一般位于主框架窗口的 ...
- 通过apiservice反向代理访问service
第一种:NodePort类型 type: NodePort ports: - port: 80 targetPort: 80 nodePort: 30008 第二种:ClusterIP类型 typ ...
- <每日一题>题目3:编写装饰器,为多个函数加上记录调用功能,要求每次调用函数都将被调用的函数名称写入文件
def log(func): def inner(*args,**kwargs): with open('log',mode='a',encoding='utf-8') as f: #以追加的方式打开 ...
- 论文翻译——Fast-R-CNN(端到端开篇, End to end)
快速的区域卷积网络方法(Fast R-CNN) 论文地址:https://arxiv.org/abs/1504.08083 摘要: 本文提出一种基于快速的区域卷积网络方法(Fast R-CNN) ...