ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
案例代码:https://gitee.com/flymini/codes01/tree/master/spring_/com-learn-spring02 **** 下面演示如何在 IoC 容器里面对属性赋值。 <br/> **1. 创建一个实体类** ```java @Data public class Complex { /** 属性类型为 基本数据类型或字符串,或包装类型 */ private double doubleVar; private String stringVar; private String cataVar; private Long longVar; /** 属性类型为接口 */ private UserDao userDao; private UserDao userDaoTwo; /** 属性类型为一个实体类 */ private Student student; /** 属性类型为集合类型 */ private String stringArray[]; private List<String> stringList; private List<Double> doubleList; private Set<String> stringSet; private Map<String, Object> objectMap; } ``` **2. 在 IoC 容器中对实体类的属性赋值** *`resources/ApplicationContentext.xml`* ```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" xmlns:utils="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> <bean id="complexBean" class="com.learn.spring02.pojo.Complex"> <property name="doubleVar" value="3.14159"/> <property name="stringVar" value="张三,你好!"/> <property name="cataVar"> <!-- 包含特殊字符的字符串写到<![CDATA[val]]>,如<、>等 --> <value><![CDATA[<<南京>>]]></value> </property> <property name="longVar" value="1000"/> <property name="userDao" ref="userDaoImpl"/> <property name="userDaoTwo"> <bean id="userDaoTwo" class="com.learn.spring02.dao.impl.UserDaoImpl"/> </property> <property name="student" ref="studentBean"/> <property name="stringArray"> <array> <value>中国</value> <value>美国</value> <value>俄罗斯</value> </array> </property> <property name="stringList"> <list> <value>中国</value> <value>美国</value> <value>俄罗斯</value> </list> </property> <property name="doubleList" ref="moneyList"/> <property name="stringSet"> <set> <value>中国</value> <value>美国</value> <value>俄罗斯</value> </set> </property> <property name="objectMap"> <map> <entry key="zhongguo" value="中国"></entry> <entry key="student" value-ref="studentBean"></entry> </map> </property> </bean> <bean id="userDaoImpl" class="com.learn.spring02.dao.impl.UserDaoImpl"/> <bean id="studentBean" class="com.learn.spring02.pojo.Student"> <property name="studentName" value="王五"/> <property name="studentAge" value="25"/> </bean> <!-- 也有对应的 set、map --> <utils:list id="moneyList"> <value>79.45</value> <value>80.05</value> </utils:list> </beans> ``` **3. 测试** ```java public class ComplexTest { /** * 获取 IoC 容器 */ private final ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContentext.xml"); @Test public void testComplex() { Complex complex = context.getBean("complexBean", Complex.class); //3.14159 System.out.println(complex.getDoubleVar()); //张三,你好! System.out.println(complex.getStringVar()); //<<南京>> System.out.println(complex.getCataVar()); //1000 System.out.println(complex.getLongVar()); //UserDaoImpl.print() complex.getUserDao().print(); //UserDaoImpl.print() complex.getUserDaoTwo().print(); //Student(studentName=王五, studentAge=25) System.out.println(complex.getStudent()); //[中国, 美国, 俄罗斯] System.out.println(Arrays.asList(complex.getStringArray())); //[中国, 美国, 俄罗斯] System.out.println(complex.getStringList()); //[79.45, 80.05] System.out.println(complex.getDoubleList()); //[中国, 美国, 俄罗斯] System.out.println(complex.getStringSet()); //{zhongguo=中国, student=Student(studentName=王五, studentAge=25)} System.out.println(complex.getObjectMap()); } } ```