spring1-test4-正确为各种属性赋值
2020-04-02 07:52
701 查看
实验4-正确为各种属性赋值
1.测试使用null值,ref引用外部的值
2.引用类型赋值(引用其他bean、引用内部bean)
3.集合类型赋值(List、Map、Properties)
4.util名称空间创建集合类型bean,引用一个可以外部引用的集合
5.级联属性赋值
1.测试使用null值,ref引用外部的值:
- ioc2.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 实验4:正确的为各种属性赋值 --> <!-- 1.测试使用null值,ref引用外部的值 2.引用类型赋值(引用其他bean、引用内部bean) 3.集合类型赋值(List、Map、Properties) 4.util名称空间创建集合类型bean,引用一个可以外部引用的集合 5.级联属性赋值 --> <bean id="person1" class="Person"> <!-- 在标签里面进行复杂的赋值 --> <!-- 使name=null --> <property name="name"> <null></null> </property> <!-- ref: 代表引用外面的值 --> <property name="car" ref="car01"></property> </bean> <bean id="car01" class="Car"> <property name="carName" value="宝马"></property> <property name="color" value="绿色"></property> <property name="price" value="300000"></property> </bean> </beans>
- Person类:
import java.util.List; import java.util.Map; import java.util.Properties; public class Person { //基本类型直接使用<property>标签赋值 private String name; private Integer age; private String gender; private String email; private Car car; private List<Book> books; private Map<String, Object> maps; private Properties properties; public Person() { System.out.println("调用了无参的构造方法。。。"); } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public Person(String name, Integer age, String gender, String email) { this.name = name; this.age = age; this.gender = gender; this.email = email; } 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 String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", email='" + email + '\'' + ", car=" + car + ", books=" + books + ", maps=" + maps + ", properties=" + properties + '}'; } }
- Car类:
public class Car { private String carName; private Integer price; private String color; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car{" + "carName='" + carName + '\'' + ", price=" + price + ", color='" + color + '\'' + '}'; } }
- Book类:
public class Book { private String bookName; private String author; public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @Override public String toString() { return "Book{" + "bookName='" + bookName + '\'' + ", author='" + author + '\'' + '}'; } }
- test:
@Test public void test07(){ ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc2.xml"); //1. 测试使用null值,默认引用类型是null,基本类型是默认值 Person bean = (Person) ioc.getBean("person1"); System.out.println(bean); System.out.println(bean.getCar()); Car car = bean.getCar(); Object car01 = ioc.getBean("car01"); System.out.println(car==car01); }
2.引用类型赋值(引用其他bean、引用内部bean)
- 测试类:
@Test public void test08() { ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc2.xml"); //从ioc容器中获取person,再通过person获取到car值 Person person1 = (Person) ioc.getBean("person1"); Car car = person1.getCar(); System.out.println(car); }
- 在person内部写一个car。这叫内部bean
<bean id="person1" class="Person"> <!-- 在标签里面进行复杂的赋值 --> <!-- 使name=null --> <property name="name"> <null></null> </property> <!-- ref: 代表引用外面的值 --> <!--<property name="car" ref="car01"></property>--> <property name="car"> <!--对象我们可以使用bean标签创建,car = new Car();引用内部bean--> <bean class="Car"> <property name="carName" value="自行车"></property> </bean> </property> </bean>
3.集合类型赋值(List、Map、Properties)
1.List集合
<bean id="book2" class="Book"> <property name="bookName" value="东游记"></property> </bean> <bean id="person2" class="Person"> <!-- 如何为list属性赋值呢?--> <property name="books"> <!-- 这么写就相当于books = new ArrayList<Book>() --> <list> <!--list标签中添加每一个元素--> <!-- 内部的id="boook000x"并不能被识别,引用内部bean时,不能被获取到 只能内部使用--> <bean id="book000x" class="Book" p:bookName="西游记"></bean> <ref bean="book2"></ref> </list> </property> </bean>
public void test09() { ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc2.xml"); Person person2 = (Person) ioc.getBean("person2"); List<Book> books = person2.getBooks(); System.out.println(books); }
2.Map集合
@Test public void test10() { ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc2.xml"); Person person2 = (Person) ioc.getBean("person2"); Map<String, Object> maps1 = person2.getMaps(); System.out.println(maps1); }
<bean id="person2" class="Person"> <!--给map赋值,maps=new LinkedHashMap<>();--> <property name="maps"> <map> <!--一个entry代表一个键值对--> <entry key="key01" value="张三"></entry> <entry key="key02" value="18"></entry> <entry key="key03" value-ref="book2"></entry> <entry key="key04" > <bean class="Car"> <property name="carName" value="benci"></property> </bean> </entry> <entry key="key05" > <map></map> </entry> </map> </property> </bean>
3.Properties属性赋值
@Test public void test11() { ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc2.xml"); Person person2 = (Person) ioc.getBean("person2"); Properties person2Properties = person2.getProperties(); System.out.println(person2Properties); }
<bean id="person2" class="Person"> <!-- 为Properties赋值--> <property name="properties"> <!-- properties = new Properties();所有的key和value都是String类型的--> <props> <prop key="username">root</prop> <prop key="password">123</prop> </props> </property> </bean>
4.util名称空间创建集合类型bean,方便之后代码中可以引用一个外部的集合
@Test public void test12() { ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc2.xml"); Person person2 = (Person) ioc.getBean("person03"); Map<String, Object> maps = person2.getMaps(); System.out.println(maps); Map<String, Object> bean = (Map<String, Object>) ioc.getBean("myMap"); System.out.println(bean.getClass()); }
<util:map id="myMap"> <!--只是添加元素--> <entry key="key01" value="张三"></entry> <entry key="key02" value="18"></entry> <entry key="key03" value-ref="book2"></entry> <entry key="key04" > <bean class="Car"> <property name="carName" value="benci"></property> </bean> </entry> <entry key="key05" > <map></map> </entry> </util:map> <!--util名称空间创建集合类型的bean,方便别人引用--> <bean id="person03" class="Person"> <property name="maps" ref="myMap"></property> </bean> <util:list id="myList"> <list></list> <bean class="Person"></bean> <value>12</value> <ref bean="myMap/> </util:map> //这个list里面有4个值,[[],Person对象,12,{}]
5.级联属性赋值
<bean id="car01" class="Car"> <property name="carName" value="宝马"></property> <property name="color" value="绿色"></property> <property name="price" value="300000"></property> </bean> <!--级联属性:就是属性的属性,就像person对象的car属性--> <bean id="person04" class="Person"> <!--为car赋值的时候,顺便改下car的值--> <property name="car" ref="car01"></property> <property name="car.price" value="900000"></property> </bean>
@Test public void test13() { ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc2.xml"); Person person04 = (Person) ioc.getBean("person04"); Object car01 = ioc.getBean("car01"); System.out.println("这是容器中的car"+car01); System.out.println("这是person中的car"+person04.getCar()); }
级联属性可以修改属性的属性,原来的值会被修改。
引用时,会一改都改,值都会被修改。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Spring如何装配各种集合类属性
- Spring中属性配置(配置Properties属性值(通过props,props子节点 )为属性赋值)
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- spring 集合属性赋值
- 【小家Spring】Spring IoC是如何使用BeanWrapper和Java内省结合起来给Bean属性赋值的
- Spring 与属性赋值相关的注解之 @Value/@PropertySources/@PropertySource
- 使用Spring实现属性文件给常量赋值
- Spring对Bean属性赋值(注入)
- 简单的spring使用反射实现属性赋值demo
- spring中给属性赋值的三种方式
- spring(DI) 懒加载的执行顺序、通过构造函数给属性赋值
- Spring注解开发之属性赋值
- spring sessionFactory 属性配置详解,applicationContext中各种属性详解
- Spring各种属性的注入配置
- ITCAST视频-Spring学习笔记(Spring如何装配各种集合类型的属性)
- Spring中如何给对象的属性赋值
- Spring注解驱动第九讲--属性赋值
- 曹工说Spring Boot源码(27)-- Spring的component-scan,光是include-filter属性的各种配置方式,就够玩半天了.md
- Spring如何装配各种集合类型的属性
- spring的xml中p名称空间与C名称空间简化属性赋值