Spring - 属性注入 5 种方式(XML)
发布日期:2021-06-30 23:47:05
浏览次数:2
分类:技术文章
本文共 6731 字,大约阅读时间需要 22 分钟。
package com.imooc.ioc.demo4;public class User { private String name; private Integer age; public User(String name,Integer age){ this.name = name; this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; }}
package com.imooc.ioc.demo4;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo4 { @Test public void demo1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User)applicationContext.getBean("user"); System.out.println(user); }}
运行结果:User{name='张三', age=23}
Ps:普通的用 value,bean用 ref。
package com.imooc.ioc.demo4;public class Person { private String name; private Integer age; private Cat cat; 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 Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", cat=" + cat + '}'; }}
package com.imooc.ioc.demo4;public class Cat { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Cat{" + "name='" + name + '\'' + '}'; }}
package com.imooc.ioc.demo4;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo4 { @Test public void demo2(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person)applicationContext.getBean("person"); System.out.println(person); }}
运行结果:Person{name='李四', age=32, cat=Cat{name='ketty'}}
运行结果:Person{name='大黄', age=34, cat=Cat{name='小黄'}}
的
package com.imooc.ioc.demo4;public class Category { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Category{" + "name='" + name + '\'' + '}'; }}
package com.imooc.ioc.demo4;public class Product { private String name; private Double price; private Category category; public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } @Override public String toString() { return "Product{" + "name='" + name + '\'' + ", price=" + price + ", category=" + category + '}'; }}
package com.imooc.ioc.demo4;public class ProductInfo { public Double calculatePrice(){ return Math.random() * 199; }}
package com.imooc.ioc.demo4;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo4 { @Test public void demo3(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Product product = (Product)applicationContext.getBean("product"); System.out.println(product); }}
运行结果:Product{name='男装', price=155.43113186211818, category=Category{name='服装'}}
package com.imooc.ioc.demo5;import java.util.*;public class CollectionBean { private String[] arrs; // 数组类型 private Listlist; // List集合类型 private Set set; // Set集合类型 private Map map; // Map集合类型 private Properties properties; // 属性类型 public String[] getArrs() { return arrs; } public void setArrs(String[] arrs) { this.arrs = arrs; } 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; } @Override public String toString() { return "CollectionBean{" + "arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + '}'; }}
aaa bbb ccc
111 222 333 ddd eee fff root 1234
package com.imooc.ioc.demo5;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo5 { @Test public void demo1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); CollectionBean collectionBean = (CollectionBean)applicationContext.getBean("collectionBean"); System.out.println(collectionBean); }}
运行结果:CollectionBean{arrs=[aaa, bbb, ccc], list=[111, 222, 333], set=[ddd, eee, fff], map={aaa=111, bbb=222, ccc=333}, properties={password=1234, username=root}}
转载地址:https://lux-sun.blog.csdn.net/article/details/84191978 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月11日 10时18分48秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
HDOJ2049 不容易系列之(4)——考新郎
2019-04-30
CodeForces 248B - Chilly Willy - 找规律
2019-04-30
POJ-2418 Hardwood Species(Trie树)(map)
2019-04-30
HDU-4300 Clairewd’s message + 4333(扩展KMP)
2019-04-30
HDU 1592 Half of and a Half(高精度)
2019-04-30
POJ-3304 Segments(计算几何)
2019-04-30
UVA-11538 Chess Queen(数学)
2019-04-30
UVA-11401 Triangle Counting(数学优化)
2019-04-30
Codeforces Round #369 (Div. 2)
2019-04-30
UVA 11426 GCD - Extreme (II)(欧拉函数)
2019-04-30
HDU-2838 Cow Sorting(树状数组)
2019-04-30
POJ-2299 Ultra-QuickSort(树状数组)(离散化)
2019-04-30
基于SSM的兼职论坛系统的设计与实现
2019-04-30
基于java的图书管理系统的设计与实现
2019-04-30
基于java的SSM框架理财管理系统的设计与实现
2019-04-30
基于java的ssm框架就业信息管理系统的设计
2019-04-30
基于java的ssm框架的旅游网站设计与实现
2019-04-30
基于java的SSM框架的流浪猫救助网站的设计与实现
2019-04-30
基于java的SSM框架的教务关系系统的设计与实现
2019-04-30