类型:
1、值类型
2、null (标签)
3、特殊符号 (< -> < )
4、CDATA
<?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">
<!-- 引用外部bean-->
<bean id="school" class="com.gyk.ObjInject.School">
<property name="location" value="henan"></property>
<property name="num" value="30000"></property>
</bean>
<bean id="school2" class="com.gyk.ObjInject.School">
<property name="location" value="henan"></property>
<property name="num" value="30000"></property>
</bean>
<!-- <bean id="student" class="com.gyk.ObjInject.Student">-->
<!-- <property name="name" value="zs"></property>-->
<!-- <property name="age" value="22"></property>-->
<!-- <property name="school" ref="school"></property>-->
<!-- </bean>-->
<!-- 嵌套内部bean-->
<bean id="student" class="com.gyk.ObjInject.Student">
<property name="name" value="zs"></property>
<property name="age" value="22"></property>
<!-- 对象属性-->
<property name="school">
<bean class="com.gyk.ObjInject.School">
<property name="location" value="henan"></property>
<property name="num" value="30000"></property>
</bean>
</property>
<!-- 设置数组属性-->
<property name="hobbies">
<array>
<value>play</value>
<value>sing</value>
<value>ball</value>
</array>
</property>
<!-- 设置 list 属性-->
<property name="schoolList">
<list>
<ref bean="school"></ref>
<ref bean="school2"></ref>
</list>
</property>
<!-- 设置map 属性-->
<property name="stringSchoolMap">
<list>
<map>
<entry>
<key>
<value>map1</value>
</key>
<ref bean="school"></ref>
</entry>
</map>
<map>
<entry>
<key>
<value>map2</value>
</key>
<ref bean="school2"></ref>
</entry>
</map>
</list>
</property>
</bean>
</beans>
package com.gyk.ObjInject;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* ClassName: Student
* Package: com.gyk.depInject
* Description:
*
* @Author Samuel
* @Create 2024/7/24 09:20
* @Version 1.0
*/
public class Student {
private String name;
private int age;
private School school;
private String[] hobbies;
private List<School> schoolList;
private List<Map<String, School>> stringSchoolMap;
public void setStringSchoolMap(List<Map<String, School>> stringSchoolMap) {
this.stringSchoolMap = stringSchoolMap;
}
public void setSchoolList(List<School> schoolList) {
this.schoolList = schoolList;
}
public void setName(String name) {
this.name = name;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public void setAge(int age) {
this.age = age;
}
public void setSchool(School school) {
this.school = school;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
", hobbies=" + Arrays.toString(hobbies) +
", schoolList=" + schoolList +
", stringSchoolMap=" + stringSchoolMap +
'}';
}
@Test
public void getObj() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ObjInject.xml");
System.out.println(applicationContext.getBean(Student.class));
}
public void setSchoolNum(int num) {
this.school.setNum(num);
}
}
package com.gyk.ObjInject;
/**
* ClassName: School
* Package: com.gyk.depInject
* Description:
*
* @Author Samuel
* @Create 2024/7/24 09:21
* @Version 1.0
*/
public class School {
private String location;
private int num;
public void setLocation(String location) {
this.location = location;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "School{" +
"location='" + location + '\'' +
", num=" + num +
'}';
}
}
2024-07-24 14:09:02 534 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49872d67
2024-07-24 14:09:02 602 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 3 bean definitions from class path resource [ObjInject.xml]
2024-07-24 14:09:02 615 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'school'
2024-07-24 14:09:02 633 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'school2'
2024-07-24 14:09:02 633 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'student'
Student{name='zs', age=22, school=School{location='henan', num=30000}, hobbies=[play, sing, ball], schoolList=[School{location='henan', num=30000}, School{location='henan', num=30000}], stringSchoolMap=[{map1=School{location='henan', num=30000}}, {map2=School{location='henan', num=30000}}]}
util:list util:map
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<util:list id="list">
<ref bean="school"></ref>
<ref bean="school2"></ref>
</util:list>
<util:list id="list2">
<util:map id="map">
<entry>
<key>
<value>school1</value>
</key>
<ref bean="school"></ref>
</entry>
<entry>
<key>
<value>school2</value>
</key>
<ref bean="school2"></ref>
</entry>
</util:map>
</util:list>
<property name="schoolList" ref="list">
<!-- <list>-->
<!-- <ref bean="school"></ref>-->
<!-- <ref bean="school2"></ref>-->
<!-- </list>-->
</property>
p命名空间
<!-- p命名空间 -->
<bean id="school3" class="com.gyk.ObjInject.School" p:num="11" p:location="ww">
</bean>
<bean id="student2" class="com.gyk.ObjInject.Student" p:age="11" p:name="e11" p:hobbies="{1},{1}"
p:schoolList-ref="list"
p:stringSchoolMap-ref="list2">
</bean>
bean 中读取本地 properties配置属性文件 druid 读取配置属性
先加依赖
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
注意配置 context 命名空间url 和 xsi location url。
@Test
public void getPropertiesInfo() throws SQLException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ObjInject.xml");
DruidDataSource student = (DruidDataSource) applicationContext.getBean(DruidDataSource.class);
System.out.println(student.getUrl());
System.out.println(student.getUsername());
System.out.println(student.getPassword());
// System.out.println(student.getConnection());
System.out.println(student.getCreatedTime());
}
jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC
root
root
Thu Jul 25 11:20:26 CST 2024