java set 赋值 list_bean的属性赋值(p名称命名空间,set赋值。构造器赋值,List集合赋值,Map集合赋值)...

该博客主要展示了如何在Java中使用Set进行赋值,包括通过有参构造、set方法、Spring自动匹配构造器、引用外部bean、给List集合和Map集合赋值的不同方式。通过单元测试详细演示了这些赋值操作的过程。
摘要由CSDN通过智能技术生成

packagecom.test;importcom.charon.enty.Acount;importcom.charon.enty.Student;importorg.junit.Before;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importjava.util.HashMap;/*** @Description TODO

* @Author Charon <1819248612@qq.com>

* @create 2020-11-06-0:45

* @Version 1.0.0*/

public classStudentTest {

ApplicationContext applicationContext;

@Beforepublic voidbefore(){//定义Spring配置文件的位置

String xmlPath = "ApplicationContext.xml";//初始化Spring配置文件,加载配置文件

applicationContext = newClassPathXmlApplicationContext(xmlPath);

}/*** 测试通过有参构造给bean赋值*/@Testpublic voidtest(){

Student student= (Student) applicationContext.getBean("student1");

System.out.println(student.getStuName());

}/*** 测试通过有参构造给bean赋值*/@Testpublic voidtest1(){

Student student= (Student) applicationContext.getBean("student2");

student.setStuName("李四");

System.out.println(student.getStuName());

}/*** Spring 自动匹配合适的构造器;*/@Testpublic voidtest3(){

Student student3= (Student)applicationContext.getBean("student3");

System.out.println(student3.getStuName());

System.out.println(student3.getStuAge());

System.out.println(student3.getStuDept());

}/*** 测试引用外部bean给对象赋值*/@Testpublic voidtest4(){

Student student= (Student) applicationContext.getBean("student4");

System.out.println(student.getStuName());

System.out.println(student.getAcount().getIdCard());

}/*** 测试给List集合赋值结果*/@Testpublic voidtest5(){

Acount acount= (Acount)applicationContext.getBean("list1");for (int i = 0; i < acount.getHobby().size(); i++) {

System.out.println(acount.getHobby().get(i));

}

}/*** 测试给Map集合赋值结果*/@Testpublic voidtest6(){

Acount acount= (Acount)applicationContext.getBean("account3");

HashMap hashMap =acount.getHashMap();

System.out.println(hashMap.get("name"));

System.out.println(hashMap.get("age"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值