博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring ioc---参数注入
阅读量:4495 次
发布时间:2019-06-08

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

注入参数:

1,基本类型值;

2,注入bean;
3,内部bean;
4,null 值;
5,级联属性;
6,集合类型属性;

package entity;public class Dog {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    }
package entity;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class People {    private int id;    private String name;    private int age;    private Dog dog;   //级联属性    private List
hobbies=new ArrayList
(); private Set
loves=new HashSet
(); private Map
works=new HashMap
(); private Properties addresses=new Properties(); public People() { super(); // TODO Auto-generated constructor stub } public People(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public Set
getLoves() { return loves; } public void setLoves(Set
loves) { this.loves = loves; } public List
getHobbies() { return hobbies; } public void setHobbies(List
hobbies) { this.hobbies = hobbies; } public Map
getWorks() { return works; } public void setWorks(Map
works) { this.works = works; } public Properties getAddresses() { return addresses; } public void setAddresses(Properties addresses) { this.addresses = addresses; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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 "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog + ", hobbies=" + hobbies + ", loves=" + loves + ", works=" + works + ", addresses=" + addresses + "]"; } }
package test;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import entity.People;public class T {    private ApplicationContext ac;    @Before    public void setUp() throws Exception {        ac=new ClassPathXmlApplicationContext("beans.xml");    }    // 基本类型值    @Test    public void test1() {        People people=(People)ac.getBean("people1");        System.out.println(people);    }        // 注入bean    @Test    public void test2() {        People people=(People)ac.getBean("people2");        System.out.println(people);    }        // 内部bean    @Test    public void test3() {        People people=(People)ac.getBean("people3");        System.out.println(people);    }        // 注入null    @Test    public void test4() {        People people=(People)ac.getBean("people4");        System.out.println(people);    }        // 级联属性    @Test    public void test5() {        People people=(People)ac.getBean("people5");        System.out.println(people);    }        // 注入集合    @Test    public void test6() {        People people=(People)ac.getBean("people6");        System.out.println(people);    }}
唱歌
跳舞
唱歌2
跳舞2
上午
写代码
下午
测试代码
aaaaa
bbbbb

 

转载于:https://www.cnblogs.com/lcpholdon/p/4508527.html

你可能感兴趣的文章
开园第一笔
查看>>
Houdini 节点参数读取输入节点的数据列表
查看>>
初识Linq to Entity
查看>>
Linux vmstat命令实战详解
查看>>
FastDFS在centos上的安装配置与使用
查看>>
HDU 1709 The Balance
查看>>
2016/7/7 设置wamp2.5 mysql密码 重点是mysql版本
查看>>
简介几种负载均衡原理
查看>>
micropython logging文档
查看>>
Webform(分页、组合查询)
查看>>
Foundation - NSDate
查看>>
geatpy - 遗传和进化算法相关算子的库函数(python)
查看>>
iOS 线程安全
查看>>
mysql 分组之后统计记录条数
查看>>
New STL Algorithms That Will Make A More Productive Developer
查看>>
js 对象 浅拷贝 和 深拷贝
查看>>
初识 python
查看>>
PCL Examples
查看>>
spring boot
查看>>
浏览器URL传参最大长度问题
查看>>