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 方式实现) 以转账为例 ...
随机推荐
- grep每次读取多大的文本
Most of the tools do not actually read a single line in from a file at a time, rather they use a buf ...
- Nginx启动报错误unlink() “nginx.pid” failed (2: No such file or directory)
在修改了配置文件之后,启动nginx的时候发现,进程已经存在了,但是端口没有监听到. 查看错误日志,有如下错误信息 2015/10/22 21:39:06 [alert] 26434#0: unlin ...
- [JZOJ3424] 【NOIP2013模拟】粉刷匠
题目 题目大意 有\(K\)种颜色的小球,每种颜色的小球有\(c_i\)个. 求相邻颜色不同的排列的方案数. \(K\leq 15\)且\(c_i\leq 6\) 思考历程&正解1 我是一个智 ...
- 「STL」bitset正传
前言 之前一些需要转二进制来解决的题目我看到很多大佬用了bitset. 然而我并不会这东西.看上去很高级的样子…… 改题改累了来学习一下233. 正文 一.bitset的构造 bitset有三种构造方 ...
- TopCoder[SRM513 DIV 1]:PerfectMemory(500)
Problem Statement You might have played the game called Memoria. In this game, there is a board ...
- python相关软件安装流程图解——虚拟机操作——复制虚拟机主机——CentOS-7-x86_64-DVD-1810
请先确保已经安装了虚拟机 python相关软件安装流程图解——虚拟机安装——CentOS-7-x86_64-DVD-1810——CentOS-01下载 https://www.cnblogs.com/ ...
- 框架前期准备篇之AutoFac常见用法总结 转载
框架前期准备篇之AutoFac常见用法总结 一. 说在前面的话 凡是大约工作在两年以上的朋友们,或多或少都会接触到一些框架搭建方面的知识,只要一谈到框架搭建这个问题或者最佳用法这个问题,势必会引起一点 ...
- angluar1.8.2 PC Mail项目笔记
兼容性技术选型 前后端分离 代理gulp nginx jq+angluar1.8.2 使用级别刚刚好的相对目录,方便转移项目或者做接口代理时的切换目录 指令过滤器服务控制器书写位置 方法封装,自己写和 ...
- 18.scrapy_maitian
ershoufang.py # -*- coding: utf-8 -*- import scrapy class ErshoufangSpider(scrapy.Spider): name = 'e ...
- USACO training course Mother's Milk /// DFS(有点意思) oj10120
题目大意: 输入 A B C 为三个容器的容量 一开始A B是空的 C是满的 每一次倾倒只能在 盛的容器满 或 倒的容器空 时才停止 输出当A容器空时 C容器内剩余量的所有可能值 Sample Inp ...