博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSM-Spring-04:Spring的DI的构造注入,P命名注入,和集合注入
阅读量:6954 次
发布时间:2019-06-27

本文共 4953 字,大约阅读时间需要 16 分钟。

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

 

DI和IOC相比,DI更偏向于实现

 

DI的set方式注入在前面入门案例里有写,所以此处不多啰嗦,直接开搞,先说构造注入和P命名注入

    构造方式,理所当然要有带参构造,这儿值得注意的是,你最好再补全一个无参构造,因为你写了带参构造,系统就不再会为你默认补全一个无参构造了,当你在不经意或者不知情的情况下被调用了,就会报错

    P命名则有注意的是那个头文件 xmlns  xsi需要你去配置一道,我下面有,你直接copy就可以

 

    在实体类中(有俩个实体类,我做了关联关系)

 

package cn.dawn.day05diup;/** * Created by Dawn on 2018/3/5. */public class Car {    private String color;    private String type;    public String getColor() {        return color;    }    public void setColor(String color) {        this.color = color;    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }}package cn.dawn.day05diup;/** * Created by Dawn on 2018/3/5. *///student类public class Student {    private String name;    private Integer age;    private Car car;    //带参构造    public Student(String name, Integer age, Car car) {        this.name = name;        this.age = age;        this.car = car;    }    //无参构造    public Student() {    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public Car getCar() {        return car;    }    public void setCar(Car car) {        this.car = car;    }}

 

    在大配置xml文件中

 

 

    没有什么好讲的,带参构造的注入方法,index索引从0开始,对应的是那个带参构造的index

    单测方法:

 

@Test    /*diP命名注入*/    public void t02(){        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");        Student student = (Student) context.getBean("student");        System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType());    }    @Test    /*di构造注入*/    public void t01(){        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");        Student student = (Student) context.getBean("student");        System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType());    }

 

 

集合注入:

    数组,List,Set,Map,Properties

  实体类

 

package cn.dawn.day05diup;import java.util.*;/** * Created by Dawn on 2018/3/5. */public class MyCollection {    private String[] array;    private List
list; private Set
set; private Map
map; private Properties properties; @Override public String toString() { return "MyCollection{
" + "array=" + Arrays.toString(array) + ", list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + '}'; } public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } public List
getList() { return list; } public void setList(List
list) { this.list = list; } public Set
getSet() { return set; } public void setSet(Set
set) { this.set = set; } public Map
getMap() { return map; } public void setMap(Map
map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; }}

 

  大配置中的bean节点

 

孟六
孟六十六
孟六百六十六
奥迪
奥小迪
奥迪迪
set1
set2
set3
孟五
555
v1
v2
v3

 

    单测

 

@Test    /*di集合注入*/    public void t03(){        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");        MyCollection mycollection = (MyCollection) context.getBean("mycollection");        System.out.println(mycollection);    }

 

    由于重写了toString,可以直接一览无余

 

 

 

 

 

 

 

转载地址:http://evjil.baihongyu.com/

你可能感兴趣的文章
.NET开发技巧——从Winform穿越到WPF
查看>>
2135亿背后的双11项目协作怎么玩?
查看>>
DRDS SQL 审计与分析——全面洞察 SQL 之利器
查看>>
微信小程序:模板消息推送实现
查看>>
CodePush自定义更新弹框及下载进度条
查看>>
自己总结的php开发中用到的工具
查看>>
小程序视频或音频自定义可拖拽进度条
查看>>
PHP导出超大的CSV格式的Excel表方案
查看>>
Mac 环境下如何生成Git shh key
查看>>
jenkins 使用磁盘检查插件 disk check plugin
查看>>
使用 Ruby 拓展 Vim
查看>>
centos7下安装LNMP(nginx+PHP7.1.9+mysql5.7)
查看>>
NodeAPI学习之Buffer
查看>>
深入java单例模式
查看>>
create-react-app
查看>>
20170812-XSS跨站脚本攻击
查看>>
Let’s Build |> 使用Elixir,Phoenix和React打造克隆版的Slack(part 1)
查看>>
如何让 StackNaivgator 实现越级回跳
查看>>
工具简述
查看>>
Hbase 集群搭建
查看>>