依赖:

  在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方式的更多相关文章

  1. Spring基于XML方式的使用

    一.IoC配置 IoC的配置是通过Spring的xml文件的bean标签进行的. 1.bean标签介绍 bean标签一般是在xml文件进行配置的,xml文件一般样式如下: <?xml versi ...

  2. 【Spring】XML方式实现(无参构造 有参构造)和注解方式实现 IoC

    文章目录 Spring IoC的实现方式 XML方式实现 通过无参构造方法来创建 1.编写一个User实体类 2.编写我们的spring文件 3.测试类 UserTest.java 4.测试结果 通过 ...

  3. Spring通过XML方式实现定时任务

    package com.wisezone.service; import java.text.SimpleDateFormat; import java.util.Date; import org.s ...

  4. Spring基于XML方式加载Bean定义信息(又名:Spring IOC源码时序图)-图解

  5. Spring 简单使用IoC与DI——XML配置

    目录 Spring简介 导入jar包 Spring配置文件 Spring的IoC IoC简介 快速使用IoC Spring创建对象的三种方式 使用构造方法 使用实例工厂 使用静态静态工厂 Spring ...

  6. Spring总结四:IOC和DI 注解方式

    首先我们要了解注解和xml配置的区别: 作用一样,但是注解写在Bean的上方来代替我们之前在xml文件中所做的bean配置,也就是说我们使用了注解的方式,就不用再xml里面进行配置了,相对来说注解方式 ...

  7. spring aop 使用xml方式的简单总结

    spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...

  8. 使用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...”. 原 ...

  9. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  10. Spring声明式事务管理(基于XML方式实现)

    --------------------siwuxie095                             Spring 声明式事务管理(基于 XML 方式实现)         以转账为例 ...

随机推荐

  1. 5.从物理层到MAC层

    第一层(物理层)     如何用两台电脑构成最小的局域网(LAN)?     网线的水晶头1.2和3.6脚分别起着收.发信号的作用,随意只要将水晶头做交叉线1-3.2-6交叉法,然后连接两台电脑.除了 ...

  2. Educational Codeforces Round 27 D. Driving Test

    单调栈 题意看了半天... #include <cstdio> #include <cstdlib> #include <cmath> #include <c ...

  3. Batch - Windows Batch 常用命令

    比较符号(不能用 < , >) The reason operators like > are not used is because they have special meani ...

  4. C++ SOCKET 基础编程

    { http://c.biancheng.net/socket/ }

  5. day29 面向对象入门

    Python之路,Day17 = Python基础17-面向对象入门 创建类和对象 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “ ...

  6. day 53 Django基础二之URL路由系统

    Django基础二之URL路由系统   本节目录 一 URL配置 二 正则表达式详解 三 分组命名匹配 四 命名URL(别名)和URL反向解析 五 命名空间模式 一 URL配置 Django 1.11 ...

  7. git push到多个不同的远程仓库

    1.若现在本地有一个已经和github远程仓库关联好的本地仓库,平时都会将本地仓库push到github上. 2.有一天突然发现"码云"这个远程仓库网站,咦!这个还可以创建priv ...

  8. USACO 2008 November Gold Cheering up the Cows /// MST oj24381

    题目大意: 输入n,p:n个点,p条路 接下来n行输入c[]:在各个点需要花费的时间 接下来p行输入u,v,w:u点到v点的路需要花费时间w 求经过所有点且最后回到起点的最少花费时间 https:// ...

  9. webpack 简单笔记(一)

    安装部分不介绍了 (一)第一个最简单的demo,单入口,单文件 目录结构: webapck.config.js中代码: 'use strict' const path = require('path' ...

  10. nginx压力测试webbench

    下载压力测试工具webbench wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz 安装依赖包 yum -y in ...