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 方式实现) 以转账为例 ...
随机推荐
- css---盒模型新增样式
box-shadow 以逗号分割列表来描述一个或多个阴影效果,可以用到几乎任何元素上. 如果元素同时设置了 border-radius ,阴影也会有圆角效果.多个阴影时和多个 text shadows ...
- String 详解
String String对象不可变,当对象创建完毕之后,如果内容改变则会创建一个新的String对象,返回到原地址中. 不可变优点: 多线程安全. 节省空间,提高效率. 源码: public fin ...
- SQL Server SQLGetData()
{ /* 语法 C++ SQLRETURN SQLGetData( SQLHSTMT StatementHandle, SQLUSMALLINT Col_or_Param_Num, SQLSMALLI ...
- thinkphp 前置和后置操作
前置和后置操作指的是在执行某个操作方法之前和之后会自动调用的方法,不过仅对访问控制器有效. 其他的分层控制器层和内部调用控制器的情况下前置和后置操作是无效的. 系统会检测当前操作是否具有前置和后置操作 ...
- 数论剩余系——cf1089F
关于模和互质,很好的题目 /* n两个质因子 x,y有 ax+by=n-1 ax+by=n-1 ax+1+by=n y|ax+1 gcd(x,y)=1 ax%y,a取[1,y-1],就会有[1,y-1 ...
- 【题解】洛谷 P1061 Jam的计数法
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- PAT甲级——A1138 Postorder Traversa【25】
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- 通过apiservice反向代理访问service
第一种:NodePort类型 type: NodePort ports: - port: 80 targetPort: 80 nodePort: 30008 第二种:ClusterIP类型 typ ...
- 2019-8-30-PowerShell-通过-WMI-获取系统安装的驱动
title author date CreateTime categories PowerShell 通过 WMI 获取系统安装的驱动 lindexi 2019-08-30 08:58:39 +080 ...
- Django问题2
接触django是从上个月开始,学习python时间也不长,但我经常在社区看看别人发表的文章,早上看到一篇不错的博客,却一直不能访 问,最终从bing的缓存里找到,因为害怕丢失和忘掉,所以顺便翻译过来 ...