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 方式实现) 以转账为例 ...
随机推荐
- thinkphp 用户注册功能
UserActiion.class.php页面: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...
- SpringBoot 非web项目简单架构
1.截图 2.DemoService package com.github.weiwei02.springcloudtaskdemo; import org.springframework.beans ...
- leetcode-17-电话号码的字母组合’
题目描述: 方法一:回溯 class Solution: def letterCombinations(self, digits): """ :type digits: ...
- 挂载U盘
.fdisk -l 查看当前系统存储盘 (sdaX一般是系统自带, sdbX则是外接) .mount /dev/sdbX /mnt/usb/ (如果usb目录不存在可创建新目录) .umount /m ...
- 一图读懂POLARDB Box数据库一体机的云原生力量!
2019杭州云栖大会上,阿里云宣布正式推出高性能数据库一体机——POLARDB Box,用户部署在自有数据中心即可享受云数据库的便捷体验,同时还为Oracle等传统数据库用户提供一键迁移功能,最多节省 ...
- 2428: [HAOI2006]均分数据
模拟退火.一种十分玄学的随机算法,网上可以查到比较详细的资料. 先随机地把数分成m组,每次随机地选择一个数,一开始直接选最小的一组,后来就随机一组,把这个数换到该组看看答案能不能变小,如果变小则换,如 ...
- skyline中大数据量的三维场景刷新速度问题
我们做了一个的类似于TE Pro的桌面系统来代替TE Pro演示我们的大三维场景.我们的三维场景包括100平方公里的全要素场景,有建筑物,地面.小品.部件.植被等.在系统运行后,三维场景刷不起来,速 ...
- 常用终止python程序方法
方法1:采用sys.exit(0)正常终止程序,从图中可以看到,程序终止后shell运行不受影响. 方法2:采用os._exit(0)关闭整个shell,从图中看到,调用sys._exit(0)后整个 ...
- springboot-配置多数据源之番外篇(分包实现)
场景: 随着业务发展,系统连接多数据库成为常态,继前面AOP的实现方式之后,这里记录一下分包实现的方式. 实现: 1.pom.xml <?xml version="1.0" ...
- Linux开机、重启和用户登录注销(2)
1.关机&重启命令 1.1基本介绍 shutdown shutdown -h now :表示立即关机 shutdown -h 1: 表示1分钟后关机 shutdown -r now : ...